[英]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: 可以通过组合两种方法来解决该问题:
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.