0

I've got a live GPS feed for a bus network, they want to be able to track if a bus goes off of route. I can get the bus stops and their stop coordinates for each bus service. The bus stops are commonly quite close together and using Google Maps it can create a route between the stops matching the path of the bus route.

My question is, preferably using the Google Maps JavaScript or Web Services API how would I detect if this GPS point is off of the route?

If there a better alternative to Google Maps?

All help is appreciated, thank you.

samtrod
  • 271
  • 2
  • 13
  • you didn't show us your data or any code at all... if you need help with the basic math aspect of it try math.SE.. or google... https://brilliant.org/wiki/distance-between-point-and-line/ – I wrestled a bear once. Feb 02 '18 at 19:55
  • The code for retrieving the GPS location of the buses and their stops is irrelevant to the question. I want to know the method of calculating if the GPS location is on a Google Maps route or not. You link a site which is great, but how would I get the GPS coordinates of a Google Map Route? Roads in the Scotland are bendy, so I need to be able to get the point along any part of a route. How do I do that? – samtrod Feb 02 '18 at 20:03
  • the only way the code is not relevant is if the question is not relevant. you either need to show your code or ask on the math stackexchange site where they don't require code... or like i said, try googling yourself, there's lots of info out there.. https://stackoverflow.com/questions/3609049/shortest-distance-between-point-and-path good luck – I wrestled a bear once. Feb 02 '18 at 20:07
  • Sorry if I wasn't clear enough in my explanation, but I have since found a solution and posted it. – samtrod Feb 04 '18 at 14:00

1 Answers1

1

I've discovered a method of achieving this, using the GPS location of the bus stops you can create waypoints along a Google Map route, using the Google Maps Directions API. Then you can use the PolyLine Overview from the outputted result to find every point along the route. The PolyLine is compressed and needs to be decoded to find the longitude and latitude of the points along the route.

This is detailed here: https://developers.google.com/maps/documentation/utilities/polylinealgorithm

You can then use the points along the line/route to say if GPS marker is within the region then the bus is enroute, if outside the bus is off. You may need to use Google's Snap to road API to ensure GPS points are accurate.

I hope this may help someone else.

EDIT: To clarify, use the Google Maps Directions API to create a route, this will give you a "PolyLine Overview". This gives you every single point along the route, which can be seen here. Using these points you can create a bounding box between each point. Then you can calculate if a point on the route by if it is withinside a bounding box.

It is important to note that PolyLine Overviews are encoded, as mentioned in my previous answer. Google has made a tool to demonstrate this decoding here. In Javascript I believe there is an inbuilt library command to encode and decode.

Hope this is clearer.

samtrod
  • 271
  • 2
  • 13
  • Hi, I need to achieve the same in my application. Can you please help me? – RaGu Feb 13 '18 at 10:46
  • I can try, what part you stuck on currently? – samtrod Feb 15 '18 at 21:17
  • I have created sample pen. Please check this link https://codepen.io/ragunathan/full/xYPPdY/ . If you click the route within way points then I need to show blue triangle, if not red circle. It works fine for the polyline. But that is not exact route – RaGu Feb 16 '18 at 06:09
  • Please refer this https://stackoverflow.com/questions/48821111/how-to-track-a-bus-is-on-route-using-google-maps – RaGu Feb 16 '18 at 06:28
  • Okay, my knowledge of JavaScript isn't very strong, I was intending on implementing the solution primarily in C#. I'll edit my above question to explain it more clearly. – samtrod Feb 16 '18 at 11:45