Problem with Watchposition and HTML5 setInterval

0

I have the following code with which I can obtain the coordinates of a telephone every 15 seconds.

The problem is that if the phone is blocked, the setInterval function stops working and it does not save the coordinates until after unlocking the phone.

function setGeolocation() {
var geolocation = window.navigator.geolocation.watchPosition( 
    function ( position ) {
        var location=new google.maps.LatLng(position.coords.latitude,position.coords.longitude)
  coordenadasRuta.push(location);
    },
    function () { /*error*/ }, {
        maximumAge: 250, 
        enableHighAccuracy: true
    } 
);

window.setTimeout( function () {
        window.navigator.geolocation.clearWatch( geolocation ) 
    }, 
    5000
   );
}; 

setGeolocation();
window.setInterval( function () { 
    setGeolocation();
}, 15000);

Those coordinates then I save them to create a PolyLine so if it crashes it does not save my route correctly.

Do you have any idea why this happens?

    
asked by Gerardo Merida 08.03.2017 в 18:06
source

1 answer

0

The problem is that the Android system of the mobile is turning off the process to save battery by default as I assume that you access from a browser I do not go as forced to keep it in memory, but there are several post with information on how to keep Android apps in background and optimize memory

    
answered by 08.03.2017 в 20:41