Понимая, что этот вопрос устарел, я надеюсь, что это может помочь кому-то в будущем.Это было мое первое знакомство с javascript и особенно со всем этим JSON-материалом, и поэтому я сильно ударился головой о стол, пытаясь понять, что я делаю не так.
Вот мое решение, которое определяет местонахождение клиентов(в широте и lng), а затем использует API геокодирования Google для определения их местоположения в «удобочитаемом» формате.
function currentLocation() {
navigator.geolocation.getCurrentPosition(foundLocation, errorLocation);
function foundLocation(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
//This is where the geocoding starts
var locationURL= "http://maps.googleapis.com/maps/api/geocode/json?latlng="
+ lat + "," + lng + "&sensor=false&callback=myLocation"
//This line allows us to get the return object in a JSON
//instead of a JSONP object and therefore solving our problem
$.getJSON(locationURL, myLocation);
}
function errorLocation() {
alert("Error finding your location.");
}
}
function myLocation(locationReturned) {
var town = locationReturned.results[0].address_components[1].short_name;
var state = locationReturned.results[0].address_components[4].short_name;
console.log(town, state);
}