How to determine which persons are close to you from a database - android

Let's say we have a few 100.000 people and they all share their location.
Their location is stored in a database online as Geopoint.
If I open an app on my device and I only want to show the let's say 50 closest people, what would be the way to approach this.
I know I can just retrieve every bodies location from that database and check the distance for every single device. But that seems to be not a very effective way. Especially if it would become millions of people.
What is the proper way to approach this?

Related

How to optimize spatial data retrieval in Android

I have SQLite table contains 1.5M lines, the table contains <lat,lng, info>, what i want to do is, for every time I receive data "lat,lng" from the GPS, I should access the table and display the corresponding info.
my question is, is this operation will be exhaustive and time consuming? if yes, how can I achieve better data retrieval fro mthe table every time i receive GPS data?
You should use Spatialite, it is a Sqlite extension to use with geographical data, it allows you to create spatial indexes and then query data which is included in a rectangle, for example around x meters from your gps coordinates.
You do no want to search for an exact location (GPS is not accurate enough for that), but for nearby entries.
Two-dimensional interval searches are not efficient when using 'normal' indexes.
You would need to use something like an R-tree index, which is not available on Android by default.
If your table never changes, you could prepare the R-tree on another machine, and in the app, search for entries manually.
I don't know exactly what your data looks like but the first thing I'd do (given that GPS position may not evolve much) is to first create a temp table with a smaller range of coordinates so you can probably reduce the 1.5M records into some thousands only. Then everything should be faster.
I didn't know about the R-Tree #CL spoke about but indeed they look very promising if only you could manage to use them under android.
Well if your use case is not as other suggests - means that you are looking for an exact position rather then an area, Then all you can do is make sure you add an index on (lat,lng) and pray for the best...
CREATE INDEX index_lat_lng ON MyTable(lat,lng);
Your question is far to woolly.
what i want to do is, for every time I receive data "lat,lng" from the GPS, I should access the table and display the corresponding info.
You need to define how often you receive data.
Will it be exhaustive and time consuming? Maybe, maybe not, without knowing how often you receive data.
Given the above, have you considered and assuming that the GPS co-ordinates are not coming in from all over the shop, creating an in memory database snapshot of the area in and around where your latitude and longitudes are currently coming from and using that until such time as they stray from the current snapshot area. At that point you will need to write out the existing data create a new snapshot for the new co-ordinates. The snapshot areas could be defined by degrees, minutes or seconds depending how fast the thing, that I suppose you are tracking, is expected to move.
You can try just with a command like:
SELECT info FROM Table WHERE lat = 'lat' AND lng = 'long';
Doesn't that just work well enough for your purpose?

Discarding a message, before it gets displayed to the user?

Im trying to develop an Android messenger application where a message is sent by user 1 to everyone using the app, the message contains the GPS location of user 1. Is it possible to determine the distance between user 1 and the user 2 and based on that either choose to display or discard the message. How do I go about it? Cloud you point out considerations that I might have missed out?
Another method I believe is possible is to periodically update a server with every users GPS location and then let the server decide who gets the message, but I would not like to use this method as it would be a privacy issue. No one would want their whereabouts being tracked by a server all day. Is there another solution to this?
You can use the haversine formula to determine the distance between two locations. Here is a link that provides the formula in various languages.
http://www.movable-type.co.uk/scripts/latlong.html
One way of doing this is to store the most recent location of a phone on the server in the database. You can then query the database (using a stored procedure) to determine who your closest neighbors (phones) are by setting a radius. For instance, show me all the phones within 500 meters.
Here is an example of how to do this:
http://www.movable-type.co.uk/scripts/latlong-db.html

Best Practice to Save GPS Coordinates

I'm developing my first android app and one of the features is tracking the user's route. At another point in the app I'm going to have a review and bring up another map to plot the locations. I implemented the LocationListener and set the minDistance to 50 meter and everything is working correctly. In the onLocationChange I add the new location to a list. I'm wondering what, in your opinion, is the best way to save this list? Currently I'm just writing the list to the local SQLite db, but I'm wondering if this best or if there are other alternatives I should explore.
Thanks

Find best matching routes from GPS data

I'm making an application where users can save GPS data from phone to server when they travel their everyday routes. For example they are heading from home to work. GPS data is stored in a database.
Now, user wants to know maybe there's more people who travels this route too. I want to compare different users routes and give to user for example 3-5 best matches from other users routes.
Important is compare the whole trip, because users can join their routes and go work together from starting at some point not just from beginning and end. Also I think important is the destination point from the users view, who is searching other users routes. Other user route must be near by the searchers route end.
There are two factors - time and location. One user drives with a car and another walks and takes a bus for example. The one who walks starts his trip earlier, another later, because he travels this route faster. In one point at certain time their routes are matching.
How can be routes compared? Is there algorithm(s) for that? Do I need to compare every point in a route?
Essentially you are talking about a combination of routing algorithms and traveling-sales-man
The most common routing algorithm was invented by Dijkstra some 50 years ago, and calculate the best way of getting from point a to point b in a directed network -- in routing applications that means that each road is represented as a edge in the network, and where each edge is associated with a "cost" i.e. the time it take to travel down the road, or the average speed, or in your case it would be the number of people traveling on that route.
The Traveling Sales Man is a slightly different but also related, trying to optimize for the number of nodes visited -- in your case it is probably solving the opposite as it is trying to maximize the number of cities (edge-intersection nodes) while minimizing the cost of traveling to all the nodes -- worth understanding if you want to solve this problem
See GIS - it's a (HUGE) field of study - very interesting but very intense
I think what you would have to do is to convert the GPS co-ordinates that you get from the phone, into routes. Have a look at this Open Route Service which is part of the Open Street Map project.
Once you have each person's latitude and longitude converted to a common set of routes, then it will be easier comparing their paths to see if they have something in common. You could also do things like search for alternate routes. Perhaps one person by traveling a couple of extra mile/km can travel together with another group of 4-5 people going in basically the same direction. Things like that.

Android Location services

I need to create a database of gps coordinates by walking some trails and periodically storing coordinates. What would be the best way to get gps coordinates quickly sent and stored on the phone in an array of some sort so I could later access the coordinates. I need to be able to save them on the phone and then get them back to the computer. Any tips will be helpful. thank you
Here's a great page on acquiring location and I'd suggest you store the locations received in a Sqlite database, which means the location data will persist between each time you run the app, which seems like what you need.
As each location comes in, you can store them in the database, and from there it's easy to handle them, e.g. pass them on. Not sure what you mean by "get them back to the computer, as you don't say why or in what format.

Categories

Resources