Friday, March 27, 2009

Geocoding on Google Maps

Geocoding is the process of converting from address into geographic coordinates which you then can position on the map. It can be accessed directly via an HTTP request or by using a GClientGeocoder object. It is worthwhile to mention two methods of GClientGeocoder object.

1. GClientGeocoder.getLatLng(): this method allows you to get location of a specific location by querying google geoserver. You need to pass two arguments, address and a callback function. That callback function is invoked with one parameter, a GLatLng object of that location, when there is a reply from this service. See example here.

2. GClientGeocoder.getLocations(): this method allows you more ability than the previous one by returning a JSON object consisting of the following information: Status (request, code), Placemark (address, AddressDetails, Accuracy, Point, coordinates,...). Moreover, it supports both standard and reverse geocoding. If you pass this method a GLatLng object instead of a String address, the geocoder will perform a reverse lookup and return a structured JSON object of the closest addressable location. See examples:

Recently, I realized Google has added another information of data returned by this method, ExtendedData which consists of north, east, south, and west of that area. This is quite useful when positioning location on the map with appropriate zoom level. All you have to do, construct a GLatLngBounds object with these data, then call getBoundsZoomLevel() by passing this GLatLngBounds object to determine proper zoom level that fits map view port.

var getLocation = function(address) {
   var geoCoder = new GClientGeocoder();
         document.getElementById('txtAddressId').value = address;

         //if this address is found
         if(location.Status.code == 200) {
            var placeMark = location.Placemark[0];
            var latLngBox = placeMark.ExtendedData.LatLonBox;

            //default zoom level is 7
            var latlng, zoom = 7;
            if(latLngBox) {
            //determine zoom level if possible
               var latLngBound = new GLatLngBounds(new GLatLng(latLngBox.south, latLngBox.west), new GLatLng(latLngBox.north, latLngBox.east));
               zoom = map.getBoundsZoomLevel(latLngBound);

            latlng = new GLatLng(placeMark.Point.coordinates[1], placeMark.Point.coordinates[0]);
            map.setCenter(latlng, zoom);
         } else {
            alert('Address is not found.');


cecil said...

Thanks Man,

your code helped me to accomplish my task

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Tahsin Hasan said...

how can I pass an additional parameter to getLocation()?

chamnap said...

just define it this way:

function getLocation(address, p1, p2) {
// the rest of the code

JavaScript function is quite dynamic. You can define 3 parameters in a function and you could pass only one or two up to you.


Subscribe in a Reader