GeolocationCoordinates: toJSON() method
Baseline 2024Newly available
Since September 2024, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
ThetoJSON() method of theGeolocationCoordinates interface is aserializer; it returns a JSON representation of theGeolocationCoordinates object.
In this article
Syntax
js
toJSON()Parameters
None.
Return value
AJSON object that is the serialization of theGeolocationCoordinates object.
Examples
>Using thetoJSON() method
In this example, callingposition.coords.toJSON() returns a JSON representation of theGeolocationCoordinates object.
js
navigator.geolocation.getCurrentPosition((position) => { console.log(position.coords.toJSON());});This would log a JSON object like so:
json
{ "accuracy": 12.0, "latitude": 53.0, "longitude": 8.0, "altitude": null, "altitudeAccuracy": null, "heading": null, "speed": null}To get a JSON string, you can useJSON.stringify(position.coords) directly; it will calltoJSON() automatically.
Specifications
| Specification |
|---|
| Geolocation> # tojson-method-0> |