Stop geolocation.watchPosition when pause event fires (Android)

111 views
Skip to first unread message

Vito Samson

unread,
May 16, 2014, 6:51:13 PM5/16/14
to phon...@googlegroups.com
I'm trying to get my app to stop watching the geolocation when the app goes into the background. Here's my code:

document.addEventListener("pause", stopGeo, false);

document.addEventListener("resume", startGeo, false);

var geo_success = function(pos) {
localStorage.setItem('latitude', pos.coords.latitude);
localStorage.setItem('longitude', pos.coords.longitude);
localStorage.setItem('accuracy', pos.coords.accuracy);
console.log('got geo');
};
var geo_error = function(err) {
console.warn("GEO ERROR(" + err.code + "): " + err.message);
};
var geo_options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 10000
};

var igniteGeo;
function startGeo() {
igniteGeo = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
}

function stopGeo() {
navigator.geolocation.clearWatch(igniteGeo);
}

This isn't working. When the app goes into the background, I continue to see the GPS icon in the phone's top bar, and the console continues to show "got geo". Am I doing something wrong here?
Reply all
Reply to author
Forward
0 new messages