首页 > 解决方案 > navigator.geolocation.getCurrentPosition 不适用于移动设备

问题描述

我已经简化了我的问题。我只想使用 android mobile 获取坐标。该网页在桌面上运行良好,但在 android 手机上无法正常工作。我的位置已开启。

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
    alert("Working"); // This alert is shown but coordinates are not displayed
        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;
}
</script>

</body>
</html>

标签: javascripthtml

解决方案


推荐阅读