如何使用 Phonegap 跟踪设备位置(iOS 和 Android)设备

时间:2023-03-22
本文介绍了如何使用 Phonegap 跟踪设备位置(iOS 和 Android)设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想知道用户何时到达或离开某个位置.我尝试使用无线网络(检查移动设备)来执行此操作,但由于多种原因我无法做到.

I would like to know when a user arrives or leaves certain location. I was trying to do this using the wireless network (check for mobile devices), but I couldn't for several reasons.

1) 我需要实时更新或每 1 到 5 分钟更新一次有关哪些设备已连接以及哪些设备刚刚断开连接的信息.

1) I needed real time updates or every 1 - 5 min of the information about which devices are connected and which devices have just disconnected.

2) 我在同一网络上从我的 PC 到我的 iPhone 的 ping 非常高(仍然不知道为什么).

2) I had very high ping from my PC to my iPhone on the same network (still don't know why).

现在我想使用在 iOS 上暂停或在 Android 中在后台运行的 Phonegap 应用程序(在后台运行)的地理定位.

Now I want to do it using geolocation from a Phonegap application (running in the background) suspended on iOS or running in the background in Android.

任何帮助将不胜感激.

推荐答案

2019 年 5 月 25 日更新

我下面的原始答案是 6 岁,相对于当前的移动操作系统版本已经过时.例如,部分唤醒锁不再适用于现代 Android 版本.

My original answer below is 6 years old and out-of-date with respect to current mobile OS versions. For example partial wakelocks no longer work on modern Android versions.

今天我的建议是(如果你有一个严肃的商业应用程序而不是一个爱好项目)使用付费版本的 cordova-background-geolocation 插件 by 晶体管软件.免费版适用于 iOS,但对于 Android 则需要许可证,费用约为数百美元.但是我认为这个价格是值得的:在我 10 多年创建位置感知移动应用程序的经验中,这是我遇到的最复杂和最可靠的解决方案.只需支付许可证费用,您就可以访问不断更新和维护的私有存储库,以兼容新版本的 Android 和iOS.

Today my recommendation would be (if you have a serious commercial app and not a hobby project) to use the paid-for version of the cordova-background-geolocation plugin by Transistorsoft. The free version works for iOS but for Android a license is required which costs in the order of several hundred US dollars. However I think the price is worth it: in my 10+ years of experience in creating location-aware mobile apps, this has been the most sophisiticated and reliable solution I've encountered. For the cost of your license, you get access to the private repo which is continually updated and maintained to be compatible with new versions of Android & iOS.

如果您正在寻找免费/开源解决方案,我会选择 cordova-plugin-background-geolocation 这是 Transistorsoft 原始插件的开源分支.然而,我对这个插件的体验好坏参半;由于是免费/开源的,它没有像付费的 Transistorsoft 插件那样频繁更新.由于最近版本的 Android 中对后台位置的新的更严格的限制,我遇到了问题,这需要很长时间才能解决或根本没有得到修复(请参阅插件的 问题列表).

If you're looking for a free/open-source solution, I would go with cordova-plugin-background-geolocation which is an open-source fork of the original plugin by Transistorsoft. However my experience with this plugin has been of mixed success; due to being free/open-source, it's not updated as frequently as the paid-for Transistorsoft plugin. I encountered problems due to new more stringent restrictions on background location in recent versions of Android which took a significant time to be resolved or have not been fixed at all (see the plugin's issue list).

原始答案(2013 年 6 月 11 日)

首先要说的是,创建一个在后台运行时接收位置更新的 Phonegap 应用程序是完全可能的,但并非易事.我自己完成了,并在 Android 和 iOS 平台上发布了应用程序.

The first thing to say is that creating a Phonegap app that receives location updates while running in the background is entirely possible, but not trivial. I've done it myself and released apps on both the Android and iOS platforms.

如果您需要准确且定期的位置更新,我建议您在目标设备上使用 GPS 接收器.在 Phonegap 中,您可以在请求位置更新时设置highAccuracy"标志.watchPosition() 函数将在设备接收到来自 GPS 接收器的更新时提供新的位置信息,因此您可以像这样使用它:

If you need accurate and regular position updates, I'd suggest using the GPS receiver on the target devices. In Phonegap, you can do this setting the "highAccuracy" flag when requesting position updates. The watchPosition() function will deliver new position information as and when the device receives updates from the GPS receiver, so you use it something like this:

navigator.geolocation.watchPosition(successCallback, errorCallback, {
  enableHighAccuracy: true,
  timeout: 10000,
  maximumAge: 0
});

请参阅 Phonegap 地理位置 API 文档了解更多详细信息(请注意在 Android 和 iOS 上进行此操作所需的权限).

See the Phonegap geolocation API documentation for more details (note the permissions that are required to make this work on Android and iOS).

要让您的应用在 Android 或 iOS 上的后台运行,您需要设置各自的原生开发环境:适用于 Android 的 Eclipse,适用于 iOS 的 XCode.您将无法使用 Phonegap Build,因为在这两种情况下都需要自定义设置才能使其正常工作.请参阅 Phonegap 平台指南了解如何做到这一点.

To keep your app running in the background on either Android or iOS you will need to setup their respective native development environments: Eclipse for Android, XCode for iOS. You will not be able to use Phonegap Build because custom settings are needed in both cases to make it work. See the Phonegap platform guides for how to do this.

要让您的应用在 Android 后台运行,您需要编写自定义 Android 服务,或者您可以像我一样使用 Phonegap 插件获取部分唤醒锁"(看这里) 让您的应用程序在后台运行并在屏幕关闭时接收位置更新.请注意,此插件的原始版本已过时-date 并且不适用于更新版本的 Phonegap,也不支持部分唤醒锁.但是,我已经对其进行了更新和扩展以供我自己使用:您可以在我对 这个问题.

To keep your app running in the background on Android, you either need to write a custom Android service, or you could do what I did and use a Phonegap plugin to acquire a "partial wakelock" (see here) to keep your app running in the background and receive position updates while the screen is off. Note that the original version of this plugin is out-of-date and doesn't work with more recent versions of Phonegap, and also doesn't support partial wakelocks. However, I've updated and extended it for my own use: you can find the source code for it in my answer to this question.

为了让您的应用在 iOS 上保持在后台运行,您需要做些不同的事情;您不需要插件,只需一个项目设置.

To keep your app running in the background on iOS, you need to do things slightly differently; you don't need a plugin, just a project setting.

您可以手动编辑项目 .plist 并添加值为location"的键UIBackgroundModes"键,或者在 XCode 中打开项目后,添加值为App"的Required Background Modes"键注册位置更新".这将导致 iOS 在每次收到位置更新时触发您使用 watchPosition() 注册的 JS 回调函数.查看这里了解更多关于iOS 项目密钥和后台模式.

You can either manually edit the project .plist and add the key "UIBackgroundModes" key with a value of "location" or, with your project open in XCode, add the "Required Background Modes" key with a value of "App registers for location updates". This will cause iOS to trigger the JS callback function you have registered with watchPosition() each time a location update is received. See here for more about iOS project keys and background modes.

希望这会有所帮助!

这篇关于如何使用 Phonegap 跟踪设备位置(iOS 和 Android)设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:融合的位置提供者似乎没有使用 GPS 接收器 下一篇:如何在Android中根据与当前位置的距离对地理点进行排序

相关文章