Google maps Can';t访问地理代码外的位置
所以我使用谷歌地图API,我想得到地理代码的定位结果。这就是我目前得到的:Google maps Can';t访问地理代码外的位置,google-maps,geocoding,Google Maps,Geocoding,所以我使用谷歌地图API,我想得到地理代码的定位结果。这就是我目前得到的: var pinProperties = {}; geocoder.geocode( {"address": address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { pinProperties.markerPos = results[0].geometry.location; }
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也会返回未定义。我需要能够从地理代码外部访问该位置。提前谢谢 地理编码是异步的。您需要在回调函数中使用返回的数据。下面的代码应该记录pinProperties.markerPos的值(除非操作状态不是“OK”)
谢谢,所以我可以澄清一下,我没有办法在回调函数之外使用pinProperties.markerPos吗?你可以,但你必须知道它是可用的。知道这一点的最简单方法是在回调函数运行时可用。
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);
});