我正试图从city
中获取Geolocation API
。我刚开始学习JS和jQuery,所以这可能是一个基本的错误。
$(document).ready(function(){
function displayLocation(latitude,longitude){
var request = new XMLHttpRequest();
var method = 'GET';
var url =
'http://maps.googleapis.com/maps/api/geocode/json?latlng='
+ latitude + ',' + longitude + '&sensor=true';
var async = true;
request.open(method, url, async);
request.onreadystatechange = function(){
if(request.readyState == 4 && request.status == 200){
var data = JSON.parse(request.responseText);
var address = data.results[0];
alert(address.city.short_name);
}
};
request.send();
};
var successCallback = function(position){
var x = position.coords.latitude;
var y = position.coords.longitude;
displayLocation(x,y);
};
navigator.geolocation.getCurrentPosition(successCallback);
});
答案 0 :(得分:1)
这应该有帮助
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
function displayLocation(latitude,longitude){
var request = new XMLHttpRequest();
var method = 'GET';
var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
var async = true;
request.open(method, url, async);
request.onreadystatechange = function(){
if(request.readyState == 4 && request.status == 200){
var data = JSON.parse(request.responseText);
alert(request.responseText); // check under which type your city is stored, later comment this line
var addressComponents = data.results[0].address_components;
for(i=0;i<addressComponents.length;i++){
var types = addressComponents[i].types
//alert(types);
if(types=="locality,political"){
alert(addressComponents[i].long_name); // this should be your city, depending on where you are
}
}
//alert(address.city.short_name);
}
};
request.send();
};
var successCallback = function(position){
var x = position.coords.latitude;
var y = position.coords.longitude;
displayLocation(x,y);
};
navigator.geolocation.getCurrentPosition(successCallback);
});
</script>