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 为什么Google place AutocompleteService回调没有得到执行?_Google Maps_Google Maps Api 3_Google Places Api - Fatal编程技术网

Google maps 为什么Google place AutocompleteService回调没有得到执行?

Google maps 为什么Google place AutocompleteService回调没有得到执行?,google-maps,google-maps-api-3,google-places-api,Google Maps,Google Maps Api 3,Google Places Api,对于placeAutocompleteService()as,我有以下代码: var strAddr=''; 如果(address.value!=“”){ var service=new google.maps.places.AutocompleteService(); service.getQueryPredictions({input:address.value},函数(预测,状态){ console.log('1'); if(status==google.maps.places.Place

对于place
AutocompleteService()
as,我有以下代码:

var strAddr='';
如果(address.value!=“”){
var service=new google.maps.places.AutocompleteService();
service.getQueryPredictions({input:address.value},函数(预测,状态){
console.log('1');
if(status==google.maps.places.PlacesServiceStatus.OK){
console.log('原始响应:'+预测[0]。结构化格式。辅助文本);
strAddr=predictions[0]。结构化格式。辅助文本;
}
});
console.log('strAddr:'+strAddr);
console.log('2');
}
console.log('3');
我在控制台中得到如下响应:

  • 斯特拉德:
  • 二,
  • 三,
  • 一,
  • 原始响应://此处获取匹配的地址
但它应该是:

  • 一,
  • 原始响应://此处匹配地址>
  • 这里有匹配的地址
  • 二,
  • 三,

为什么回调没有按预期的顺序执行?

不能保证您得到这个结果

  • 一,
  • 原始响应://此处匹配地址>
  • 这里有匹配的地址
  • 二,
  • 三,

因为这(新的google.maps.places.AutocompleteService();)是异步调用,您的回调可以按任意顺序调用,
getQueryPredictions
函数是异步的(可能是通过AJAX实现的)。因此,您不能保证它会在紧接着它的代码之前完成

e、 g.调用getQueryPredictions可能需要0.5秒。因此,在这种情况下,这两行将能够立即执行:

console.log('strAddr: ' + strAddr);
console.log('2');
在等待getQueryPredictions响应的回调函数之外发生的任何代码中,都不能依赖
strAddr