Google Maps API Tutorial© 2007, 2008, 2009 Mike Williams
|Translate this page:|
Snap point to streetIt is now possible (in countries that support directions) to snap points to the nearest street that's in the Google database.
This could be useful if you want to record a specific path by drawing a polyline that follows the streets.
The effect is achieved by requesting directions from the clicked point to itself. When the directions information returns, it will include a polyline with a single vertex. This polyline "follows" the street grid, and that vertex is the nearest point that lies on the street grid. So instead of plotting the point exactly where the user clicked, we can plot the point at the returned street vertex.
Here's a simple example. Click on the map and the marker will be added to the nearest known street.
In this example, the markers are draggable and snap to a street when the drag ends.
In this example, polylines following the streets are added between the snapped points.
In this example, the two previous features are combined. Polylines are redrawn when a marker is dragged.
Up to the start of this tutorial