Google Maps Reverse Geocoder

This reverse geo-coder will convert a point on a map to an address. In practice this means the nearest available address.

...Loading...

Feedback

Instructions

Javascript Used


var geocoder;
var map;

function initialize()
{
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(54.55, -5.953);
var myOptions = {zoom: 11,center: latlng,mapTypeId:google.maps.MapTypeId.HYBRID,draggableCursor:'crosshair'}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click',function(me){
codeLatLng(me.latLng);
});
}
function codeLatLng(latlng)
{
if (geocoder)
{
geocoder.geocode({'latLng': latlng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
if (results[1])
{
document.getElementById("address").innerHTML=results[1].formatted_address;
}
else
{
document.getElementById("message").innerHTML="Geocoder failed due to: " + status;
document.getElementById("address").innerHTML="";
}
}
});
}
}

Comments For This Page

Hi, If you want the country instead of the full address use &quot;place = response.Placemark[0]&quot; then &quot;place.address&quot; and this should return the country name. This is what is being used on <a href="http://www.daftlogic.com/sandbox-click-for-country.htm">Click For Country</a>.

By Daft Logic on 3rd May 2008

nice job there!

I'm wondering how difficult this will be to simply output the country...

When you zoom out of this version and click, it bugs out displaying several addresses

By Bitcloud on 23rd April 2008

Add Your Comment

There's no need to create an account or provide unnecessary details. Just your comment.