Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps Can';t访问地理代码外的位置_Google Maps_Geocoding - Fatal编程技术网

Google maps Can';t访问地理代码外的位置

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; }

所以我使用谷歌地图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也会返回未定义。我需要能够从地理代码外部访问该位置。提前谢谢

地理编码是异步的。您需要在回调函数中使用返回的数据。下面的代码应该记录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);
});