tags:

views:

45

answers:

2

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.

+1  A: 

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

Cannonade
I think you mean distanceFrom rather than getDistance. getDistance is a GDirections Method, and you don't want to do a GDirections call for every vertex on the polyline because that would be very slow and quickly eat into your GDirections quota.
Mike Williams
Nope. I am talking about *GLatLng* *getDistance* which is a utility method to check the distance between two geo points.
Cannonade
Oops ... I just realized the cause of confusion. I meant the *distanceFrom* method on GLatLng.
Cannonade
+1  A: 

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

Mike Williams