It is possible to watch the location in the backgr

2019-01-20 08:58发布

问题:

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

回答1:

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.



回答2:

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



回答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!