Javascript 在Google Map api中获取距离时出现问题
我尝试使用google maps api获取两点之间的距离Javascript 在Google Map api中获取距离时出现问题,javascript,google-maps-api-2,Javascript,Google Maps Api 2,我尝试使用google maps api获取两点之间的距离 function load_points(){ var geocoder = new GClientGeocoder(); var firstZip, secondZip; geocoder.getLatLng(document.getElementById("firstZip").value, function(point) { if (!point) {
function load_points(){
var geocoder = new GClientGeocoder();
var firstZip, secondZip;
geocoder.getLatLng(document.getElementById("firstZip").value, function(point) {
if (!point) {
alert("This point could not be geocoded");
} else {
firstZip = point;
var marker = new GMarker(point);
map.addOverlay(marker);
}
});
geocoder.getLatLng(document.getElementById("secondZip").value, function(point) {
if (!point) {
alert("This point could not be geocoded");
} else {
secondZip = point;
var marker = new GMarker(point);
map.addOverlay(marker);
}
});
alert(new GLatLng(firstZip).distanceFrom(new GLatLng(secondZip)));
}
问题是,当我尝试时,似乎首先执行警报,然后执行地理编码部分,当然,distanceFrom方法失败,因为firstZip和secondZip的值未定义。有人知道如何解决这个问题。函数是异步的,所以在尝试使用
firstZip
和secondZip
变量之前,您需要等待对它的两个调用都成功返回。谢谢,但是如何做,如何知道返回了响应?