最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps V3 snap to nearest Street - Stack Overflow

programmeradmin1浏览0评论

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

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?

Share Improve this question edited Dec 22, 2011 at 14:12 John Conde 220k99 gold badges462 silver badges501 bronze badges asked Jan 26, 2011 at 12:22 PeterPeter 1511 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

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!"
          });
      }
    });
});

many thanks Revolution42 it works quite nice

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map_canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="http://maps.googleapis.com/maps/api/js?key=YOU API KEY&sensor=false">
    </script>
    <script type="text/javascript">
      function initialize() {
        var myOptions = {
          center: new google.maps.LatLng(-34.397, 150.644),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            myOptions);


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!"
          });
      }
    });
});


}



    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>
发布评论

评论列表(0)

  1. 暂无评论