问题
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.
回答1:
Try using a different browser in mobile it should work.
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
}
}
来源:https://stackoverflow.com/questions/56322913/javascript-code-not-fetching-location-longitude-and-latitude-for-mobile-browser