Javascript code not fetching location longitude and latitude for mobile browser

别等时光非礼了梦想. 提交于 2019-12-11 05:59:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!