问题描述
我有一个 array
,其中包含来自 Google Geocode 和 Places JavaScript API 的位置结果。
Place 结果中此数组中的任何元素都包含 place_id
值,我想利用它来获取有关该地点的更多基本信息。
初始数组 (uniqueGeoRes
) 最多可包含 7 个结果。
我对 Places API 服务和回调以及如何在已检查 uniqueGeoRes
的所有元素以获取其他地方数据时实现回调感到困惑。
例如,如果 uniqueGeoRes
的所有 7 个元素都有一个 place_id
,我需要使用 Places Service 来获取每个元素的附加信息。
执行此操作的循环在 Places 回调完成之前完成,因为外部函数不是异步的。
我认为承诺可能会有所帮助,但我仅限于 ES3。这是我到目前为止所拥有的。
这不会给出预期的结果,因为当回调完成时,值 i
已经满足 q
的值,即使在第一次回调时也是如此。
我完全迷失了自己!
在对新填充的 addrOutput
数组执行任何操作之前,如何确保 Places 回调完成?
var q;
q = 7;
// Set the size of the results array (q is hardcoded maximum value)
if (uniqueGeoRes.length < q) {
q = uniqueGeoRes.length;
}
uniqueGeoRes.length = q;
var addrOutput = [];
//Check for additional Places data
function updatePlace(arr,callback1){
console.log('asyncFunction has been called');
console.log('processing: ',arr);
for (var i = 0; i < q; i++) { // each element in the uniqueGeoRes array
if (arr[i].source = 'Places API') {
// Append extra information from the Place look up on individual place_id
console.log('Getting additional information for PLACES result: ' + arr[i].place_id);
var request = { placeId: uniqueGeoRes[i].place_id,fields: ['address_component','formatted_address','geometry','name'] };
service.getDetails(request,callback);
function callback(place,status) { // Get the additional place information.
if (status == google.maps.places.PlacesServiceStatus.OK) {
addrOutput.push(place);
console.log ('Loop is ' + i + '/' + q + '. addrOutput length is now: ' + addrOutput.length);
if (i === q) {
callback1()
}
} else {
console.warn ('Place Service Status is bad.');
}
}
}
}
};
// Call when asyncFunction completes
function callback1(error) {
if (!error) console.log('asyncFunction is complete');
}
updatePlace(uniqueGeoRes,callback1);
这是当前的输出
[Log] asyncFunction has been called (development,line 817)
[Log] processing: – [Object,Object,…] (7) (development,line 818)
[Object,Object]Array (7)
[Log] Getting additional information for PLACES result: ChIJwSc0D-zih0gR2uOIFj2UCv8 (development,line 824)
[Log] Getting additional information for PLACES result: ChIJr_4dcTH9h0gRl1OqMr4OSec (development,line 824)
[Log] Getting additional information for PLACES result: ChIJ9wqvVQ7jh0gRPYUz575U0Bs (development,line 824)
[Log] Getting additional information for PLACES result: ChIJBw3rDLXih0gRivw-FONRgZU (development,line 824)
[Log] Getting additional information for PLACES result: ChIJ774cdbXih0gRwWCg-MxTKjs (development,line 824)
[Log] Getting additional information for PLACES result: ChIJwQnvs5Pih0gR125FEtKwli0 (development,line 824)
[Log] Getting additional information for PLACES result: ChIJXyhtsZPih0gRIZgUPLvv_Ws (development,line 824)
[Log] Loop is 7/7. addrOutput length is now: 1 (development,line 835)
[Log] asyncFunction is complete (development,line 854)
[Log] Loop is 7/7. addrOutput length is now: 2 (development,line 854)
[Log] Loop is 7/7. addrOutput length is now: 3 (development,line 854)
[Log] Loop is 7/7. addrOutput length is now: 4 (development,line 854)
[Log] Loop is 7/7. addrOutput length is now: 5 (development,line 854)
[Log] Loop is 7/7. addrOutput length is now: 6 (development,line 854)
[Log] Loop is 7/7. addrOutput length is now: 7 (development,line 854)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)