Javascript code not fetching location longitude an

2019-08-01 03:22发布

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条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-01 04:03

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
 }
}
查看更多
登录 后发表回答