开发者

How do i get the lat and lng of google map?

Hi i have the following code and i was wondering if i can get the lat and lng of the start point and of the end point.

here's the code

  <script type='text/jav开发者_如何学JAVAascript'>



     var rendererOptions = {
        draggable: true
      };
      var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
      var directionsService = new google.maps.DirectionsService();
      var map;
      function initialize() {
      var myOptions = {
          zoom: 7,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        directionsDisplay.setMap(map);  
        calcRoute();
      }

      function calcRoute() {
     var request = {
          origin: "Craiova, Romania",
          destination: "Bucuresti, Romania",
          travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);

          }
        });
      }

      window.onload = function() {
           initialize();
        };
    </script>


The best way is to use the response:

directionsService.route(request, function(response, status) {
    var start = response.routes[0].overview_path[0]; // Craiova
    var end = response.routes[0].overview_path.slice(-1)[0]; // Bucharest
    // start.lat(); start.lon();
    // end.lat(); end.lon();
});

Another way is to use the geocoder:

geo = new google.maps.Geocoder();
geo.geocode({address: "Craiova, Romania"}, function(res) {
    var location = res[0].geometry.location;
    // location.lat(); location.lon();
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜