javascript 检测 Google Maps API 配额限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24430147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect Google Maps API quota limit
提问by Trendfischer
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.
如果达到 Google Maps Javascript API V3 的配额,有人知道通过 Javascript 或 HTTP 请求进行测试的方法吗?获取为用户显示的错误消息就足够了。
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.
我们启用了计费并为 Google Maps Javascript API v3 设置了大约 100,000 个配额,但有时我们会破坏它,有时甚至没有意识到。现在我们喜欢使用 nagios 之类的工具(可能通过使用 phantomjs)来监控我们的 api 访问以获得即时警告。但是我找不到任何关于如何测试的信息,如果我们的配额已超过。
Update2
更新2
A similar request ist found here: How to detect that the 25000 request per day limit has been reached in Google Maps?
在这里找到了一个类似的请求:如何检测 Google 地图中已达到每天 25000 个请求的限制?
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.
但是只有对 API-Console 的引用。如果有人设法使用 Javascript 回退到静态地图,我可以针对我的情况修改脚本。
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:
这是谷歌地图的本地化配额错误消息的示例,它为用户而不是地图显示。如果发生这样的事情,我宁愿删除地图容器:
回答by xyleen
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:
我假设您想知道是否超出了 Google Maps API Web 服务请求限制。这是官方文档所说的:
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.
超出使用限制
如果超过使用限制,您将收到 OVER_QUERY_LIMIT 状态代码作为响应。
这意味着 Web 服务将停止提供正常响应并切换到仅返回状态代码 OVER_QUERY_LIMIT,直到再次允许更多使用。这可能发生:
- 在几秒钟内,如果由于您的应用程序每秒发送的请求过多而收到错误。
- 在接下来的 24 小时内的某个时间,如果由于您的应用程序每天发送的请求过多而收到错误。一天的时间,在
这每天配额的服务是客户重置之间以及每个API不同,并且可以随时间而改变。收到状态代码为 OVER_QUERY_LIMIT 的响应后,您的应用程序应确定超出了哪个使用限制。这可以通过暂停 2 秒并重新发送相同的请求来完成。如果状态代码仍为 OVER_QUERY_LIMIT,则您的应用程序每天发送的请求过多。否则,您的应用程序每秒发送的请求过多。
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.com/maps/documentation/business/articles/usage_limits#limitexceeded
引自谷歌地图文档:https: //developers.google.com/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.
TLDR:发出请求,如果收到 OVER_QUERY_LIMIT 响应,请在两秒钟后重试。如果响应仍然相同,则您已达到每日限制。
回答by mob
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
我根据观察到 Google 错误消息包含具有dismissButton
class 属性的元素提出了一个解决方案。使用 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 hiHyman 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();
});
}