GeolocationCoordinates: toJSON()-Methode
Baseline
2024
Newly available
Since September 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Die toJSON()-Methode der GeolocationCoordinates-Schnittstelle ist ein Serializer; sie liefert eine JSON-Darstellung des GeolocationCoordinates-Objekts.
Syntax
js
toJSON()
Parameter
Keine.
Rückgabewert
Ein JSON-Objekt, das die Serialisierung des GeolocationCoordinates-Objekts darstellt.
Beispiele
>Verwendung der toJSON()-Methode
In diesem Beispiel gibt der Aufruf von position.coords.toJSON() eine JSON-Darstellung des GeolocationCoordinates-Objekts zurück.
js
navigator.geolocation.getCurrentPosition((position) => {
console.log(position.coords.toJSON());
});
Dies würde ein JSON-Objekt wie folgt protokollieren:
json
{
"accuracy": 12.0,
"latitude": 53.0,
"longitude": 8.0,
"altitude": null,
"altitudeAccuracy": null,
"heading": null,
"speed": null
}
Um einen JSON-String zu erhalten, können Sie JSON.stringify(position.coords) direkt verwenden; diese Methode ruft toJSON() automatisch auf.
Spezifikationen
| Specification |
|---|
| Geolocation> # tojson-method-0> |
Browser-Kompatibilität
Loading…