android, find the nearest location without going through all the locations - android

I am building a taxi dispatching app
first: I need the passenger app to show the nearest taxis, now I know how to do that in code but in my way I have to go through all the taxis locations(in database on server) and calculate the distance from the passenger location and get the lowest ones - I don't want to do this because there will be a lot of taxis and going through all of them and making some math is kind of pain on the server- is there a way to get the nearest ones without going through all of them?
second: what the best database to use - first, should it be sql or non-sql - I need a very robust database, because there will be a lot of updates (I mean the passengers locations added then deleted when arriving to destination && the taxis location changes frequently)
finally: I will use RubyOnRails to do the server side and json as data transfer format, do you have a recommendation to me on something better?
Thanks

Calculating distances takes time because of the complex equations involved.
Instead, try to do a more simplistic "fake distance" calculations.
For example, instead of d = SQRT (x*x + y*y) for your purpose you may try something like
d = |x| + |y| and sort for that. You don;t need the exact formula here because a rough approximation would do.

For first: Use you need to calculate nearest distance. But instead of comparing with all taxi location i recommend you to pass lat long of passenger.
On server maintain taxi location in lat long. Write query to get taxi's with in +/- 3 of lat long. This will get limited no of taxi's available near passenger area.
For sencond: = Use Relational database, SQL Server is good for such applications I have used in my last projects.
For server: I don't have idea of RubyOnRails but this is also faster & easy to implements as they says.. , you have choose the best data exchange format Json no need to change this; you can use zip to improve performance.

For the first question:
I think you can send from the mobile device a radio (ie: /43.34343/-3.3333/1000 and 1000 represents the radio in meters), and from the server return only the taxis included on this area.
You can do that calculating the distance between the device location, and the list of the Taxis.

Try this link
It will show you all the nearest places like Hotels,theatres,etc..
it will show the address of the particular place in list and also it will show in map view..
Hope this will be helpfull for you.

Related

Android location based application that needs to work with radius calculation

I'm building a location based application.
Lets say that i have user A and i have a latitude and longitude values of his current location.
I got user B,C,D and thier locations as well..
For the example - user B + D are in a radius of 5km from user A and user C is in 12km radius from user A and I want to know how can i make a function that will tell me that whose near to me by 4/5/6/7km and etc..
If i user A wants users that are 8.5km away from him i will have as a result user B + D and thier distance from user A.
Now.. i know that i can use the Location class and use the distance function to calculate the distance between two users.
But the problem is that if i want to calculate that for the radius distance i need to fetch the entire users list from my database and send it to the client to start calculating distances between him and those i fetched from the server.
Now i dont want to do that off course if there is a better and more effecient way..
Firstly, I thought off using http request or some mathematical functions to calculate the distance between my users on the server side but the client (Android) offers very good tools to do so , so because of that I am lost of knowing to is the best thing to do.
Thanks head up :)
Hey don't you think that always sending locations of n-1 users to one requesting from server is inefficient and consumes unnecessary bandwidth than sending only few in the radius vicinity? The server can easily do this computation.
Think of a scenario where your app user base grows and grows? Then what?
Such a computation is always performed on the server.
Also nearly all of the times, server has much more computing power than the client. So even though android tools look lucrative, don't end up using those in a scenario like this.
In terms of tools, there are similar on the server side too ex. the haversine function. Also some databases like mongo also have inbuilt location filters. So this is really worth checking out.

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?

Geo Fencing in Android

I am trying to build geo fencing in my android application. however i am not clear with this idea. whenever user opens my app, i want to see if he is in any AMC theater. how do i do that? should i use latitude longitude values or i should use physical address? if i use lat lon values how accurate will the output be? if i go for physical address, how time efficient will it be?
i have currently written a code wherein i have hardcoded the values. but thats not going to work for real app. my app can track users coordinates but i am not clear how this data can b put to use.
You need to use latitude and longitude, with very less accuracy (may be 20meters or less), and just look for Satellite GPS coordinates. If the device gets a satellite GPS fix, it would have very good accuracy (sometimes as close as 5m), and based on that, do whatever task you want to perform.
Best bet is to get the lat/lon coordinates of nearby AMC Theaters from the Google Places API. Calculate the distance to any of the points and use a threshold to determine if you're in the general vicinity of the building. The geolocation point is bound to be on the edge of the building, not the center of it (or perhaps even on the street), so it's unlikely you can just use a simple radius from the point.
For geo fencing, the best bet is to scan for the WiFi networks in the area and if any match the properties of your desired location, then you're in it. Of course, good luck getting that data. The other thing you could do to ensure users are in the venue is to plant NFC tags in the building and have them scan the tags. Again, this may only be feasible if you're doing a contract for AMC. QR codes are too easy to duplicate. Anyone could take a picture of one and scan it at any time.

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.

Do something when phone reaches a set of location?

I have a list of coordinates in the database identified as POI. For a city could be >100 records.
I would like to get notified when the phone gets in 150 meters range of one of the location. The location coordinates too has an error/radius, usually 10 to 100meters. Since I don't find it good to add each location(could be hundreds) for a trigger, how can I optimize the wake-up code?
Also do I have options to remove a previously setup notification from the queue?
You could store your POIs in some sort of intelligent Hash-Table using the coordinates to compute a unique hash. Each time a location update arrives you make a lookup in your hash-table to see if there are POIs near the current location. This lookup should only take O(1), since it is a hash-lookup.
The desired range should be taken into account when computing the hashes and storing the POIs.
Just an idea!
Kind regards,
mefiX
There's an app named Locale, that can toggle various events based on your GPS location OR available Wifi network OR cell-station id, etc
It also has a plugins interface. It could be useful for you to examine that app and, maybe, write a plugin for it.
This problem reminds me of graphics in video games. There's no need to load the points that are well outside your range of movement. I'd break down the map into a grid, set triggers for the 8 adjecent grid blocks and then for each of the POI within the current grid block. When a new grid block is reached the triggers are updated. It'd probably be smart to overlap the grid blocks considering the range of error.

Categories

Resources