Google Maps: how to obtain Latitude and Longitude from an address

Wednesday, May 14, 2008

Even this time responding to a comment with a post, since the general interest. Armando asked me if it was possible, through Google Maps, get Latitude and longitude from a city name or a road. The answer is yes! Through the Google Maps API geocoding service can be requested directly Google to trasfromare our textual information (state, city, street) in geographical coordinates. You can try an example online directly on the site of documementazione Google.

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

NOTE: I remember that to include the Google API must ask for a key: See How to put Google Map in its Web about how to obtain the key API. This will be used for the inclusion of the script, in the form:

HTML:
  1. 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 easy, as most of the APIs made available by Google:

JavaScript:
  1. / / Create the object GClientGeocoder
  2. GClientGeocoder ( ) ; var geocoder = new GClientGeocoder ();
  3. / / Address is a text, it could also be only one city
  4. ; var address = 'via salaria, rome, italy';
  5. address, geocoder. getLatLng (address,
  6. point ) { function (point) (
  7. !point ) { if (! point) (
  8. address ' non trovato' ) ; alert (address 'not found');
  9. { ) Else (
  10. 'Latitudine = ' point. lat ( ) ' Longitudine = ' point. lng ( ) ) ; alert ( 'Latitude =' point. lat () 'Longitude =' point. lng ());
  11. )
  12. )
  13. );

For completeness monstro also how to perform the procedure reversed, at this point. By adding an event to our map, it is possible to know which are the values of Latitude and Longitude:

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

4 comments to:
"Google Maps: how to obtain Latitude and Longitude from an address"

  1. Wednesday, May 14, 2008 at 16:58
    Google Maps: how to obtain Latitude and Longitude from an address | FDS said:

    [...] Complete [...]

  2. Tuesday, June 17, 2008 at 16:14
    K8 said:

    Very good thank you!

  3. Thursday, July 03, 2008 at 13:35
    ced said:

    goods!
    pour ceux qui en developpe local, you can use http://localhost pour la demande de cle google

  4. Monday, July 14, 2008 at 18:56
    Michele said:

    thanks for the help.

Leave a comment

You can use the following TAG XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>