在Google Maps api中尝试抓取错误消息

Aar*_*ron 13 javascript google-maps

我使用javascript并收到一条消息,表示我已超出此API的每日请求配额.有没有办法在try catch块中捕获此错误消息,因此当我查看配额时,我可以执行另一段代码.我看过几篇类似的帖子,但没什么有用的.这是我的代码.

(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']));
Run Code Online (Sandbox Code Playgroud)

Luc*_*oni 7

根据文档更新:

如果要以编程方式检测身份验证失败(例如,自动发送信标),则可以准备回调函数.如果定义了以下全局函数,则在身份验证失败时将调用它.function gm_authFailure(){// code}

以下是gm_authFaliure函数应该能够捕获的错误列表.它还提到了OverQuotaMapError错误.

根据文件:

如果在特定时间段内发出的请求太多,则API会返回OVER_QUERY_LIMIT响应代码.

所以你应该检查响应代码.如果Google地图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])
}())
Run Code Online (Sandbox Code Playgroud)
<!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>
Run Code Online (Sandbox Code Playgroud)