GeolocationCoordinates: toJSON()-Methode

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

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