浏览器未聚焦时允许HTML5网络应用访问位置(iOS) [英] Allow HTML5 web app to access location when browser isn't focused (iOS)

查看:97
本文介绍了浏览器未聚焦时允许HTML5网络应用访问位置(iOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近建立了一个HTML5网页应用程式,用于在接近设定目的地时通知使用者。我已经将其设置为缓存,以便在具有GPS或AGPS功能的设备中无需网页访问即可使用。因此,为了设置图片,它是一个我已经缓存并添加到主屏幕的网络应用程序,使它像原生应用程序一样,但它实际上只是一个HTML5应用程序(带有Javascript)。



该应用程序本身似乎正在工作(因为检测用户的位置,因为我不知道它会返回任何错误),但我已经确定了一个主要问题; HTML5地理定位功能在短时间内停止工作,当应用程序不再焦点时(我知道这是因为当我运行它并更改为不同的应用程序时,地理定位指示器从iPhone的屏幕上消失,但是当我该应用程序的焦点,地理位置图标仍然存在)。

我打算让应用程序工作,以便当用户距离目的地200米内时,应用程序的窗口成为焦点,并向用户发送警报他们正在接近他们的目的地。因此它被设计成在后台工作。

问题是,至少在iPhone上看起来,一个Web应用程序只能在窗口关注时才能进行地理定位访问(我认为这是事实,因为我进入iPhone的位置设置,看到在我的应用程序的位置设置下,它只能在应用程序正在使用时跟踪位置,即当应用程序在屏幕上可见时)。

我在寻找的是一种方式(在HTML5或JS中),即使应用程序不可见时,Web应用程序仍可继续监视用户的位置屏幕



以下是我用来检测用户位置的代码:

<$ p $函数getLocation(){
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});
}
else {
alert(警告:无法确定你的位置!);


函数watchCurrentPosition(){
navigator.geolocation.watchPosition(success_callback,error_callback,{enableHighAccuracy:true});


函数success_callback(p)
{
return [p.coords.longitude.toFixed(2),p.coords.latitude.toFixed(2) ]。
}

函数error_callback(p)
{
alert(Warning - error!);
}

如果是iPhone 6上的iOS 8.1.3,任何用途。

解决方案

在做了一些进一步的研究和测试之后,我发现了以下答案:


  1. Q / A 1 :我测试了第一个答案中建议的iFrame方法,但发现它不起作用并导致错误发生,正如评论中所建议的那样

  2. 问题2 :这里的答案似乎是目前的情况。

因此,只要应用程序本身就是当前窗口,应该似乎工作,我的测试会同意这个结论。尽管它并不是一个完整的本地应用程序,但对于像我这样并不是全职应用程序开发人员的人来说,这是一笔代价昂贵的冒险,但似乎不可能。 HTML5的地理位置功能似乎更适合Web应用程序,用户可以通过地图而不是地理位置监控来检查他们的位置!


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!

解决方案

After doing some further research and testing, I found the following answers:

  1. 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
  2. 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!

这篇关于浏览器未聚焦时允许HTML5网络应用访问位置(iOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆