GeolocationPosition: 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 des GeolocationPosition-Interfaces ist ein Serializer; sie gibt eine JSON-Darstellung des GeolocationPosition-Objekts zurück.
Syntax
js
toJSON()
Parameter
Keine.
Rückgabewert
Ein JSON-Objekt, das die Serialisierung des GeolocationPosition-Objekts darstellt.
Beispiele
>Verwendung der toJSON()-Methode
In diesem Beispiel gibt der Aufruf von position.toJSON() eine JSON-Darstellung des GeolocationPosition-Objekts zurück.
js
navigator.geolocation.getCurrentPosition((position) => {
console.log(position.toJSON());
});
Dies würde ein JSON-Objekt wie folgt protokollieren:
json
{
"timestamp": 1717509611840,
"coords": {
"accuracy": 13.0,
"latitude": 53.0,
"longitude": 8.0,
"altitude": null,
"altitudeAccuracy": null,
"heading": null,
"speed": null
}
}
Um einen JSON-String zu erhalten, können Sie direkt JSON.stringify(position) verwenden; diese Funktion ruft toJSON() automatisch auf.
Spezifikationen
| Specification |
|---|
| Geolocation> # tojson-method> |
Browser-Kompatibilität
Loading…