Hemantsom
Hemantsom

Reputation: 553

How to stop watchPosition() Position retrieval timed out error?

I am developing a phonegap application that uses the current location of the device in its functionalities but when tested on Iphone the App give an error message every 60 seconds as follows: enter image description here

The Source code I am using to get the location is:

function onDeviceReady() {

    var options = { maximumAge: 0, timeout: 10000,enableHighAccuracy: false };
    var watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);

    function onSuccess(position) {
        if (position.coords.latitude) {
            sessionStorage.setItem("appLatitude", position.coords.latitude);
            sessionStorage.setItem("appLongitude", position.coords.longitude);
        }
        //alert(sessionStorage.getItem("appLatitude") + "&" + sessionStorage.getItem("appLongitude"));
    }

    function onError(error) {
        z.appNotification.getAlert('code: ' + error.code + '\n' +
                'message: ' + error.message + '\n');
    }

}

How can stop that error message from prompting?

Upvotes: 2

Views: 3250

Answers (1)

Dawson Loudon
Dawson Loudon

Reputation: 6029

If you only want to get the location once use:

navigator.geolocation.getCurrentPosition(onSuccess, onError);

If you want to stop watching for any reason, use:

navigator.geolocation.clearWatch(watchID);

Upvotes: 3

Related Questions