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);
	}
});

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


Facebook  Twitter      Stumbleupon  Delicious
  

6 Comments on “Google Map Geocoding Tutorial with Example”

  • mackdk wrote on 1 July, 2009, 20:55

    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

  • Viral Patel wrote on 1 July, 2009, 20:58

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

  • jeet lal patel wrote on 29 July, 2009, 11:30

    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

  • Noman wrote on 17 August, 2009, 11:56

    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….

  • Viral Patel wrote on 17 August, 2009, 13:08

    @Noman, Thanks for the comment :)

  • harry_tegar wrote on 31 December, 2009, 11:53

    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

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 ViralPatel.net. All rights reserved.