How to Geo code an address

19 Apr

We are going to convert an address into latitude and longitude coordinates. So we are doing geo coding with Google API.

Demo           Download

Here is the complete source code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="<a></a>">
<meta http-equiv="<a>Content-Type</a>" content="<a>text/html; charset=utf-8</a>" />
<title>Untitled Document</title>
<script src="<a>jquery-1.7.js</a>"></script>
<script src="<a href="view-source:"></a>"></script>
<script type="<a>text/javascript</a>">
    $(document).ready(function () {

        // wire up button click
        $('#go').click(function () {
            // get the address the user entered
            var address = $('#address').val();
            if (address) {
                // use Google Maps API to geocode the address
                // set up the Geocoder object
                var geocoder = new google.maps.Geocoder();
                // return the coordinates
                geocoder.geocode({ 'address': address }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        if (results[0]) {
                            // print results
                        } else {
                            error('Google did not return any results.');

                    } else {
                        error("Reverse Geocoding failed due to: " + status);
            else {
                error('Please enter an address');


    // output lat and long
    function printLatLong(lat, long) {
        $('body').append('<p>Lat: ' + lat + '</p>');
        $('body').append('<p>Long: ' + long + '</p>');

    function error(msg) {


        <label for="<a>address</a>">Enter an Address:</label>
        <input type="<a>text</a>" id="<a>address</a>" />
        <input type="<a>button</a>" id="<a>go</a>" value="<a>Find Latitude And Longitude</a>"/>

Leave a comment

Posted by on April 19, 2012 in Geo Programming, JavaScript



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: