Phương thức định vị địa lý getCurrentPosition()
Ví dụ
Lấy vĩ độ và kinh độ vị trí của người dùng:
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
Hãy tự mình thử »
Sự miêu tả
Phương thức getCurrentPosition() trả về vị trí hiện tại của thiết bị.
Hỗ trợ trình duyệt
Method |
|
|
|
|
|
getCurrentPosition() |
5.0 |
9.0 |
3.5 |
5.0 |
16.0 |
Cú pháp
navigator.geolocation.getCurrentPosition( success, error, options )
Giá trị tham số
Parameter |
Type |
Description |
success |
|
Required. A callback function that takes a Position object as its sole input
parameter |
error |
|
Optional. A callback function that takes a PositionError object as its sole
input parameter |
options |
|
Optional. A PositionOptions object |
Giá trị trả về
Vị trí hiện tại của thiết bị |
Người tìm đường W3schools
Theo dõi tiến trình của bạn - hoàn toàn miễn phí!