Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How retrieve latitude and longitude via Google Maps API?

How retrieve latitude and longitude via Google Maps API?

like image 540
Bored Avatar asked May 05 '10 03:05

Bored


People also ask

Is Google reverse geocoding API free?

Reminder: To use the Geocoding API, you must enable billing on each of your projects and include an API key with all API or SDK requests. The Geocoding API uses a pay-as-you-go pricing model.


3 Answers

With API v3

google.maps.event.addListener(map, 'click', function(event) {
   alert( 'Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng() );
});

Likewise you can retrieve when a marker is dragged dropped :

google.maps.event.addListener(marker, 'dragstart', function(event) {
   alert( 'Lat: ' + event.latLng.lat() + ' and Longitude is: ' + event.latLng.lng() );
});
like image 60
Adnan Baliwala Avatar answered Sep 21 '22 02:09

Adnan Baliwala


If you need to find out the latitude and longitude of an address using the Google Maps API, you need to use the Google Maps Geocoding Service:

var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();

var address = "1600 Amphitheatre Parkway, Mountain  View";
geocoder.getLatLng(address, function(point) {
         var latitude = point.y;
         var longitude = point.x;  

         // do something with the lat lng
    });

If you would like to get the latitude and longitude of a position clicked on your Google map, you can do this in the click event:

GEvent.addListener(map, "click", function(marker,point) {
        var latitude = point.y;
        var longitude = point.x;

        // do something with the lat/lng
    });
like image 42
RedBlueThing Avatar answered Sep 19 '22 02:09

RedBlueThing


I have found the result via the following method:

http://maps.googleapis.com/maps/api/geocode/json?address=woking&sensor=false

  console.log(response.results[0].geometry.viewport.northeast.lat);
  console.log(response.results[0].geometry.viewport.northeast.lng);
like image 39
m82amjad Avatar answered Sep 19 '22 02:09

m82amjad