我的javascript代码不能从移动chrome浏览器中获取经度和纬度。
下面给出的代码适用于笔记本电脑或桌面浏览器,但不适用于移动设备
<script>
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position) {
alert(position.coords.latitude);
alert(position.coords.longitude);
document.getElementById("input1").value = position.coords.latitude;
document.getElementById("input2").value = position.coords.longitude;
}
</script>
在网站浏览器上显示或获取经度和纬度。
发布于 2019-05-27 10:25:58
试着在移动设备上使用不同的浏览器,应该可以。
您也可以添加handle_error回调函数,以便更清楚地了解问题。
navigator.geolocation.getCurrentPosition(showPosition,handle_error);
function handle_error(err){
if(err.code == 1)
{
//User denied permission
}
else if(err.code == 2)
{
//position unavailable
}
else if(err.code == 3)
{
//Timeout
}
else{
//Some other error
}
}
https://stackoverflow.com/questions/56322913
复制相似问题