0

所以我正在使用谷歌地图 API,我想获得地理编码的位置结果。这就是我目前所拥有的:

var pinProperties = {};
geocoder.geocode( {"address": address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        pinProperties.markerPos = results[0].geometry.location;
    }
    else if (status === "ZERO_RESULTS") {
        pinProperties.markerPos = map.getCenter();
    }
    else {
        alert("Geocode was not successful for the following reason: " + status);
    }
});
console.log(pinProperties.markerPos);

但是,即使满足前两个条件之一,pinProperties.markerPos 也会返回 undefined。我需要能够从地理编码外部访问该位置。提前致谢!

4

1 回答 1

0

地理编码是异步的。您需要在回调函数中使用返回的数据。下面的代码应该记录 pinProperties.markerPos 的值(除非操作状态不是“OK”)

var pinProperties = {};
geocoder.geocode( {"address": address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        pinProperties.markerPos = results[0].geometry.location;
    }
    else if (status === "ZERO_RESULTS") {
        pinProperties.markerPos = map.getCenter();
    }
    else {
        alert("Geocode was not successful for the following reason: " + status);
    }
    console.log(pinProperties.markerPos);
});
于 2013-09-02T16:09:25.830 回答