Javascript代码未获取移动浏览器的位置经度和纬度 [英] Javascript code not fetching location longitude and latitude for mobile browser
本文介绍了Javascript代码未获取移动浏览器的位置经度和纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的JavaScript代码无法从移动chrome浏览器获取经度和纬度.
My javascript code not fetching longitude and latitude from mobile chrome browser.
以下给出的代码适用于笔记本电脑或台式机浏览器,但不适用于移动
the below-given code is working for laptop or desktop browser but not mobile
<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>
在网站浏览器上显示或获取经度和纬度.
display or get longitude and latitude on the website browser.
推荐答案
尝试在移动设备上使用其他浏览器应该可以.
Try using a different browser in mobile it should work.
您还可以添加 handle_error 回调函数,以更清楚地了解问题.
You can also add handle_error callback function to understand the issue more clearly.
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
}
}
这篇关于Javascript代码未获取移动浏览器的位置经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文