Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine if a particular address is along a route within x miles?

Tags:

google-maps

Is there a way to determine if a particular address is along a route within x miles? Is there support for this in the Google maps API?

I have a database of addresses and I am trying to figure out which locations lie along a given route as determined by the Google Maps API.

like image 323
Arsalan Ahmed Avatar asked Nov 05 '22 17:11

Arsalan Ahmed


2 Answers

You can set the getPolyline option, on the GDirectionsOptions optional parameter, to the GDirections load request. This will get you the polyline data for the route.

Once you have this data you can iterate over each point in the polyline and determine the distance to each of your own datapoints (you can use the GLatLng distanceFrom method to calculate the distance).

Once you have the shortest distance to your route for each of your data points, you can and work out, based on some tolerance, if the point lies on the route.

Edit:

Although it is fine to call the GLatLng distanceFrom method repeatedly (it is just a utility method to get the distance between two points), I realized my answer simplifies the problem. To get an accurate distance from the route, you will need to determine the distance from the polyline between the closest two points (not just the distance from each point).

like image 161
RedBlueThing Avatar answered Nov 15 '22 13:11

RedBlueThing


Bill Chadwick has "Distance of Point to Polyline or Polygon" code at the bottom of this page, which could prove useful.

like image 39
Mike Williams Avatar answered Nov 15 '22 12:11

Mike Williams