Google Maps API功能不会及时返回结果 [英] Google Maps API function does not return results in time

查看:133
本文介绍了Google Maps API功能不会及时返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个具有以下属性的对象数组: Address Latitude 经度标题

  poi (139 Main St,0,0,Farmer's Market),
poi(18 N Hopkins Ave,37.4455,-143.7728,基督教青年会),
poi(42 Universe St ,37.4855,-143.8781,原始比萨#32)

现在,第一个对象不有经纬度的任何设置,所以我希望使用谷歌的Geocode API来填补缺失的数据。我包含API脚本链接与我的API密钥,这是正常工作。

然后创建一个 for 循环通过数组并找到任何缺少数据的对象。它应该通过并用正确的信息替换任何空单元格。但是,尽管信息被检索,但是在循环完成之后这样做。我可以输出正确的信息,但是只能在函数返回空数据之后。

  for(i = 0; i < poiArray.length; i ++){
var curr = poiArray [i];

if(!curr.lat ||!curr.long){

geocoder = new google.maps.Geocoder();
geocoder.geocode({'address':curr.address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
cur.lat = results [0] .geometry.location.k;
cur.long = results [0] .geometry.location.B;
} else {
console.log('failed to include '+ curr.title);
poiArray.splice(i--,1);
}

});



$ b $ / code $ / pre>

尝试超时和嵌套函数,但似乎没有任何工作。有什么建议么?




如果我在之后进行控制if(status == google .maps.GeocoderStatus.OK){变量 i 在我的循环中,我得到我的数组的最后一个数字...每一次。而循环只似乎更新我的数组中的最后一个项目,如果我加载后,我的 poiArray 来自Google的异步数据加载。

你可以通过函数闭包来解决你的问题。另外,您不应该使用Google Maps JavaScript API v3(.k,.B)的未公开的属性,它们将随着API的更新而改变,并破坏您的代码。

  var geocoder = new google.maps.Geocoder(); 
函数geocodeAddress(curr,i){
geocoder.geocode({'address':curr.address},function(results,status){
if(status == google.maps。 GeocoderStatus.OK){
cur.lat = results [0] .geometry.location.lat();
cur.long = results [0] .geometry.location.lng();
} else {
console.log('failed to include'+ curr.title);
poiArray.splice(i--,1);
}
});

(i = 0; i var curr = poiArray [i];
if(!curr.lat ||!curr.long){
geocodeAddress(curr,i);
}
}


So I have an array of objects with the following properties: Address, Latitude, Longitude, and Title

 poi("139 Main St", 0, 0, "Farmer's Market" ),
 poi("18 N Hopkins Ave", 37.4455, -143.7728, "YMCA" ),
 poi("42 Universe St", 37.4855, -143.8781, "Original Pizza #32" )

Now, the first object does not have anything set for latitude and longitude, so I was hoping to use Google's Geocode API to fill in the missing data. I included the API script link with my API key, which is working fine.

I then created a for loop that goes through the array and finds any object with missing data. It's supposed to go through and replace any empty cells with the correct information. However, though the information is retrieved, it does so after the loop is completed. I can console out the correct information, but only after the function returns empty data.

for (i=0; i < poiArray.length; i++ ) {
    var curr = poiArray[i];

    if ( !curr.lat || !curr.long ) {

        geocoder = new google.maps.Geocoder();
        geocoder.geocode( { 'address': curr.address }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                cur.lat = results[0].geometry.location.k;
                cur.long = results[0].geometry.location.B;
            } else {
                console.log('failed to include ' + curr.title);
                poiArray.splice(i--, 1);    
            }

       });  

    }

}

I've tried timeout and nesting functions, but nothing seems to work. Any suggestions?


Let me mention a bit more weirdness: if I console after if (status == google.maps.GeocoderStatus.OK) { the variable i in my for loop, I get the last number of my array... every time. And the loop only seems to update the last item in my array if I console my poiArray after the asynchronous data from Google has loaded.

解决方案

you can solve your issue with function closure. Also, you shouldn't use undocumented properties of the Google Maps Javascript API v3 (.k, .B), they will change with updates of the API and break your code.

var geocoder = new google.maps.Geocoder();
function geocodeAddress(curr, i) {
  geocoder.geocode( { 'address': curr.address }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
       cur.lat = results[0].geometry.location.lat();
       cur.long = results[0].geometry.location.lng();
    } else {
       console.log('failed to include ' + curr.title);
       poiArray.splice(i--, 1);    
    }
  });  
}
for (i=0; i < poiArray.length; i++ ) {
    var curr = poiArray[i];
    if ( !curr.lat || !curr.long ) {
        geocodeAddress(curr, i);
    }
}

这篇关于Google Maps API功能不会及时返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆