GeolocationCoordinates: phương thức 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.
Phương thức toJSON() của giao diện GeolocationCoordinates là một serializer; nó trả về một biểu diễn JSON của đối tượng GeolocationCoordinates.
Cú pháp
js
toJSON()
Tham số
Không có.
Giá trị trả về
Một đối tượng JSON là kết quả tuần tự hóa của đối tượng GeolocationCoordinates.
Ví dụ
>Dùng phương thức toJSON()
Trong ví dụ này, gọi position.coords.toJSON() sẽ trả về một biểu diễn JSON của đối tượng GeolocationCoordinates.
js
navigator.geolocation.getCurrentPosition((position) => {
console.log(position.coords.toJSON());
});
Lệnh này sẽ ghi ra một đối tượng JSON như sau:
json
{
"accuracy": 12.0,
"latitude": 53.0,
"longitude": 8.0,
"altitude": null,
"altitudeAccuracy": null,
"heading": null,
"speed": null
}
Để có một chuỗi JSON, bạn có thể dùng trực tiếp JSON.stringify(position.coords); nó sẽ tự động gọi toJSON().
Thông số kỹ thuật
| Specification |
|---|
| Geolocation> # tojson-method-0> |