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

Wednesday, May 14, 2008

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

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

NOTE: remember to include the need to take a free Google API key: See How to put Google Map into their Web about how to obtain the API key. This will be used for the inclusion of the script, in the form:

HTML:
  1. "http://www.google.com/jsapi?key=ABCDEFG" type = "text/javascript" >< / script> <script "http://www.google.com/jsapi?key=ABCDEFG" src = 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 really easy, as most of the API provided by Google:

JavaScript:
  1. / / Create the object GClientGeocoder
  2. new GClientGeocoder ( ) ; var geocoder = new GClientGeocoder ();
  3. / / The address is just a text, could be just a city
  4. 'via salaria, roma, italia' ; 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, also shows how to perform the reverse procedure, 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. "Latitudine = " + point. lat ( ) + " Longitudine = " + point. lng ( ) ; myHTML var = "Latitude =" + point. lat () + "Longitude =" + point. lng ();
  4. point , myHtml ) ; map. openInfoWindow (point, myHTML);
  5. )
  6. )

Related Post

Was this article helpful?: Per nientePocoAbbastanzaMoltoMoltissimo
Loading ... Loading ...

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

  1. getAvatar 1.0
  2. getAvatar 1.0
    June 17, 2008 k8:

    Excellent thanks!

  3. getAvatar 1.0
    03 lug, 2008 CED:

    goods!
    pour ceux qui développe en local, vous pouvez utiliser http://localhost cle pour la demande de google

  4. getAvatar 1.0
    July 14, 2008 Michele

    thanks for the help.

  5. getAvatar 1.0
    April 11, 2009 Mark

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

  6. getAvatar 1.0
    December 30, 2009 Orange:

    Thanks I was looking for exactly

Leave a comment

TAG XHTML PERMISSIONS: <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 [as][/as]           // Actionscript [css][/css]         // CSS Style Sheet [html][/html]       // HTML [js][/js]           // Javascript [objc][/objc]       // Objective-C [php][/php]         // PHP [sql][/sql]         // SQL