Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop execution of the getCurrentPosition method

I'm using the getCurrentPosition method in Javascript. I would like to implement a button that stops the execution of the method "getCurrentPosition" when it's clicked. I tried with throw/try/catch blocks but it doesn't seem to work :


    try {
        $('#cancel').on("click", function() { 
            $.mobile.loading('hide'); 
            throw "stop";
        });
        navigator.geolocation.getCurrentPosition(foundLocation, noLocation, {enableHighAccuracy: true, timeout : 30000 }); 
    }
    catch ( er ) { alert(er); return false; }

Any ideas? Is it even possible in JS ? I had an idea but I don't know if it's possible to trigger a timeout with a JS method so that the getCurrentPosition breaks ?

like image 532
Laila Avatar asked Apr 11 '13 14:04

Laila


1 Answers

Solution:

Method navigator.geolocation.getCurrentPosition is an asynchronous function so don't count on stopping it just like that.

You should instead use other function called: navigator.geolocation.watchPosition. It works in the same way as getCurrentPosition but what make it usefull in your case is another function called navigator.geolocation.clearWatch and that function is used to stop watchPosition.

Example:

geoLoc = navigator.geolocation;
watchID = geoLoc.watchPosition(showLocation, errorHandler, options);
geoLoc.clearWatch(watchID);
like image 93
Gajotres Avatar answered Sep 17 '22 23:09

Gajotres