GeolocationPosition: toJSON()-Methode

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

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
toJSON

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

Siehe auch