2016-06-07 97 views
0

我想通過javascript中的Google Maps Geolocation API在MAC地址的基礎上獲取物理位置(經緯度和長度)。Google Maps API:請求和接收json數據以獲取地理位置

按照谷歌地圖API的文檔,進行地理定位,信息必須在要求其所返回JSON數據https://developers.google.com/maps/documentation/geolocation/intro#responses

我已經試過這段JavaScript代碼的指定網址JSON格式https://developers.google.com/maps/documentation/geolocation/intro#body

xhr = new XMLHttpRequest(); 
var url = "https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY"; 
xhr.open("POST", url, true); 
xhr.setRequestHeader("Content-type", "application/json"); 
xhr.onreadystatechange = function() { 
    if (xhr.readyState == 4 && xhr.status == 200) { 
     var json = JSON.parse(xhr.responseText); 
     console.log(json.location.lat+","+json.location.lng+","+json.accuracy); 
    } 
} 
var data = JSON.stringify({ 
       "wifiAccessPoints": [ 
        { 
         "macAddress": "MY_MAC_ADDRESS" 
        }, 
        { 
         "macAddress": "MY_MAC_ADDRESS" 
        } 
       ] 
      }); 

xhr.send(data); 

但我得到以下錯誤控制檯

POST https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY 403() 

什麼代碼錯了?或者還有另一種更好的方法?

NOTE:您可能會問爲什麼不簡單使用navigator.geolocation?那麼,因爲我構建項目的系統不支持HTML5地理定位API。

回答

0

錯誤(403),你得到一個基於documentiondailyLimitExceededuserRateLimitExceeded。原因是您已超出您在Google Developers Console中配置的每用戶每用戶限制的請求數。應該配置此限制以防止單個或一小組用戶耗盡您的每日配額,同時仍允許合理訪問所有用戶。

有關更多信息以及如何檢查您在Google Developers Console中使用的使用情況或配額,請遵循此page上的指導原則。