Created
March 1, 2015 06:26
-
-
Save hexatridecimal/510c5309d7583c98a4ae to your computer and use it in GitHub Desktop.
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
function geocode(lat,lon) { | |
geocoder = new google.maps.Geocoder(); | |
var latlng = new google.maps.LatLng(lat,lon); | |
geocoder.geocode({'latLng': latlng}, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
if (results[0]) { | |
var address = "", city = "", state = "", zip = "", country = "", formattedAddress = ""; | |
var lat; | |
var lng; | |
for (var i = 0; i < results[0].address_components.length; i++) { | |
var addr = results[0].address_components[i]; | |
// check if this entry in address_components has a type of country | |
if (addr.types[0] == 'country') | |
country = addr.long_name; | |
else if (addr.types[0] == 'street_address') // address 1 | |
address = address + addr.long_name; | |
else if (addr.types[0] == 'establishment') | |
address = address + addr.long_name; | |
else if (addr.types[0] == 'route') // address 2 | |
address = address + addr.long_name; | |
else if (addr.types[0] == 'postal_code') // Zip | |
zip = addr.short_name; | |
else if (addr.types[0] == ['administrative_area_level_1']) // State | |
state = addr.long_name; | |
else if (addr.types[0] == ['locality']) // City | |
city = addr.long_name; | |
} | |
if (results[0].formatted_address != null) { | |
formattedAddress = results[0].formatted_address; | |
} | |
var location = results[0].geometry.location; | |
lat = location.lat(); | |
lng = location.lng(); | |
alert('City: '+ city + '\n' + 'State: '+ state + '\n' + 'Zip: '+ zip + '\n' + 'Formatted Address: '+ formattedAddress + '\n' + 'Lat: '+ lat + '\n' + 'Lng: '+ lng); | |
} else { | |
alert('Geocoder failed due to: ' + status); | |
} | |
} | |
}); | |
} | |
$(document).ready(function(){ | |
console.log(geocode(34.039429, -118.265616)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment