【发布时间】:2016-03-15 18:54:43
【问题描述】:
我正在尝试获取当前位置坐标经度和纬度。此代码在 Firefox Microsoft Edge 上正常运行。但在 google chrome 上无法正常运行,任何修改都可以使其与其他浏览器兼容。
<!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) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
</script>
</body>
</html>
【问题讨论】:
-
这是一个 JSBIN:jsbin.com/juraqo/edit?html,js,output。这对我在 Google Chrome 中非常有用。
-
但它没有提供准确的结果。
-
您在寻找的准确度如何?
-
方圆一公里的地图。
-
我的距离在 10 米以内(使用 Chrome)。不过我在大城市。在农村地区,情况可能更糟。你的距离有多远?
标签: javascript jquery html google-maps-api-3