【发布时间】:2014-03-29 14:06:42
【问题描述】:
我有 2 个函数,函数 A 调用函数 B,它对地址进行地理编码并将 LatLng 对象返回给函数 A,但不知何故,函数 A 不等待函数 B 从 Google 返回结果。
function A() {
var address = document.getElementById("txtbox").value;
//geocoding here
var here = B(address);
if (here == null) {
console.log("seems like geocode didn't work, defaulting value");
在函数B中
function B(address) {
var here;
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
console.log("geocoding");
if (status == google.maps.GeocoderStatus.OK) {
console.log(results[0].geometry.location);
currentlatlng = results[0].geometry.location;
lng = currentlatlng.lng();
lat = currentlatlng.lat();
here = new google.maps.LatLng(lat, lng);
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
});
return here;
}
但似乎控制台的输出是
seems like geocode didn't work, defaulting value
geocoding
因此,似乎函数 A 调用函数 B 然后继续执行它..
我以为它会等待,但根据我对 google maps api 如何工作的理解,它本身并没有“等待”,所以我该怎么办?
【问题讨论】:
-
为重复道歉,但我接受了有效的答案,谢谢@jfriend00
标签: javascript html function google-maps