所以我使用谷歌地图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。我需要能够从地理编码外部访问该位置。提前谢谢!
答案 0 :(得分: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);
});