GeolocationCoordinates:toJSON() 方法
GeolocationCoordinates
介面的 toJSON()
方法是序列化器;它返回以 JSON 表示的 GeolocationCoordinates
物件。
語法
js
toJSON()
參數
無。
返回值
一個 JSON
物件,它是 GeolocationCoordinates
物件的序列化表示。
範例
使用 toJSON()
方法
在這個範例中,調用 position.coords.toJSON()
返回 GeolocationCoordinates
物件的 JSON 表示。
js
navigator.geolocation.getCurrentPosition((position) => {
console.log(position.coords.toJSON());
});
這將會記錄一個像這樣的 JSON 物件:
json
{
"accuracy": 12.0,
"latitude": 53.0,
"longitude": 8.0,
"altitude": null,
"altitudeAccuracy": null,
"heading": null,
"speed": null
}
要獲取 JSON 字串,你可以直接使用 JSON.stringify(position.coords)
;它會自動調用 toJSON()
。
規範
Specification |
---|
Geolocation # tojson-method-0 |
瀏覽器相容性
BCD tables only load in the browser