javascriptweb-servicesgoogle-mapsgoogle-maps-api-3gps

Track if a Bus GPS point is on route - Google Maps API


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?


Solution

  • 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.