最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect Google Maps API quota limit - Stack Overflow

programmeradmin3浏览0评论

Does anyone know a way to test via Javascript or a HTTP-Request, if the quota of Google Maps Javascript API V3 is reached? Getting the error-message, which is displayed for the user, would be sufficient.

We enabled billing and have a quota for Google Maps Javascript API v3 on around 100,000, but sometimes we break it and sometimes even do not realize it. Now we like to monitor our api access with a tool like nagios (perhaps by using phantomjs) to get an instant warning. But I could not find any information on how to test, if our quota has exceeded.

Update2

A similar request ist found here: How to detect that the 25000 request per day limit has been reached in Google Maps?

But there is only a reference to the API-Console. If someone managed to do a fallback to static maps using Javascript, I could modify the script for my case.

Update

Here is an example of the localized quota error-message by google maps, which appears for the user instead of a map. I would prefer to remove the maps container if something like this happens:

Does anyone know a way to test via Javascript or a HTTP-Request, if the quota of Google Maps Javascript API V3 is reached? Getting the error-message, which is displayed for the user, would be sufficient.

We enabled billing and have a quota for Google Maps Javascript API v3 on around 100,000, but sometimes we break it and sometimes even do not realize it. Now we like to monitor our api access with a tool like nagios (perhaps by using phantomjs) to get an instant warning. But I could not find any information on how to test, if our quota has exceeded.

Update2

A similar request ist found here: How to detect that the 25000 request per day limit has been reached in Google Maps?

But there is only a reference to the API-Console. If someone managed to do a fallback to static maps using Javascript, I could modify the script for my case.

Update

Here is an example of the localized quota error-message by google maps, which appears for the user instead of a map. I would prefer to remove the maps container if something like this happens:

Share Improve this question edited May 23, 2017 at 11:53 CommunityBot 11 silver badge asked Jun 26, 2014 at 12:08 TrendfischerTrendfischer 7,6327 gold badges43 silver badges53 bronze badges 2
  • 2 one way my pany guards against quota thresholds is by only allowing the user to view the map if they click a blurred map img that says 'view on map' - we then drop the image from the dom and init the map in its place. – random-forest-cat Commented Jul 2, 2014 at 16:24
  • A blurred map is a nice idea and would certainly help us. But it could be inconvenient for the user. We tried with static maps and on click loading the map. But static maps have a similar quota and due to the licensing, we cannot store the image locally. And there is always the possibility for an outstanding event, which eats up our quota in the early morning already. – Trendfischer Commented Jul 3, 2014 at 8:03
Add a ment  | 

3 Answers 3

Reset to default 5

I assume you want to find out if you've exceeded the Google Maps API Web Services request limit. Here's what the official documentation says:

Usage limits exceeded

If you exceed the usage limits you will get an OVER_QUERY_LIMIT status code as a response.

This means that the web service will stop providing normal responses and switch to returning only status code OVER_QUERY_LIMIT until more usage is allowed again. This can happen:

  • Within a few seconds, if the error was received because your application sent too many requests per second.
  • Some time in the next 24 hours, if the error was received because your application sent too many requests per day. The time of day at
    which the daily quota for a service is reset varies between customers and for each API, and can change over time.

Upon receiving a response with status code OVER_QUERY_LIMIT, your application should determine which usage limit has been exceeded. This can be done by pausing for 2 seconds and resending the same request. If status code is still OVER_QUERY_LIMIT, your application is sending too many requests per day. Otherwise, your application is sending too many requests per second.

And a code sample:

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"

Quoted from the Google Maps documentation: https://developers.google./maps/documentation/business/articles/usage_limits#limitexceeded

TLDR: Make a request, if you get OVER_QUERY_LIMIT response, try again in two seconds. If the response is still the same, you've hit the daily limit.

I came up with a solution based on the observation that the Google error message contains an element with the dismissButton class attribute. With jQuery, it is something like

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./8/7163/6852688771_c90c9887c3.jpg'>" +
        "<p><button class='dismissButton'>OK</button>");
    $("button.dismissButton").click( function() {
        $("button.dismissButton").parentsUntil("div").parent().hide();
    });
}

For Google Javascript Map, I use below codes;

var googleMapTryCount = 0;
var googleMapCheckInterval = setInterval(function () {
    googleMapTryCount++;
    if($(".dismissButton:visible").length == 1){
        gm_authFailure();
        clearInterval(googleMapCheckInterval);
    }

    if(googleMapTryCount >= 10) { 
        clearInterval(googleMapCheckInterval); 
    }
}, 1000);

function gm_authFailure(){
    console.log("gm_authFailure");
    //do something
}
发布评论

评论列表(0)

  1. 暂无评论