2017-07-26 58 views
11

我使用的JavaScript,并得到一个消息,我已经超过了我的这个API的每日请求配额。有没有办法在try catch块中捕获此错误消息,因此当我检查配额时,我可以执行另一段代码。我看过几个类似的帖子,但没有什么有用的。这是我的代码。尝试赶上谷歌地图API中的错误消息

(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'])); 

回答

2

更新 由于每documentation

如果你想以编程方式检测认证失败(例如自动发送信标)

你可以编写回调函数。如果定义了以下全局函数,那么将在身份验证失败时调用它。功能gm_authFailure(){//代码}

这是错误的gm_authFaliure功能应该能够赶上的list。它还提到了一个OverQuotaMapError错误。

作为每documentation

如果过多请求在一定时间期间内提出,API返回一个OVER_QUERY_LIMIT响应代码。

所以你应该检查响应代码。如果谷歌地图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>

+0

我已经尝试创建一个捕获响应代码的变量。它所有的console.logs是未定义的。你能给我一个如何捕获响应代码或如何使用函数gm_authFailure()的例子。我已经尝试过并且没有成功。 – Aaron

+0

似乎gm_authFaliure函数没有收到任何参数,因此您不能检查错误的类型,但可以确定出现了问题并且正常处理它。查看更新的答案。 –

+0

你能告诉我如何整合googleMapError与我的代码,我已经尝试了几种不同的方法,并没有一个工作 – Aaron

-1

是,JavaScript supports try-catch blocks。这里是你的代码的实现:

(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') 
      try { 
       var map = new google.maps.Map(element, options) 
      } catch (error) { 
       // handle error 
       console.log(error.message); 
      } finally { 
       // optional cleanup code 
      } 
     }(window, window.google, result[i]['latitude'], result[i]['longitude'])); 
+0

我已经尝试把试图抓住var map = new google.maps.Map(element,options)。它没有工作 – Aaron

+1

你能否提供一些关于_how_没有工作的更多细节,比如特定的错误信息? –

+0

尝试捕捉不起作用。没有具体的错误信息。 try catch没有执行 – Aaron

0

作为每谷歌文档。

如果超过了使用限制,你会得到一个OVER_QUERY_LIMIT状态 代码作为响应。

这意味着Web服务将停止提供正常响应 并切换为仅返回状态码OVER_QUERY_LIMIT,直到再次允许更多 的使用。这可能发生:

  • 在几秒钟内,如果收到错误,因为您的应用程序每秒发送太多请求。

  • 在接下来的24小时内,如果因为您的应用程序每天发送太多请求而收到错误。每日配额为
    在太平洋时间午夜重置。

请参考this链接。这将有所帮助。