gpt4 book ai didi

javascript - 尝试在 google maps api 中捕获错误消息

转载 作者:可可西里 更新时间:2023-11-01 01:53:17 25 4
gpt4 key购买 nike

我正在使用 javascript 并收到一条消息,指出我已超出此 API 的每日请求配额。有没有办法在 try catch block 中捕获此错误消息,这样当我超过我的配额时,我可以执行另一段代码。我看过几个类似的帖子,但没有任何帮助。这是我的代码。

(function (window, google, lat, lng) {
var options = {
center: {
lat: Number(lat),
lng: Number(lng)
},
zoom: 5,
disableDefaultUI: true,
scrollwheel: true,
draggable: false
},
element = document.getElementById('map-canvas')
var map = new google.maps.Map(element, options)
}(window, window.google, result[i]['latitude'], result[i]['longitude']));

最佳答案

更新根据 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}

这是一个list gm_authFaliure 函数应该能够捕获的错误。它还提到了一个 OverQuotaMapError 错误。

根据 documentation :

if too many requests are made within a certain time period, the API returns an OVER_QUERY_LIMIT response code.

所以你应该检查响应代码。如果 Google map javascript 库不允许访问响应代码,那么我建议向 API 发出 HTTP 请求以获取响应代码。

function initMap(window, google, lat, lng) {
var options = {
center: {
lat: Number(lat),
lng: Number(lng)
},
zoom: 5,
disableDefaultUI: true,
scrollwheel: true,
draggable: false
},
element = document.getElementById('map-canvas'),
map = new google.maps.Map(element, options);
};

function googleMapsCustomError(){
alert('Google Maps custom error triggered');
}

// if you want to respond to a specific error, you may hack the
// console to intercept messages.
// check if a message is a Google Map's error message and respond
// accordingly
(function takeOverConsole() { // taken from http://tobyho.com/2012/07/27/taking-over-console-log/
var console = window.console
if (!console) return

function intercept(method) {
var original = console[method]
console[method] = function() {
// check message
if(arguments[0] && arguments[0].indexOf('OverQuotaMapError') !== -1) {
googleMapsCustomError();
}

if (original.apply) {
// Do this for normal browsers
original.apply(console, arguments)
} else {
// Do this for IE
var message = Array.prototype.slice.apply(arguments).join(' ')
original(message)
}
}
}
var methods = ['error']; // only interested in the console.error method
for (var i = 0; i < methods.length; i++)
intercept(methods[i])
}())
<!DOCTYPE html>
<div id="map-canvas"></div>

<script>
// Notice i am defining this within my html file, just to be sure that this function exists before the Google Maps API is loaded.
window.gm_authFailure = function() {
// remove the map div or maybe call another API to load map
// maybe display a useful message to the user
alert('Google maps failed to load!');
}

window.showMap = function() {
var lat = -34.397,
lng = 150.644;
initMap(window, window.google, lat, lng);
};
</script>

<!-- We are passing an invalid API key. Also notice that we have defined 'callback' as 'showMap' which means that when the Google API JavaScript library is finished loading it will call the 'showMap' function. -->
<script src="https://maps.googleapis.com/maps/api/js?key=INVALID_API_KEY&callback=showMap"
async defer></script>

关于javascript - 尝试在 google maps api 中捕获错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45338480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com