0

我们有一个购买的 Google Map API 密钥。在我们的 Angular 代码中,我们尝试使用“google.maps.Geocode().geocode”,它使用 agm/core 角度库对纬度/经度列表进行反向地理编码。在一秒钟内,我们想发送大约 20-30 个请求,以便我们可以获得有效的响应并在我们的门户网站中显示地址。但是我们收到以下错误:地理编码 API 调用的“OVER_QUERY_LIMIT”。

这是相同的代码片段:

return Observable.create((observer: Observer<object>) => {
if (geocoder) {
       new google.maps.Geocoder().geocode({ 'location': latlng }, function (results, status) {
       console.log(status);
       if (status === 'OK') {
           console.log(results[0].formatted_address);
       }
    });
}});

我们使用 java 脚本尝试了相同的操作并得到了相同的错误。我不确定我们是否需要发送任何其他参数来避免此错误。如果您能指导我们解决问题,我将不胜感激。

提前致谢。

4

1 回答 1

1

感谢您让社区有机会在这里为您提供帮助。现在,您可以用两种不同的方式解决这个问题。我会使用这两种方法,因为一种方法是为了防止您达到 QPS 限制,另一种方法是帮助您在“那座桥准备好过桥”时管理情况,可以这么说.

1) 您可能会在 Google 的标准服务条款允许的情况下缓存所有结果。

Google Maps API 服务条款规定您可以暂时缓存 Google Maps 数据长达 30 天,以提高应用程序的性能。通过缓存 Web 服务响应,您的应用程序可以避免在短时间内发送重复请求。事实上,Web 服务响应总是包含 Cache-Control HTTP 标头,它指示您可以缓存结果的时间段——例如,Cache-Control: public, max-age=86400。为了提高效率,请确保您的应用程序始终将结果缓存至少在此标头中指定的时间,但不超过 Google Maps API 服务条款中指定的最长时间。

2)您可以使用超时和/或抖动请求在响应之间的随机间隔(如Google Docs中所述)和JS Timeout以及由@Andrew Leach提供的完整示例代码来限制您的请求。

// delay between geocode requests - at the time of writing, 100 miliseconds seems to work well
var delay = 100;


  // ====== Create map objects ======
  var infowindow = new google.maps.InfoWindow();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var geo = new google.maps.Geocoder(); 
  var map = new google.maps.Map(document.getElementById("map"), mapOptions);
  var bounds = new google.maps.LatLngBounds();

  // ====== Geocoding ======
  function getAddress(search, next) {
    geo.geocode({address:search}, function (results,status)
      { 
        // If that was successful
        if (status == google.maps.GeocoderStatus.OK) {
          // Lets assume that the first marker is the one we want
          var p = results[0].geometry.location;
          var lat=p.lat();
          var lng=p.lng();
          // Output the data
            var msg = 'address="' + search + '" lat=' +lat+ ' lng=' +lng+ '(delay='+delay+'ms)<br>';
            document.getElementById("messages").innerHTML += msg;
          // Create a marker
          createMarker(search,lat,lng);
        }
        // ====== Decode the error status ======
        else {
          // === if we were sending the requests to fast, try this one again and increase the delay
          if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            nextAddress--;
            delay++;
          } else {
            var reason="Code "+status;
            var msg = 'address="' + search + '" error=' +reason+ '(delay='+delay+'ms)<br>';
            document.getElementById("messages").innerHTML += msg;
          }   
        }
        next();
      }
    );
  }

       // ======= Function to create a marker
 function createMarker(add,lat,lng) {
   var contentString = add;
   var marker = new google.maps.Marker({
     position: new google.maps.LatLng(lat,lng),
     map: map,
     zIndex: Math.round(latlng.lat()*-100000)<<5
   });

  google.maps.event.addListener(marker, 'click', function() {
     infowindow.setContent(contentString); 
     infowindow.open(map,marker);
   });

   bounds.extend(marker.position);

 }

  // ======= An array of locations that we want to Geocode ========
  var addresses = [
           '251 Pantigo Road Hampton Bays NY 11946',
           'Amagensett Quiogue NY 11978',
           '789 Main Street Hampton Bays NY 11946',
           '30 Abrahams Path Hampton Bays NY 11946',
           '3 Winnebogue Ln Westhampton NY 11977',
           '44 White Oak Lane Montauk NY 11954',
           '107 stoney hill road Bridgehampton NY 11932',
           '250 Pantigo Rd Hampton Bays NY 11946',
           '250 Pantigo Rd Hampton Bays NY 11946',
           '44 Woodruff Lane Wainscott NY 11975',
           'Address East Hampton NY 11937',
           'Address Amagansett NY 11930',
           'Address Remsenburg NY 11960 ',
           'Address Westhampton NY 11977',
           'prop address Westhampton Dunes NY 11978',
           'prop address East Hampton NY 11937',
           'Address East Hampton NY 11937',
           'Address Southampton NY 11968',
           'Address Bridgehampton NY 11932',
           'Address Sagaponack NY 11962',
            "A totally bogus address"
  ];

  // ======= Global variable to remind us what to do next
  var nextAddress = 0;

  // ======= Function to call the next Geocode operation when the reply comes back

  function theNext() {
    if (nextAddress < addresses.length) {
      setTimeout('getAddress("'+addresses[nextAddress]+'",theNext)', delay);
      nextAddress++;
    } else {
      // We're done. Show map bounds
      map.fitBounds(bounds);
    }
  }

  // ======= Call that function for the first time =======
  theNext();
于 2018-02-28T00:36:21.200 回答