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