このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

GeolocationPosition: toJSON() メソッド

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.

>

toJSON()GeolocationPosition インターフェイスのメソッドで、シリアライザーです。これはこの GeolocationPosition オブジェクトの JSON 表現を返します。

構文

js
toJSON()

引数

なし。

返値

JSON オブジェクトです。この GeolocationPosition オブジェクトをシリアライズしたものです。

>

toJSON() メソッドの使用

この例では、position.coords.toJSON() を呼び出すと、その GeolocationPosition オブジェクトの JSON 表現を返します。

js
navigator.geolocation.getCurrentPosition((position) => {
  console.log(position.toJSON());
});

これは次のような JSON オブジェクトをログ出力します。

json
{
  "timestamp": 1717509611840,
  "coords": {
    "accuracy": 13.0,
    "latitude": 53.0,
    "longitude": 8.0,
    "altitude": null,
    "altitudeAccuracy": null,
    "heading": null,
    "speed": null
  }
}

JSON 文字列を取得するには、JSON.stringify(position) を直接呼び出してください。これは toJSON() を自動的に呼び出します。

仕様書

Specification
Geolocation>
# tojson-method>

ブラウザーの互換性

関連情報