Created
August 1, 2011 20:55
-
-
Save thinkt4nk/1118979 to your computer and use it in GitHub Desktop.
Reverse Geocode using google.maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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