Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. GeolocationCoordinates
  4. toJSON()

GeolocationCoordinates: toJSON() method

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.

ThetoJSON() method of theGeolocationCoordinates interface is aserializer; it returns a JSON representation of theGeolocationCoordinates object.

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp