2017-05-08 67 views
-1

我想解析一个带有400个地址的json,并在每个位置设置地图图标。我的问题是,当我循环的项目时,我得到一个错误:OVER_QUERY_LIMIT。但是,使用Google地理编码API设置职位的最佳方式是什么? 我的函数如下:Address geocode via jquery geocoder.geocode(400 Items)

 function getAddresses(data) { 
      var items, markers_data = []; 
      if (data.addresses.length > 0) { 
       items = data.addresses; 

       for (var i = 0; i < items.length; i++) { 
        var 
         item = items[i] 
         , street = item.address.street 
         , zip = item.address.zip 
         , city = item.address.city 
         , country = item.address.country; 
        var 
         geocoder = new google.maps.Geocoder() 
         , fulladdress = street + ',' + zip + ' '+ city + ',' + country; 

        setTimeout(function() { 
         geocoder.geocode({'address': fulladdress}, 
          function(results, status) { 
          if (status == google.maps.GeocoderStatus.OK) { 

           console.log(results[0].geometry.location.lat()); 

           console.log(results[0].geometry.location.lng());  
           markers_data.push({ 
            lat : results[0].geometry.location.lat(), 
            lng : results[0].geometry.location.lng(), 
            title: item.name, 
            infoWindow: { 
             content: '<h2>'+item.name+'</h2><p>'+ street + zip + city +'</p>' 
            } 
           }); 
          } else { 
           console.log('not geocoded'); 
           console.log('status'); 
           console.log(status); 
          } 
         }); 
        }, 1000); 
       } 
      } 

      map.addMarkers(markers_data); 
     } 

我试图把我的geocoder.geocode功能超时功能,但不幸的是这不需额外的帮助。我在我的js中使用插件gmaps.js。

回答

0

有一个每个会话配额地理编码服务为文档中所述:

The rate limit is applied per user session, regardless of how many users share the same project. When you first load the API, you are allocated an initial quota of requests. Once you use this quota, the API enforces rate limits on additional requests on a per-second basis. If too many requests are made within a certain time period, the API returns an OVER_QUERY_LIMIT response code.

The per-session rate limit prevents the use of client-side services for batch requests, such as batch geocoding. For batch requests, use the Google Maps Geocoding API web service.

https://developers.google.com/maps/documentation/javascript/geocoding#UsageLimits

所以,你应该调节你的客户端调用和初始10名的请求后,每秒执行1个请求或者使用Geocoding API Web服务实现服务器端批处理地理编码,您可以每秒处理多达50个请求。

顺便说一句,在您的代码中,您尝试在1秒后执行所有请求。您应该增加每个下一个请求的延迟时间。

setTimeout(function() { 
    //Your code 
}, 1000 * i); 

因此,第一个请求将立即执行,第二个1秒后,第二个2秒后等等。