Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps V3 snap to nearest Street

When I click into a map I want the marker to get snaped to the nearest street. There is a good exmaple here: http://econym.org.uk/gmap/example_snappath.htm (Main Page:http://econym.org.uk/gmap/snap.htm). However - this example is for google maps version 2. Is there a way to do this in v3?

like image 399
Peter Avatar asked Jan 26 '11 12:01

Peter


1 Answers

The example that you have given gets the result by doing a direction lookup and then getting the first location from it. In api v3 this is accomplished with the following code where "map" is the name of your map

var directionsService = new google.maps.DirectionsService();

google.maps.event.addListener(map, 'click', function(event) {
    var request = {
        origin:event.latLng, 
        destination:event.latLng,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
          var marker = new google.maps.Marker({
             position: response.routes[0].legs[0].start_location, 
             map: map,
             title:"Hello World!"
          });
      }
    });
});
like image 100
Revolution42 Avatar answered Sep 23 '22 03:09

Revolution42