我已经在PhoneGap中开发了一个应用程序,它通过地理定位API跟踪您的行进路线,我通过 navigator.geolocation.getCurrentPosition(onSuccess, onError);
获取设备的当前位置,并通过 navigator.geolocation.watchPosition(function(position)
定期间隔后获取位置来更新折线但现在问题是's not working in background i.e (when I open another application and it starts running in background ) it doesn't更新折线意味着不会触发这个 navigator.geolocation.watchPosition(function(position)
函数,所以我尝试通过 Cordova-plugin-background-mode
这个插件运行应用程序它开始在后台运行应用程序再次没有触发 navigator.geolocation.watchPosition(function(position)
函数意味着这不是't required i.e (there wasn' t任何需要安装插件才能运行在后台模式下的应用程序) . 所以请帮我解决在后台模式下获取位置的问题 . 如果我使用这个 Cordova-plugin-mauron85-background-geolocation
插件,我可以获得一个位置,因为我通过观察位置?如果应用程序在前台运行该怎么办?我是否必须使用 watchPosition
功能或其他功能?
2 回答
你可能想深入研究这个location plugin . 他们似乎认为他们已经解决了这个问题 .
快速fiddle和我可以访问的手机上的测试显示这不是一个电话空白问题 . 这是一个操作系统问题 . 大多数手机都经过优化,可以暂停后台活动,尤其是网页浏览器活动 . 最好结合本机事件来获得您想要的行为 .
我注意到即使是后台任务,手机也会保持所有连接都打开 . 您可以通过对服务器进行ajax调用来解决这种情况,该服务器不会拒绝,但无法响应,添加超时处理程序,并设置所需的时间间隔,在ajax超时处理程序中检查位置然后设置一个新的电话 .
祝好运!
有一个
cordova-plugin-background-mode
样本: