Skip to content

Instantly share code, notes, and snippets.

@thinkt4nk
Created August 1, 2011 20:55
Show Gist options
  • Save thinkt4nk/1118979 to your computer and use it in GitHub Desktop.
Save thinkt4nk/1118979 to your computer and use it in GitHub Desktop.
Reverse Geocode using google.maps
var inArray = function(needle,haystack) {
for(i=0;i<haystack.length;i++) {
if( needle === haystack[i] ) {
return true;
}
}
}
var reverseGeocode = function(options)
{
if( typeof(options.lat) !== 'undefined' && typeof(options.lng) !== 'undefined' && typeof(options.onReverseGeocode) === 'function' )
{
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(options.lat,options.lng);
geocoder.geocode({location : latlng},function(result,status) {
if( status === 'success' )
{
$(result).each(function() {
(function(address) {
if( inArray('locality',address.types) && inArray('political',address.types) )
{
options.onReverseGeocode(address.formatted_address);
}
})( this );
});
}
});
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment