1

I have a few GPS location request through out my HTML file embedded in other functions. I am facing a problem that, when the app is opened if the location isnt on(Or location is switched off after the request is fired). Then i would have to restart the app inorder for the location request to work again.

If it fails once, then even the location is switched backon, every single request after that would be an error.

Why does that happen?

var Geo11={};


   navigator.geolocation.getCurrentPosition(success11,error11, { enableHighAccuracy: true, maximumAge: 5000, timeout: 20000 });



    //Get the latitude and the longitude;
    function success11(position) {
        Geo11.lat = position.coords.latitude;
        Geo11.lng = position.coords.longitude;
       calcRoute(Geo11.lat, Geo11.lng);
    }


            function error11(error){

                            alert('Please check your GPS setting, then reload.');

    }
John
  • 983
  • 1
  • 13
  • 31

1 Answers1

0

Firstly, you have to do proper error handling. See the snippet.

function(error) {
        if(error.code == 0){
        // unknown error
        } else if(error.code == 1) {
        // permission denied
        } else if(error.code == 2) {
        // position unavailable
        } else if(error.code == 3) {
        // timeout
        }
        console.log(error.message);
    },

Secondly, after error handling you would also have to implement reload.

Note: Since GPS is turned on from an OFF state GPS hardware component is (re)booting and in the absence of proper timeout and reload mechanism, it won't work as intended.

That being said. In my opinion in your use case it would be better if you just ask the user for location permission before calling geolocation. There's a very good example here for your reference.

Also, this answer is worth reading.

Community
  • 1
  • 1
Kay_N
  • 987
  • 5
  • 12