Google Geolocator and Google Maps API and ZIP Code Lookup Demo

A very simple demonstration of how to get geolocation data for an address or ZIP code and pop it onto a Google map. This uses jQuery, and the Google Maps API.

Enter an address or ZIP code: 

JavaScript to retrieve the XML from a handler on the server

$(document).ready(function() {  
    $("#txtAddress").val("1600 Pennsylvania Ave, NW, Washington, DC");              
    initialize();  
  
    // This is the jQuery equivalent of <body onunload="GUnload();">  
    $(window).unload(function() {   
        GUnload();  
    });  
});  
  
var map = null;  
var geocoder = null;  
  
function initialize() {  
    if (GBrowserIsCompatible()) {  
        map = new GMap2($("#map_canvas")[0]);  
        geocoder = new GClientGeocoder();  
      
        $("#map_canvas").hide();  
    }  
}  
  
function showAddress() {  
    var address = $("#txtAddress").val();  
  
    if (geocoder)   
    {  
        geocoder.getLatLng(address, function(point) {  
            if (!point)   
            {  
                alert(address + " not found");  
            }   
            else   
            {  
                $("#map_canvas").show();  
  
                map.setCenter(point, 10);  
                var marker = new GMarker(point);  
                map.addOverlay(marker);  
                map.addControl(new GLargeMapControl());  
                map.addControl(new GMapTypeControl());  
  
                $("#divLatLong").empty();  
                $("#divLatLong").append("<strong>Latitude: " + point.y + ", Longitude: " + point.x + "     " + point.toString() + "</strong>");  
            }  
        });  
    }  
}