简体   繁体   English

如何避免Google Maps JavaScript API出现问题?

[英]How can I avoid problems with Google Maps JavaScript API?

I am using the Google Maps JavaScript API in combination with an api key. 我将Google Maps JavaScript API与api键结合使用。 This works great for some hours or days, but after a specific intervall I get the following 403 error and the map is gone: 这在几个小时或几天内都有效,但是经过特定的时间间隔后,出现以下403错误,地图消失了:

在此处输入图片说明

I don't know where the problem is because I didn't have reached the 25.000 requests per day yet. 我不知道问题出在哪里,因为我每天尚未达到25.000个请求。 If I reset the api key and reload the page the map is loading correctly again, but I don't wanna reset the api key again and again. 如果我重设api密钥并重新加载页面,则地图将再次正确加载,但是我不想一次又一次地重设api密钥。

As you can see I am using a custom map, but I don't think that the code is the problem, but here is the code: 如您所见,我使用的是自定义地图,但是我认为代码不是问题所在,但是代码如下:

(function(window, google){

    var infoWindow = null;

    function init() {
        var map;
        var mapOptions = {
            zoom: 12,
            scrollwheel: false,
            center: new google.maps.LatLng(
                51.050409,
                13.737262
            ),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(
            document.getElementById('map'),
            mapOptions
        );
        setMarkers(map, locations);
        infoWindow = new google.maps.InfoWindow({
            content: "holding..."
        });
    }

    function setMarkers(map, locations) {
        var i, icon, marker, contentString;
        for (i = 0; i < locations.length; i++) {
            icon = '../Images/icn-marker.png';
            contentString = '<div><b>' + locations[i][0] + '</b><br>' + locations[i][1] + '<br>' + locations[i][4] + '</div>';
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][2], locations[i][3]),
                map: map,
                title: locations[i][0],
                icon: icon,
                html: contentString
            });
            google.maps.event.addListener(marker, 'click', function() {
                infoWindow.setContent(this.html);
                infoWindow.open(map, this);
            });
        }
    }

    google.maps.event.addDomListener(window, 'load', init);

})(window, google);

Any my script include looks like the following: 我的所有脚本都包括以下内容:

<script src="https://maps.googleapis.com/maps/api/js?key=MYAPIKEY"></script>
<script src="http://example.de/example/map.js"></script>

Hope you can help, because I dont' know where the problem is. 希望您能提供帮助,因为我不知道问题出在哪里。

The 403 error response indicate that your API key is not valid or was not included in the request. 403 error响应表明您的API密钥无效或未包含在请求中。 Please enxure that you have included the entire key and that you have enabled the API for this key. 请确保已包含整个密钥,并且已为此密钥启用了API。

Also, check your usage limit , if you exceed the usage limits or otherwise abuse the service, the web service will return a specific error message. 另外,请检查您的使用限制 ,如果超出使用限制或滥用服务,Web服务将返回特定的错误消息。 If you continue to exceed limits, your access to the web service may be blocked. 如果您继续超出限制,则可能会阻止您访问Web服务。 It is also possible to receive 403 Forbidden responses. 也有可能收到403禁止响应。

The problem can be address by combining two approaches: 可以通过组合两种方法来解决该问题:

  • Lowering usage, by optimizing applications to use the web services more efficiently. 通过优化应用程序以更有效地使用Web服务来降低使用率。
  • Increasing usage limits, when possible, by purchasing additional allowance for your Google Maps API for Work license. 在可能的情况下,通过购买Google Maps API for Work许可的额外津贴来提高使用限制。

Note: When requests fail, you should ensure that you retry requests with exponential backoff. 注意:如果请求失败,则应确保重试带有指数补偿的请求。 For example, if a request fails once, retry after a second, if it fails again, retry after two seconds, then four seconds, and so on. 例如,如果请求失败一次,请一秒钟后重试;如果请求再次失败,请在两秒钟后再重试四秒钟,依此类推。 This ensures that broken requests or wide scale failures do not flood Google's servers, as many clients try to retry requests very quickly. 这样可以确保中断的请求或大规模失败不会淹没Google的服务器,因为许多客户端会尝试非常快地重试请求。

Here's a related SO ticket encountered 403 error response: Google static map API getting 403 forbidden when loading from img tag 这是一个相关的SO票证,遇到403错误响应: 从img标签加载时,Google静态地图API被禁止403

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Javascript中访问Google Maps API的版本号? - How can I access the version number of the Google Maps API in Javascript? 如何通过javascript动态添加Google Maps API? - How can I dynamically add the Google Maps API via javascript? 如何使用Google Maps Javascript API获得鸟瞰图 - How can I get aerial view with the Google Maps Javascript API 在Google Maps Javascript API v2(营业执照)中,如何正确调用Maps API? - In Google Maps Javascript API v2(for Business License), How can I call Maps API correctly? 如何在传递给 google maps api 的 javascript 的 xml 数据中添加锚标记 - How can I add a anchor tag in xml data passed to javascript for google maps api 如何从 Google Maps JavaScript API 中的给定路径值获取距离? - How can I get distance from given path value in Google Maps JavaScript API? 如何从谷歌地图javascript api v3获得距离 - How can I get distances from Google maps javascript api v3 如何使用Google Maps javascript API中的一组坐标生成卫星图像? - How can I produce a satellite image using a set of coordinates in Google Maps' javascript API? 如何使用javascript api v3在flash中显示Google地图? - How can I display Google maps inside flash using javascript api v3? 如何在Google Maps Javascript API中计算多个多边形的中心? - How can I calculate the center of multiple polygons in Google Maps Javascript API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM