Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google maps API v3 how do I get LatLng with a given address?

If a user inputs an address, I want to convert to the equivalent LatLng.

I've read the documentation, and I think I can use the Geocoder class to do this, but can't figure out how to implement it.

Thanks for any help!

like image 788
kylex Avatar asked Oct 13 '10 18:10

kylex


People also ask

How do I find Latlng address?

There is a last trick to get Address from Lat-Long (Geo-coordinates). You can simply hit google-maps web service passing the Latitude and longitude. It is simply a GET-Method web-service.


2 Answers

There is a pretty good example on https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

To shorten it up a little:

geocoder = new google.maps.Geocoder();  function codeAddress() {      //In this case it gets the address from an element on the page, but obviously you  could just pass it to the method instead     var address = document.getElementById( 'address' ).value;      geocoder.geocode( { 'address' : address }, function( results, status ) {         if( status == google.maps.GeocoderStatus.OK ) {              //In this case it creates a marker, but you can get the lat and lng from the location.LatLng             map.setCenter( results[0].geometry.location );             var marker = new google.maps.Marker( {                 map     : map,                 position: results[0].geometry.location             } );         } else {             alert( 'Geocode was not successful for the following reason: ' + status );         }     } ); } 
like image 68
Amasuriel Avatar answered Oct 10 '22 23:10

Amasuriel


I don't think location.LatLng is working, however this works:

results[0].geometry.location.lat(), results[0].geometry.location.lng() 

Found it while exploring Get Lat Lon source code.

like image 24
Avinav Avatar answered Oct 10 '22 23:10

Avinav