# Finding nearest Stop location (DB) from current location

I could not think of a Title which exactly describes my problem, but I will try to explain.

I have a bus route as DB table where Lat/Long of each Bus stop is stored.

Now, I get live GPS cordinates and when the bus reaches a particular point, I want to know whether its close to Stop S2 or S3 - The key is that least distance should be measured along the bus route , not the shortest linear distance ignoring say a curve.

I am using Google maps API. Kindly suggest which path to take.Please note that its not a nearest place from current location using google places API.

-

Not sure if you're going to be able to use the Google Maps api to do this. You more likely need a bespoke gis solution, by which I mean probably a spatial database which stores the bus routes and then works out firstly the nearest point on the route (which probably involves firstly finding the nearest route, then the nearest node on that route), otherwise with a large network you would have to iterate through thousands of route points.

The next task is to work out whether it is nearer along the line to one stop than the other stop. This is fairly easily done, so long as you have access to the underlying line geometry.

If you can get the route geometry in Gmaps there may be a way of iterating through the line segments and doing various "nearest" calculations on them, but the Google Maps API is quite a long way from real spatial database functionality, ad would probably involve a lot of API calls, unless you can implement your own solution for things like converting lat/long to distances on the ground.

Otherwise, you're stuck using the less accurate method.

-
Hi Steve , we are using MySQL and as per my knowledge, the spatial capabilities are limited.Do you mean to say that we need a backend Desktop GIS process where the coordinates from GPS are converted to networks and stored in a spatial DB ? or do you suggest we can automate this process using some algorithm ? – Adarsh Madhavan Oct 11 '12 at 12:43
I think the newest version of MySQL is ok for spatial. Why don't you use that and do all the spatial stuff with that rather than Google? With a spatial DB, you can do spatial processing within the app itself, no need for a desktop GIS. a spatial DB is already a GIS. – Stev_k Oct 11 '12 at 13:29
Just because something is a "spatial DB", doesn't mean it is able to do all sorts of spatial operations. OP is in particular asking for very specific routing functionality, which may not be part of it. Furthermore OP may not have any data for the area in question, but only what Google has internally. – Uffe Kousgaard Oct 12 '12 at 5:33
@ Uffe Kousgaard, While I agree to your first point regarding functionality , The second point may not be needed from Google for this application.. ie the possible bus stops and route locations are available in our internal database .But yes, we would like some POIs from Google or Openstreet for users to relate the stops to known benchmarked places. – Adarsh Madhavan Oct 12 '12 at 6:21
@Uffe Kousgaard - I don't think this is routing in its truest sense at all - it's a problem constrained by one link in a network so it can use standard geospatial tools to find a) the nearest vertex of the route to the user position and b) is the distance from that vertex to stop s2 greater or less than the distance from that vertex to stop s3. If that is the problem, no specific "routing" functionality is required – Stev_k Oct 12 '12 at 8:46

My approach would be to first check which vertex of the polyline lies the closest to the actual position. If you have that, you can grab the vertices of the polyline from vertex 0 till that vertex and create a new google maps polyline from those. Note, that you will not plot that new polyline on the map, but will only use it for determining the distance to the next stop. If your polyline is not segmented i.e. it is not made up of separate polylines between bus stops, you can do the following: Determine the closest polyline vertex to the bus' location and determine the closest polyline vertices to the stations. Then all those points will be one of the vertices of the big polyline. Then you can use the getAt method of the polyline object to determine which point of the polyline is the bus stop and the bus location is, and then you can do the same that I have explained in my first paragraph. For me it sounded simple, but please let me know how it works for you! I hope it helped!

-