地理定位 getCurrentPosition() 方法
示例
獲取使用者位置的緯度和經度
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "此瀏覽器不支援地理定位。";
}
}
function showPosition(position) {
x.innerHTML = "緯度: " + position.coords.latitude +
"<br>經度: " + position.coords.longitude;
}
自己動手試一試 »
描述
getCurrentPosition() 方法返回裝置的當前位置。
瀏覽器支援
方法 |
|
|
|
|
|
getCurrentPosition() |
5.0 |
9.0 |
3.5 |
5.0 |
16.0 |
語法
navigator.geolocation.getCurrentPosition(success, error, options)
引數值
引數 |
型別 |
描述 |
success |
|
必需。一個回撥函式,它以 Position 物件作為其唯一的輸入引數 |
error |
|
可選。一個回撥函式,它以 PositionError 物件作為其唯一的輸入引數 |
選項 |
|
可選。一個 PositionOptions 物件 |
返回值
W3schools 學習路徑
跟蹤您的進度 - 免費!