Reverse Geocoding

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam sed mattis leo. Nullam lobortis consequat libero ut consectetur. Donec pellentesque vulputate nunc, vitae elementum ipsum feugiat sed. Integer sagittis pretium ipsum, semper bibendum velit vehicula eu. Sed adipiscing, neque ac pulvinar viverra, ipsum mauris sagittis mi, eget ultrices diam erat ac enim. Pellentesque elit lacus, interdum eget tempor eget, dapibus nec sem. Fusce tristique.

The term geocoding generally refers to translating a human-readable address into a location on a map. The process of doing the converse, translating a location on the map into a human-readable address, is known as reverse geocoding.

The Geocoder supports reverse geocoding directly. Instead of supplying a textual address, supply a comma-separated latitude/longitude pair in the latLng parameter.

The following example geocodes a latitude/longitude value and centers the map at that location, bringing up an info window with the formatted address. We return the second result, which is less specific than the first (in this case, a neighborhood name):

results[0].formatted_address: "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
results[1].formatted_address: "Williamsburg, NY, USA",
results[2].formatted_address: "New York 11211, USA",
results[3].formatted_address: "Kings, New York, USA",
results[4].formatted_address: "Brooklyn, New York, USA",
results[5].formatted_address: "New York, New York, USA",
results[6].formatted_address: "New York, USA",
results[7].formatted_address: "United States"

Note that in the previous example we showed the second result (by selecting results[1]. The reverse geocoder often returns more than one result. Geocoding "addresses" are not just postal addresses, but any way to geographically name a location. For example, when geocoding a point in the city of Chicago, the geocoded point may be labeled as a street address, as the city (Chicago), as its state (Illinois) or as a country (The United States). All are addresses to the geocoder. The reverse geocoder returns all of these results.

The reverse geocoder matches political entities (countries, provinces, cities and neighborhoods), street addresses, and postal codes.

The full list of addresses returned by the previous query are shown below.

results[0].formatted_address: "275-291 Bedford Ave, Brooklyn, NY 11211, USA",
results[1].formatted_address: "Williamsburg, NY, USA",
results[2].formatted_address: "New York 11211, USA",
results[3].formatted_address: "Kings, New York, USA",
results[4].formatted_address: "Brooklyn, New York, USA",
results[5].formatted_address: "New York, New York, USA",
results[6].formatted_address: "New York, USA",
results[7].formatted_address: "United States"

Addresses are returned in the order of best to least matches. Generally, the more exact address is the most prominent result, as it is in this case. Note that we return different types of addresses, from the most specific street address to less specific political entities such as neighborhoods, cities, counties, states, etc. If you wish to match a more general address, you may wish to inspect the results[].types field.

NOTE:Reverse geocoding is not an exact science. The geocoder will attempt to find the closest addressable location within a certain tolerance.

Set up the variables on page load

var infowindow = new google.maps.InfoWindow();
$(document).ready(function(){
    var geocoder;
    var map;
    initialize();
})

Initialize Function

function initialize(){
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(38.898748, -77.037684);
    var myOptions = {
        zoom: 10,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
}

Reverse Geocoding Function

function codeLatLng(){
    //get the input from the textbox
    var input = document.getElementById('latlng').value;
    //convert the var to an array
    var latlngstring = input.split(",",2);
    //create two new variables for lat and lng
    var lat = parseFloat(latlngstring[0]);
    var lng = parseFloat(latlngstring[1]);
    //
    var latlng = new google.maps.LatLng(lat, lng);
    geocoder.geocode({'latLng': latlng}, function(results, status){
        //check to see if the status is ok
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                map.setZoom(11);
                marker = new google.maps.Marker({
                    position: latlng,
                    map: map
                });
                infowindow.setContent(results[1].formatted_address);
                infowindow.open(map, marker);
            } else {
                alert('no results found');
            }
        } else {
            alert('Geocoder failed due to: ' + status);
        }
    });
}