2017-05-08 49 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秒後等等。