With watch I mean using HTML5's native Geolocation watch support. This is meant for a web app, not a native one. Thanks for helping out :D
You cannot watch the user's location from a Web App in background. I have checked it on Chrome (Android M), and as soon as the Web App goes to background the GPS icon disappears and the watch position process is stopped. (Once the Web App is in the foreground again, the watch process is resumed).
The same is happening when the device is locked. The watch process is stopped until the user unlocks it and the Web App is in the foreground.
UPDATE 2021-04
There is a new API in discussion, "Geotracking", that will allow for background tracking. The issue needs votes, though. If you are interested in doing so, go here: https://bugs.chromium.org/p/chromium/issues/detail?id=898536
I've tested the watchPosition
api on android, it seems to work for firefox but not chrome.
UPDATE: - I am using Nexus 5X, with Android N
Yes. You can definitely do watch the location in the background if your mobile browser supports Geolocation API
.
You can use
watchPosition()
function.
watchPosition() Code Snippet
var watchID = navigator.geolocation.watchPosition(function(position) {
do_something(position.coords.latitude, position.coords.longitude);
});
If the position data changes (either by device movement or if more accurate geo information arrives), you can set up a callback function that is called with that updated position information. This is done using the
watchPosition()
function, which has the same input parameters asgetCurrentPosition()
. The callback function is called multiple times, allowing the browser to either update your location as you move, or provide a more accurate location.
getCurrentPosition() Code Snippet
navigator.geolocation.getCurrentPosition(function(position) {
do_something(position.coords.latitude, position.coords.longitude);
});
To get more information, refer Mozilla/Geolocation/WatchCurrentPosition
Hope it helps!
© 2022 - 2024 — McMap. All rights reserved.
watchPosition
was interrupted in this way? Or are we left making statistically-educated guesses on the Δtimestamps from one loop to the next? – Demission