Skip to content

Instantly share code, notes, and snippets.

@turbod
Created March 18, 2014 13:11
Show Gist options
  • Save turbod/9619706 to your computer and use it in GitHub Desktop.
Save turbod/9619706 to your computer and use it in GitHub Desktop.
Get city name by location
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