这是我的示例
var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + area.getCenter().lat + "," + area.getCenter().lng + "&sensor=false";
$.getJSON(url, function (data) {
alert("inside json");
//console.log(data);
$.each(data.results[0], function (i, inside) {
console.log(i);
});
//var addr = data.results[0].geometry.location.lat;
});
现在我意识到调用函数根本没有激活....我在这里阅读其他帖子并且他们说当JSON不是有效格式时会发生这种情况但是在这里我从Google API获得JSON所以我希望它的格式有效.... JSON就在这里
http://maps.googleapis.com/maps/api/geocode/json?latlng=27.88,78.08&sensor=false
你可以对此发表评论吗?或者我可能犯了一些错误答案 0 :(得分:1)
也许尝试完整的ajax调用:
$.ajax({
url: "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + area.getCenter().lat + "," + area.getCenter().lng + "&sensor=false",
data: {},
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "JSON",
timeout: 10000,
success: function (Result) {
for (var i = 0; i < Result.d.length; i++) {
element = Result.d[i];
console.log(element);
};
},
error: function (xhr, status) {
alert(status + " - " + xhr.responseText);
}
});
答案 1 :(得分:0)
可以将其添加到
$(document).ready(function(){
});
示例:
$(document).ready(function () {
var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + area.getCenter().lat + "," + area.getCenter().lng + "&sensor=false";
$.getJSON(url, function (data) {
alert("inside json");
//console.log(data);
$.each(data.results[0], function (i, inside) {
console.log(i);
});
//var addr = data.results[0].geometry.location.lat;
});
});