Google Map Geocoding Tutorial with Example

google-map-reverse-geocodingGoogle Map API has been a great way to show geographical information on web. A lot of mashup tools like this, have been created around Google Maps to show a wide variety of data. In my previous article about Introduction to Google Maps API, I had described basic APIs to integrate Google Map in your webpage. In this small article we will discuss a great feature of Google Maps API that can be used to locate any City/Country/Place on Map. This is called Geocoding. Google Maps API provides a wonderful API called Geocoding API that enables you to fetch any location and pin point it on Google Map. GClientGeocoder is the class that we use to get the geocoder that get us the location. We will use getLatLng() method to get latitude/longitude of any location. Check the following code.
var place = "New York"; geocoder = new GClientGeocoder(); geocoder.getLatLng(place, function(point) { if (!point) { alert(place + " not found"); } else { var info = "<h3>"+place+"</h3>Latitude: "+point.y+" Longitude:"+point.x; var marker = new GMarker(point); map.addOverlay(marker); marker.openInfoWindowHtml(info); } });
Code language: JavaScript (javascript)
In above code snippet we passed string “New York” and a handler function to getLatLng() method of GClientGeocoder. GClientGeocoder class will call google server for the location and when it gets the result, it pass the result to the handler function that we specified. Thus handler function will get point (GPoint) object from which we can get the latitude and longitude of location. In above code we have created a marker and placed it on the map.

Online Demo

Google Map Reverse Geocode Example
Get our Articles via Email. Enter your email address.

You may also like...

20 Comments

  1. mackdk says:

    What you describe is geocoding not reverse geocoding

    Geocoding: process of finding associated geographic coordinates (often expressed as latitude and longitude) from other geographic data

    http://en.wikipedia.org/wiki/Geocoding

    Reverse geocoding: the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name

    http://en.wikipedia.org/wiki/Reverse_geocoding

  2. ops.. Thanks mackdk for pointing out the error. I will update the post and make it geocoding.

    • Check says:

      You might also want to edit the link to the example, it still says Reverse Geocoding.

  3. hello everybody
    i m creating a module of map in which i want to display all city of the visible part of the map
    (i have database in which state_name city_name lat_of_city long_of_city)
    so i want to find current state when user drag the map.

    how to find when user drag the map?
    and then current location

    please help me

    thanks

  4. Noman says:

    ohhhhhhhhhhh realy thanx buddy u solve my problem… currently i am working on application where the Lat/Long is required i simple get idea from ur site and find the solution… realy big help for me thanx keep it up….

  5. @Noman, Thanks for the comment :)

  6. harry_tegar says:

    Hi Viral Patel and everyone

    Do you have any basic example code of Reverse geocoding? If you do, may I have it? I’m kinda new to this Reverse Geocoding stuff.

    sincerely yours

    Harry

  7. stace says:

    I’m a total newbie and having trouble integrating your GeoCode code with the initialize function in your earlier tutorial. When a given page loads, I would like it to show the map for New York (or whatever is in that variable. Can you share a brief code example of how it would look?

    Thanks very much!

  8. Harshit says:

    how to get zipcode from the address?

  9. nileema says:

    hello every one
    i need geocoded database for pune region.how to get it

  10. paresh says:

    i tried this code
    and also set permission in manifest.xml internet,access_fine_location,access_coarse_location

    but this project could not get location name from getFromLocation().
    getFromLocation(lat.long,1) always return null

    please kindly read this and help me

  11. subbu says:

    i need to create an application in asp web pages….(i.e,)using latitude and longitude…pls help me…..i want to show my project within this week to my PM…….pls its my humble request

  12. jaroslav says:

    I want embed a functionality to “search various place over the world” in my Google Earth API application. When I am using the code displayed in your article ( “GClientGeocoder” object and “getLatLng() method, I am not successful. Can you give me more links for study or more examples How to use it in Google Earth API ? Thanks

    Jaroslav

  13. milan says:

    how to get distance in reverse geo coding?
    such as 0.5554 km from sg highway, ahmedabad. or
    1.575 km from vastrapur circle, ahmedabad….

  14. Payal Shah says:

    i want to search more than one cities at a time like if i write in textbox Pune Mumbai then it should mark both…is it possible..?

  15. Mohit says:

    hlo sir,
    i am using this code, i didn’t get the actual lat,lng.please hlp me

  16. Arpit says:

    I want a code like by entering a zipcode it gaves me a list of nearest branch from my database.

  17. Saurabh says:

    • Using this application you can search locality/city on the basis of Pin Code/STD Codes in India as well as perform reverse searching by submitting the pin code/ STD Code and retrieving the corresponding area or city.can you provide me solution sir plz

  18. Dinesh Krishnan says:

    Very Good example, Thanks for sharing

  19. Rahul says:

    Hi All,
    Please anyone can provide a Reverse Geocoding example.
    I am working on it first time.
    Please can anyone help on it.

    Thanks in Advance

Leave a Reply

Your email address will not be published. Required fields are marked *