Ich möchte den physischen Standort (lat und lang) auf der Grundlage der MAC-Adresse über Google Maps Geolocation api in Javascript erhalten.Google Maps API: Anfordern und Empfangen von JSON-Daten für Geolocation
Gemäß der Dokumentation von Google Maps API für das Geotargeting, müssen die Informationen im JSON-Format https://developers.google.com/maps/documentation/geolocation/intro#body an die angegebene URL angefordert werden, auf dem sie die Daten in json gibt https://developers.google.com/maps/documentation/geolocation/intro#responses
ich dieses Stück JavaScript-Code versucht haben:
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);
Aber ich bekomme die folgende Fehlermeldung in der Konsole
POST https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY 403()
Anything falsch mit dem Code? Oder gibt es eine andere bessere Methode?
HINWEIS: Sie könnten fragen, warum nicht einfach navigator.geolocation
verwenden? Nun, weil das System, auf dem ich mein Projekt aufbaue, keine HTML5 Geolocation API unterstützt.