javascript - 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>
解决方案
推荐阅读
- reactjs - 为什么单击 React 后未检查单选按钮?
- opengl - 给定一个投影矩阵,如何在 OpenGL 中获得视场角?
- excel - 运行时错误 9 仅在一台机器上发生?
- php - PHP:如何将字符串中的单词 A 替换为单词 B - A 和 B 在数组 [] 和 [+1] 中
- c# - 如何在替代单击c#上打开和关闭工具条菜单下拉菜单
- java - 如何从本地目录加载 resources.properties,而不是 Java 中的 JAR?
- c++ - system("/path/to/bash.sh") return file.txt: Permission denied only when c++ program was run as root
- python - 基于时间段的一列的平均值+另一列的条件
- python-3.x - 如何将字符串 06-09-2019 14:34 日期转换为 UTC 06/09/2019 14:34
- javascript - 如何动态读取按钮文本?