我在使用 Google map Javascript Api 时遇到问题。 我想检测我的每日限额配额是否为每 24 小时 25,000 次 map 加载,如 here 中所述已达到。
我已经搜索了很多类似的问题,如[ 1 , 2 , 3 , 4 ],但是他们:
- 没有有用的回复 (1)
- 使用其他 Api 而不是 Google Maps Javascript Api (2)
- 指的是地理编码而不是 map 加载 (3)
- 使用 Google Api 控制台 (4)
我发现唯一有用的问题是 this 。 指Google documentation ,这表示:
If you want to programmatically detect an authentication failure (for example to automatically send an beacon) you can prepare a callback function. If the following global function is defined it will be called when the authentication fails. function gm_authFailure() { /* Code */ };
如果我有一个像这样的简单脚本:
<script>
function initMap() {
var map;
map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: { lat: 42.345, lng: 12.46 }
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"></script>
如何实现函数 gm_authFailure(),以检测达到的查询限制?
我是 Js 新手,不知道如何做到这一点。你能给我一些例子吗?
最佳答案
你只需要全局定义你的函数,就像这样
<script>
function initMap() {
var map;
map = new google.maps.Map(document.getElementById('map'), {
zoom: 7,
center: { lat: 42.345, lng: 12.46 }
});
}
function gm_authFailure() {
alert("test"); // here you define your authentication failed message
};
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"></script>
<div id="map" style="height: 500px; width: 500px;"></div>
关于javascript - 使用 Google Maps Javascript Api 检测 map 加载时的查询限制消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45633672/