Thursday, September 20, 2007

Google Maps Geo Coding Example

Geocoding is the process of converting addresses (like "Bangalore") into geographic coordinates (like latitude 12.97xxxxx and longitude 77.f9xxxx), which you can use to place markers or position the map. The Google Maps API includes a Geocoding service that can be accessed directly via an HTTP request or by using a GClientGeocoder object.

Note that geocoding is a time and resource intensive task. Whenever possible, pre-geocode your addresses (using the HTTP geocoder or other geocoding service), and store your results.

Getting the Geo Code using Google Api For a Particular Geo Address using

::class GClientGeocoder::

This class is used to communicate directly with Google servers to obtain geocodes for user specified addresses. In addition, a geocoder maintains its own cache of addresses, which allows repeated queries to be answered without a round trip to the server.

The Following Example shows How to echo/alert the Longitude and Latitude of a Particular location

var geocoder = null;

function initialize() {
if (GBrowserIsCompatible()) {
geocoder = new GClientGeocoder();

function showLocationLatLon(addressget) {
var address = addressget;
geocoder.getLocations(address, echoLatLong);

function findLocation(address) {
document.forms[0].q.value = address;

echoLatLong(response) {
if (!response || response.Status.code != 200) {
alert("Sorry, we were unable to geocode that address");
} else {

place = response.Placemark[0];
point = new GLatLng(place.Point.coordinates[1],
alert("Address:: "+place.address);
alert("Lat/Lon "+place.Point.coordinates[1]+" "+place.Point.coordinates[0]);

The Above Example is self explanatory.


GClientGeocoder(cache?)Creates a new instance of a geocoder that talks directly to Googleservers. The optional cache parameter allows one to specify acustom client-side cache of known addresses. If none is specified,a GFactualGeocodeCache is used.(Since 2.55)

MethodsReturn ValueDescription
getLatLng(address, callback)noneSends a request to Google servers to geocode the specifiedaddress. If the address was successfully located, the user-specified callback function is invoked with a GLatLng point. Otherwise, the callback function is given a null point. In case of ambiguous addresses, only the point for the best match is passed to the callback function.(Since 2.55)
getLocations(address, callback)noneSends a request to Google servers to geocode the specified address. A reply that contains status code, and if successful, one or more Placemark objects, is passed to the user-specified callback function. Unlike the GClientGeocoder.getLatLng method, the callback function may determine the reasons for failure by examining the code value of the Status field.(Since 2.55)
getCache()GGeocodeCache Returns currently used geocode cache, or null, if no client-side caching is performed. (Since 2.55)
setCache(cache)noneSets a new client-side caching. If this method is invoked with cache set to null, client-side caching is disabled. Setting a new cache discards previously stored addresses. (Since 2.55)
setViewport(bounds)noneSets the geocoder to magnify geocoding results within or near the given viewport. The viewport is expressed as a GLatLngBounds rectangle. Note that setting a viewport does not restrict results to that bounding box, though it will elevate them in priority.(Since 2.82)
getViewport()GLatLngBounds Returns the viewport for magnifying geocoding results within that geocoder. The viewport is expressed as a GLatLngBounds rectangle.(Since 2.82)
setBaseCountryCode(countryCode)noneSets the geocoder to bias search results as if they were sent from the domain specified by the given ISO 3166-1 (alpha-2) country code. Geocoding is only supported for those countries in which Google Maps itself supports geocoding. Most ISO 3166-1 codes are identical to top-level Internet domain, with some notable exceptions. For example, "ES" refers to the top-level Internet domain for Spain: .es while "GB" refers to the top-level Internet domain for Great Britain: that the default domain is the domain from which you initially load the Maps API.Country codes are case insensitive.(Since 2.82)
getBaseCountryCode()StringReturns the current country code in use by the given geocoder. (If no country code is in effect, this method returns null.) (Since 2.82)
reset()none Resets the geocoder. In particular this method calls theGGeocodeCache.reset() method on the client-side cache, if one is used by this geocoder.(Since 2.55)

Example Of GeoCoding Using Google Map APi

var map = null;
var geocoder = null;

function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
geocoder = new GClientGeocoder();

function showAddress(address) {
if (geocoder) {
function(point) {

if (!point) {
alert("Sorry, we were unable to geocode that address");
} else {

map.setCenter(point, 13);
var marker = new GMarker(point);



More Info Here::

No comments :

Post a Comment