It is possible to watch the location in the background on Mobile (iOS / Android)?
Asked Answered
R

4

21

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

Rustle answered 25/11, 2015 at 20:52 Comment(0)
J
18

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.

Jumna answered 16/3, 2016 at 17:15 Comment(1)
Is there any (deterministic) way to tell or record when the watchPosition was interrupted in this way? Or are we left making statistically-educated guesses on the Δtimestamps from one loop to the next?Demission
J
3

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

Jeseniajesh answered 24/4, 2021 at 7:47 Comment(0)
N
0

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

Nigelniger answered 16/12, 2016 at 20:47 Comment(0)
C
-3

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 as getCurrentPosition(). 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!

Charwoman answered 25/11, 2015 at 21:10 Comment(3)
Thanks for answering, and this will work even if the device is locked and in someones pocket?Rustle
@Rustle Honestly I have not tested this use case. I am afraid that you will not be able to retrieve the updated position when the device is locked. Do let us know here if you test this use case!Charwoman
Incorrect answer, geoocation does not work for cached (background) processes on Android.Susquehanna

© 2022 - 2024 — McMap. All rights reserved.