Skip to content

Instantly share code, notes, and snippets.

@dmshvetsov
Created September 1, 2013 12:21
Show Gist options
  • Save dmshvetsov/6404126 to your computer and use it in GitHub Desktop.
Save dmshvetsov/6404126 to your computer and use it in GitHub Desktop.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var coord = new google.maps.LatLng(latitude,longitude);
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': coord}, function(results, status) {
for (var i = 0; i < results[4].address_components.length; i++) {
for (var j = 0; j < results[4].address_components[i].types.length; j++) {
if(results[4].address_components[i].types[j] == 'locality') {
var city_name = results[4].address_components[i].long_name;
alert(city_name);
}
}
}
});
});
} else {
alert("Geolocation API is not supported in your browser.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment