2017-07-26 42 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鏈接。這將有所幫助。