问题
I have recently built an HTML5 web app that is designed to notify the user when approaching a set destination. I have set it up to cache so it can be used without web-access in devices with a GPS or AGPS functionality. So, to set the picture, it is a web app that I have cached and added to the homescreen to make it like a native app, but it is really just an HTML5 app (with Javascript).
The app itself seems to be working (insofar as detecting the user's location as it isn't returning any errors as I know it otherwise would), but I have identified a major problem with it; the HTML5 Geolocation functions stop working after a short period of time when the app is no longer in focus (and I know this because when I run it and change to a different app, the geolocation indicator disappears from the iPhone's screen, yet when I have the app in focus, the geolocation icon remains).
I intended for the app to work such that when the user is within 200 meters of the destination, the app's window becomes the focus and an alert is sent to the user informing them they are approaching their destination. Hence it is designed to be working in the background.
The problem is that it seems on the iPhone at least that a web app can only have geolocation access when the window is focused (I assume this is the case, as I went into the location settings on the iPhone and saw that under my app's location settings it was enabled to track location only while the app was in use i.e. when the app was visible on the screen).
What I am looking for is a way (in HTML5 or JS) that would allow a web app to continue monitoring the user's location even when the app is not visible on the screen.
Here is the code I have used to detect the user's location:
function getLocation(){
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});
}
else{
alert("Warning: unable to determine your position!");
}
}
function watchCurrentPosition() {
navigator.geolocation.watchPosition(success_callback, error_callback, {enableHighAccuracy:true});
}
function success_callback(p)
{
return [p.coords.longitude.toFixed(2), p.coords.latitude.toFixed(2)];
}
function error_callback(p)
{
alert("Warning - error!");
}
I am running this on iOS 8.1.3 on an iPhone 6 if that is of any use. Thanks in advance!
回答1:
After doing some further research and testing, I found the following answers:
- Q/A 1: I tested the iFrame method suggested in the first answer, but found it did not work and caused errors to occur as is suggested in the comments
- Q/A 2: The answer here seems to be the case currently.
So as long as the app itself is the current window, it should work it seems, and my testing would agree with that conclusion. Short of making it a full native app, which is a somewhat costly venture for someone like me who isn't a full-time app developer though, it seems impossible. The geolocation features of HTML5 would seem to be more designed for a web app where users can check their locations via a map rather than geolocation monitoring!
来源:https://stackoverflow.com/questions/28576265/allow-html5-web-app-to-access-location-when-browser-isnt-focused-ios