我相信我的地理编码器结果存在时间问题。请参阅下面的代码片段。
我基本上是在执行地理编码并获得结果。然后我通过 jQuery AJAX 调用将结果传递给服务器端方法。最后,该方法的结果返回一个 JSON 对象。根据结果,我可能会也可能不会执行第二次地理编码。这就是问题所在。
您可以看到我有一个变量hasResult
默认为 false,并在确定有效结果后切换为 true。在所有情况下,这都是完美的,除非需要进行第二次地理编码。地理编码成功发生并且代码执行,但最终hasResult
检查仍然返回 false。怎么会这样?
var hasResult = false;
geocoder.geocode({ "address": address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$.ajax({
url: "URL",
type: "POST",
dataType: "json",
data: results
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result.Valid) {
hasResult = false;
//Some other code, works perfect
} else {
geocoder.geocode({ "address": result.ValidRequest }, function (resultsNew, statusNew) {
if (statusNew == google.maps.GeocoderStatus.OK) {
hasResult = false;
//Some other code, works perfect
}
});
}
});
});
}
});
}
});
if (hasResult == true) {
alert('Success');
} else {
alert('Fail');
}
谢谢