我想通過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。