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, through Google Maps, Latitude and Longitude obtain from a city name or street. The answer is yes! Using the Google Maps API geocoding service you can directly ask Google trasfromare our textual information (state, city, street) in geographical coordinates. You can try a sample online direct on the documementazione Google.

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

NOTE: remember to include the need to apply for a free Google API key: see How to add Google Map in your Web on how to get the API key. This will be used for the 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', it should be replaced with the key that Google releases free: Get key Google .

The use of this object is really simple, as most of the API 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 also 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 =' + point. lat () + 'Longitude =' + point. lng ());
}
}
);

For completeness, I also show how to perform the reverse process at this point. Adding an event to our map, in fact, it is possible to know what 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 =" + point. lat () + "Longitude =" + point. lng ();
point , myHtml ) ; map. openInfoWindow (point, myHtml);
}
);

9 comments to: " "

  1. June 17, 2008 k8 :

    Great thank you!

  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 Michele:

    Thank you for your help.

  4. April 11, 2009 Marco:

    Even more simple and fully automatic is:
    http://mygeoposition.com

  5. December 30, 2009 oranges :

    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 and discussed in Google Maps: how to get Latitude and Longitude from an address. On Apple iPhone, or iPad, you can however overcome this obstacle by using so [...]

  7. July 29, 2010 Ghe:

    I state that I am a Beginner_________.
    I'm trying to insert into mysite 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

    THANKS for the help!

  8. January 8, 2011 Marco Arzani :

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

Leave a comment

TAG XHTML PERMITS: CODE ENTRY:
 <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