【问题标题】:Google Maps API OVER QUERY LIMIT per second limitGoogle Maps API OVER QUERY LIMIT 每秒限制
【发布时间】:2012-12-10 10:43:05
【问题描述】:

我正在使用 Google Maps API 在地图上显示大约 50 个位置。我正在使用客户端地理编码。我正在使用 window.setTimeout 来控制应用程序每秒发送的地理编码请求数。如果我每秒发送超过 1 个请求,我会收到 OVER QUERY LIMIT 响应。

问题:这个限制不应该是每秒 10 次查询吗?如果是,那么我做错了什么?如果没有,那么 Business API 是否有更慷慨的每秒查询限制?

请注意,我们的应用程序不会达到每天 25,000 个查询。

【问题讨论】:

    标签: google-maps google-maps-api-3


    【解决方案1】:

    地理编码器具有配额和速率限制。根据经验,您可以对大约 10 个位置进行地理编码而不会达到查询限制(实际数量可能取决于服务器负载)。最好的解决方案是在收到 OVER_QUERY_LIMIT 错误时延迟,然后重试。查看这些类似的帖子:

    【讨论】:

    • 感谢 geocodezip。我确实尝试使用 window.setTimeout 来延迟它,但它只能延迟 1 秒。现在,我更改了代码,以便发出地理编码请求,直到收到 OVER_QUERY_LIMIT 响应。之后我暂停 3 秒,然后重复。这种策略似乎在大约 26 秒(而不是 50 秒)内解决了 50 个请求。如果有更好的策略,我会全力以赴。
    【解决方案2】:

    通常当您需要在地图上显示这么多点时,最好使用服务器端方法,本文解释了何时使用每个点:

    地理编码策略:https://developers.google.com/maps/articles/geocodestrat

    客户端限制并不完全是“每秒 10 个请求”,由于 API 文档中没有解释,因此我不会依赖它的行为。

    【讨论】:

      【解决方案3】:

      由于 Google 服务器过载,这种方法不正确。 有关更多信息,请参阅 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"
        }
         ]
      };
      			
      function loadMarkerTimeout(timeout) {
        setTimeout(loadMarker, timeout)
      }
      
      function loadMarker() {	
        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){				
          GMaps.geocode({
            address: e.address,
            callback: function(results, status) {
              geocode_count++;		
              
              console.log('Id: '+e.customerId+' | Status: '+status);
              
              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: 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 -->

      【讨论】:

        【解决方案4】:

        而不是客户端地理编码

        geocoder.geocode({
            'address': your_address
          }, function (results, status) {
             if (status == google.maps.GeocoderStatus.OK) {
               var geo_data = results[0];
               // your code ...
           } 
        })
        

        我会使用服务器端地理编码 API

        var apikey = YOUR_API_KEY;
        var query = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + address + '&key=' + apikey;
        $.getJSON(query, function (data) {
          if (data.status === 'OK') { 
            var geo_data = data.results[0];
          } 
        })
        

        【讨论】:

          猜你喜欢
          • 2018-04-18
          • 1970-01-01
          • 1970-01-01
          • 2012-10-17
          • 1970-01-01
          • 2018-11-09
          • 2017-08-03
          • 2018-06-27
          相关资源
          最近更新 更多