2012-12-23 54 views
20

我使用Google Maps API在地圖上顯示約50個位置。我正在使用客戶端地理編碼。我使用window.setTimeout來控制應用程序每秒發送的地理編碼請求的數量。如果我每秒發送超過1個請求,則會收到OVER QUERY LIMIT響應。Google Maps API OVER QUERY LIMIT per second limit

問:是不是這個限制應該是每秒10個查詢?如果是的話,那麼我會做錯什麼?如果不是,那麼Business API是否每秒限制更多慷慨的查詢?

請注意,我們的應用程序不會達到每天25,000個查詢。

+0

你如何查詢地理編碼服務器?顯示一些代碼。 – keune

+0

看看這裏: http://stackoverflow.com/questions/16659398/google-maps-over-query-limit – user2403424

+0

的可能的複製[OVER \ _query \ _LIMIT在使用谷歌地圖(HTTP://計算器.com/questions/3529746/over-query-limit-while-using-google-maps) – xomena

回答

25

地理編碼器具有配額和速率限制。根據經驗,您可以對10個位置進行地理編碼,而不會觸及查詢限制(實際數量可能取決於服務器負載)。最好的解決辦法是延遲當你遇到OVER_QUERY_LIMIT錯誤,然後重試。看到這些類似的帖子:

+4

感謝geocodezip。我確實嘗試了使用window.setTimeout來延遲它,但它只能延遲1秒。現在,我更改了代碼,以便在發出OVER_QUERY_LIMIT響應之前發出地理編碼請求。之後,我暫停3秒鐘,然後重複。這個策略似乎在大約26秒(而不是50秒)內解決了50個請求。如果有更好的策略,我會全神貫注。 – user544192

4

通常,當你需要在地圖上顯示這麼多點,你會更好使用服務器端方法,本文解釋何時使用每一種:

地理編碼策略:https://developers.google.com/maps/articles/geocodestrat

客戶端限制並不完全是「每秒10個請求」,並且由於API文檔中沒有解釋,所以我不會依賴其行爲。

1

這種做法是不是谷歌服務器超載的正確怎麼一回事,因爲。 欲瞭解更多信息請參閱 https://gis.stackexchange.com/questions/15052/how-to-avoid-google-map-geocode-limit#answer-15365


順便說一句,如果你仍要繼續,在這裏你可以找到讓你加載多個標記AJAX源谷歌地圖避免OVER_QUERY_LIMIT錯誤代碼。

我已經在我的onw服務器上測試過,它的工作原理!

var lost_addresses = []; 
    geocode_count = 0; 
    resNumber = 0; 
    map = new GMaps({ 
     div: '#gmap_marker', 
     lat: 43.921493, 
     lng: 12.337646, 
    }); 

function loadMarkerTimeout(timeout) { 
    setTimeout(loadMarker, timeout) 
} 

function loadMarker() { 
    map.setZoom(6);   
    $.ajax({ 
      url: [Insert here your URL] , 
      type:'POST', 
      data: { 
       "action": "loadMarker" 
      }, 
      success:function(result){ 

       /*************************** 
       * Assuming your ajax call 
       * return something like: 
       * array(
       *  'status' => 'success', 
       *  'results'=> $resultsArray 
       * ); 
       **************************/ 

       var res=JSON.parse(result); 
       if(res.status == 'success') { 
        resNumber = res.results.length; 
        //Call the geoCoder function 
        getGeoCodeFor(map, res.results); 
       } 
      }//success 
    });//ajax 
};//loadMarker() 

$().ready(function(e) { 
    loadMarker(); 
}); 

//Geocoder function 
function getGeoCodeFor(maps, addresses) { 
     $.each(addresses, function(i,e){     
       GMaps.geocode({ 
        address: e.address, 
        callback: function(results, status) { 
          geocode_count++;   

          if (status == 'OK') {  

           //if the element is alreay in the array, remove it 
           lost_addresses = jQuery.grep(lost_addresses, function(value) { 
            return value != e; 
           }); 


           latlng = results[0].geometry.location; 
           map.addMarker({ 
             lat: latlng.lat(), 
             lng: latlng.lng(), 
             title: 'MyNewMarker', 
            });//addMarker 
          } else if (status == 'ZERO_RESULTS') { 
           //alert('Sorry, no results found'); 
          } else if(status == 'OVER_QUERY_LIMIT') { 

           //if the element is not in the losts_addresses array, add it! 
           if(jQuery.inArray(e,lost_addresses) == -1) { 
            lost_addresses.push(e); 
           } 

          } 

          if(geocode_count == addresses.length) { 
           //set counter == 0 so it wont's stop next round 
           geocode_count = 0; 

           setTimeout(function() { 
            getGeoCodeFor(maps, lost_addresses); 
           }, 2500); 
          } 
        }//callback 
       });//GeoCode 
     });//each 
};//getGeoCodeFor() 

例子:

map = new GMaps({ 
 
    div: '#gmap_marker', 
 
    lat: 43.921493, 
 
    lng: 12.337646, 
 
}); 
 

 
var jsonData = { 
 
    "status":"success", 
 
    "results":[ 
 
    { 
 
    "customerId":1, 
 
    "address":"Via Italia 43, Milano (MI)", 
 
    "customerName":"MyAwesomeCustomer1" 
 
    }, 
 
    { 
 
    "customerId":2, 
 
    "address":"Via Roma 10, Roma (RM)", 
 
    "customerName":"MyAwesomeCustomer2" 
 
    } 
 
    ] 
 
}; 
 
\t \t \t 
 
function loadMarkerTimeout(timeout) { 
 
    setTimeout(loadMarker, timeout) 
 
} 
 

 
function loadMarker() { \t 
 
    map.setZoom(6); 
 
    
 
    $.ajax({ 
 
    url: '/echo/html/', 
 
    type: "POST", 
 
    data: jsonData, 
 
    cache: false, 
 
    success:function(result){ 
 

 
     var res=JSON.parse(result); 
 
     if(res.status == 'success') { 
 
     resNumber = res.results.length; 
 
     //Call the geoCoder function 
 
     getGeoCodeFor(map, res.results); 
 
     } 
 
    }//success 
 
    });//ajax 
 
    
 
};//loadMarker() 
 

 
$().ready(function(e) { 
 
    loadMarker(); 
 
}); 
 

 
//Geocoder function 
 
function getGeoCodeFor(maps, addresses) { 
 
    $.each(addresses, function(i,e){ \t \t \t \t 
 
    GMaps.geocode({ 
 
     address: e.address, 
 
     callback: function(results, status) { 
 
     geocode_count++; \t \t 
 
     
 
     console.log('Id: '+e.customerId+' | Status: '+status); 
 
     
 
     if (status == 'OK') { \t \t 
 

 
      //if the element is alreay in the array, remove it 
 
      lost_addresses = jQuery.grep(lost_addresses, function(value) { 
 
      return value != e; 
 
      }); 
 

 

 
      latlng = results[0].geometry.location; 
 
      map.addMarker({ 
 
      lat: latlng.lat(), 
 
      lng: latlng.lng(), 
 
      title: e.customerName, 
 
      });//addMarker 
 
     } else if (status == 'ZERO_RESULTS') { 
 
      //alert('Sorry, no results found'); 
 
     } else if(status == 'OVER_QUERY_LIMIT') { 
 

 
      //if the element is not in the losts_addresses array, add it! 
 
      if(jQuery.inArray(e,lost_addresses) == -1) { 
 
      lost_addresses.push(e); 
 
      } 
 

 
     } 
 

 
     if(geocode_count == addresses.length) { 
 
      //set counter == 0 so it wont's stop next round 
 
      geocode_count = 0; 
 

 
      setTimeout(function() { 
 
      getGeoCodeFor(maps, lost_addresses); 
 
      }, 2500); 
 
     } 
 
     }//callback 
 
    });//GeoCode 
 
    });//each 
 
};//getGeoCodeFor()
#gmap_marker { 
 
    min-height:250px; 
 
    height:100%; 
 
    width:100%; 
 
    position: relative; 
 
    overflow: hidden; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script src="http://maps.google.com/maps/api/js" type="text/javascript"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/gmaps.js/0.4.24/gmaps.min.js" type="text/javascript"></script> 
 

 

 
<div id="gmap_marker"></div> <!-- /#gmap_marker -->

0
Here I have loaded 2200 markers. It takes around 1 min to add 2200 locations. 
<https://jsfiddle.net/suchg/qm1pqunz/11/> 


Hope it would help you. 
+0

嘿,你有什麼想法如何在INVALID_REQUEST的情況下跳過? – hyperfkcb