Created
March 18, 2014 13:11
-
-
Save turbod/9619706 to your computer and use it in GitHub Desktop.
Get city name by location
This file contains hidden or 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 errorLocation(msg) { | |
console.log(msg); | |
} | |
function successLocation(position) { | |
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); | |
var geocoder = new google.maps.Geocoder(); | |
geocoder.geocode({'latLng': latlng}, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
var result = results[0]; | |
//look for locality tag and administrative_area_level_1 | |
var city = ""; | |
for(var i=0, len=result.address_components.length; i<len; i++) { | |
var ac = result.address_components[i]; | |
if(ac.types.indexOf("locality") >= 0) city = ac.long_name; | |
} | |
//only report if we got Good Stuff | |
if(city != '' && state != '') { | |
$('#location-input').val(city); | |
} | |
} | |
}); | |
} | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(successLocation, errorLocation); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment