Alex Marius
Alex Marius

Reputation: 37

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/javascript'>



     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>

Upvotes: 1

Views: 283

Answers (1)

Gabi Purcaru
Gabi Purcaru

Reputation: 31574

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();
});

Upvotes: 1

Related Questions