GeolocationCoordinates: toJSON() method
The toJSON()
method of the GeolocationCoordinates
interface is a serializer; it returns a JSON representation of the GeolocationCoordinates
object.
Syntax
js
toJSON()
Parameters
None.
Return value
A JSON
object that is the serialization of the GeolocationCoordinates
object.
Examples
Using the toJSON()
method
In this example, calling position.coords.toJSON()
returns a JSON representation of the GeolocationCoordinates
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 use JSON.stringify(position.coords)
directly; it will call toJSON()
automatically.
Specifications
Specification |
---|
Geolocation # tojson-method-0 |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
toJSON |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
- No support
- No support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.