javascript - 使用 Google Maps Javascript Api 检测 map 加载时的查询限制消息

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

我在使用 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/

相关文章:

java - 我的逐项叠加层和 Google map 中的叠加层有何区别?

javascript - 在 Google Maps API v3 中将图像添加到 InfoBox

flutter - 如何在谷歌地图 Flutter 中动态绘制多边形

java - 如何在 Google map 中查找多边形的中心纬度(Android、Java)

android - Google Maps API - 标记图像的偏移量 - 替换标记 - 不在 GeoPosition

javascript - 在 javascript 中构建一个自迭代表?

javascript - 包装 jQuery.ajax : Cascading inner jqXHR as a new deffered object to return

javascript - xml 到 json 映射挑战

javascript - Angular 2 : Toggle buttons are not working as expected

javascript - 使用angularjs在特定页面加载外部库