Google Maps: How to get Latitude and Longitude from an address

Also this time I reply to a comment with a post, given the general interest. Armando asked me if it was possible, using Google Maps, Latitude and Longitude obtained from a city name or street. The answer is yes! Using the Google Maps API geocoding service you can ask Google directly from reprocessing our textual information (state, city, etc.) in geographic coordinates. You can try an online example of documementazione Google directly on the site.

The main purpose behind this procedure is GClientGeocoder . . Using the method getLatLng() you can convert a string into an object address GLatLng .

NOTE: remember to include the need to request a free Google API key: see How to put Google Maps in your web on how to get the API key. This will be used for inclusion of the script, in the form:

1
"http://www.google.com/jsapi?key=ABCDEFG" type = "text/javascript" >< / script > < script src = "http://www.google.com/jsapi?key=ABCDEFG" type = "text / javascript"> </ script >

Where is 'ABCDEFG', should be replaced with the key that Google releases free: Get key Google .

The use of this object is very simple, like most of the APIs provided by Google:

1
2
3
4
5
6
7
8
9
10
11
12
13
/ / Create the object GClientGeocoder
new GClientGeocoder ( ) ; var geocoder = new GClientGeocoder ();
/ / The address is just a text, it could be just a city
'via salaria, roma, italia' ; var address = 'Via Salaria, Rome, Italy';
address , geocoder. getLatLng (address,
point ) { function (point) {
! point ) { if (point) {
address + ' non trovato' ) ; alert (address + 'not found');
{ Else {}
'Latitudine = ' + point. lat ( ) + ' Longitudine = ' + point. lng ( ) ) ; alert ('Latitude =' + points. lat () + 'Longitude =' + points. lng ());
}
}
);

For completeness, also shows how to perform the reverse process, at this point. Adding an event to our map, in fact, it is possible to know which are the values ​​of Latitude and Longitude:

1
2
3
4
5
6
map , "click" , GEvent. AddListener (map, "click"
overlay , point ) { function (overlay, point) {
"Latitudine = " + point. lat ( ) + " Longitudine = " + point. lng ( ) ; myHtml var = "Latitude =" + points. lat () + "Longitude =" + points. lng ();
point , myHtml ) ; map. openInfoWindow (point, myHtml);
}
);

9 comments to "Google Maps: How to get Latitude and Longitude from an address"

  1. June 17, 2008 k8 :

    Great, thanks!

  2. July 3, 2008 c and d:

    goods!
    pour ceux here Developpe en local, vous pouvez utiliser http://localhost pour la demande de cle google

  3. July 14, 2008 Michael:

    Thank you for your help.

  4. April 11, 2009 Mark:

    Even more simple and completely automatic:
    http://mygeoposition.com

  5. December 30, 2009 orange :

    Thanks I was looking for this

  6. May 5, 2010 How to get Latitude and Longitude in Objective-C | Undolog.com :

    [...] For example, you can use the service provided by Google Geocoding in Google Maps and discussed: how to get Latitude and Longitude from an address. Apple iPhone or iPad, however, you can overcome this obstacle by using so [...]

  7. July 29, 2010 Ghe:

    I state that I am a principainte.
    Mysite I'm trying to insert into the google map, getting latitude and longitude with the object GClientGeocoder.
    But I have not figured out how to do ....

    And 'possible to have a complete example starting from

    and ending at

    THANK YOU for the help!

  8. January 8, 2011 Mark Arzani :

    Thanks for the article.
    It was just what I was looking for.
    I was able to fully integrate this functionality into my site.

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA