[GIS] Finding nearest Stop location (DB) from current location

google mapsrouting

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.

Diagram showing 4 Bus stops and current GPS (star

Best Answer

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.

Related Question