检测Google Maps API配额限制

时间:2014-06-26 12:08:36

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

如果达到Google Maps Javascript API V3的配额,是否有人知道通过Javascript或HTTP-Request进行测试的方法?获取为用户显示的错误消息就足够了。

我们启用了结算功能,并且对Google Maps Javascript API v3的配额大约为100,000,但有时我们会将其分解,有时甚至无法实现。现在我们喜欢使用像nagios这样的工具监控我们的api访问(也许通过使用phantomjs)来获得即时警告。但是,如果我们的配额已经超过,我找不到任何关于如何测试的信息。

UPDATE2

此处可找到类似的请求:How to detect that the 25000 request per day limit has been reached in Google Maps?

但是只有API-Console的引用。如果有人设法使用Javascript回退到静态地图,我可以修改我的案例的脚本。

更新

以下是Google地图的本地化配额错误消息示例,该消息是为用户而不是地图显示的。如果发生这种情况,我宁愿删除地图容器:

Example of localized quota message

2 个答案:

答案 0 :(得分:6)

我认为您想知道自己是否超出了Google Maps API Web服务请求限制。以下是官方文档所说的内容:

  

超出使用限制

     

如果超出使用限制,您将获得OVER_QUERY_LIMIT状态   代码作为回应。

     

这意味着Web服务将停止提供正常响应   并切换到仅返回状态代码OVER_QUERY_LIMIT直到更多   再次允许使用。这可能发生:

     
      
  • 如果因为您的应用程序每秒发送的请求过多而收到错误,则会在几秒钟内完成。
  •   
  • 在接下来的24小时内,如果因为您的应用程序每天发送了太多请求而收到错误。一天中的时间   重置服务的每日配额因客户而异   并且对于每个API,并且可以随时间变化。
  •   
     

收到状态代码为OVER_QUERY_LIMIT的回复后,您的回复   应用程序应确定已超出哪个使用限制。这个   可以通过暂停2秒并重新发送相同的请求来完成。   如果状态代码仍为OVER_QUERY_LIMIT,则表示您的应用程序正在发送   每天请求太多。否则,您的应用程序也在发送   每秒许多请求。

代码示例:

url = "MAPS_API_WEBSERVICE_URL"
attempts = 0
success = False

while success != True and attempts < 3:
  raw_result = urllib.urlopen(url).read()
  attempts += 1
  # The GetStatus function parses the answer and returns the status code
  # This function is out of the scope of this example (you can use a SDK).
  status = GetStatus(raw_result)
  if status == "OVER_QUERY_LIMIT":
    time.sleep(2)
    # retry
    continue
  success = True

if attempts == 3:
  # send an alert as this means that the daily limit has been reached
  print "Daily limit has been reached"

引自Google地图文档:https://developers.google.com/maps/documentation/business/articles/usage_limits#limitexceeded

TLDR:发出请求,如果收到OVER_QUERY_LIMIT响应,请在两秒钟后重试。如果回复仍然相同,那么您已达到每日限额。

答案 1 :(得分:1)

我基于观察到Google错误消息包含具有dismissButton类属性的元素的想法,提出了一个解决方案。使用jQuery,就像

var quota_exceeded = false;
$(document).ready( function() {
    setTimeout(check_quota, 1000);
    setTimeout(check_quota, 3000);
    setTimeout(check_quota, 10000);
});
function check_quota() {
    if (quota_exceeded) return;
    if (typeof($("button.dismissButton")[0]) === "undefined") return;
    quota_exceeded = true;
    // you can even hijack the box and add to Google's message
    $("button.dismissButton").parents("td").html(
        "We have exceeded our quota!!!!!<p>" +
        "<img src='https://c1.staticflickr.com/8/7163/6852688771_c90c9887c3.jpg'>" +
        "<p><button class='dismissButton'>OK</button>");
    $("button.dismissButton").click( function() {
        $("button.dismissButton").parentsUntil("div").parent().hide();
    });
}