简体   繁体   English

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

[英]How to track the device location (iOS and Android) device using Phonegap

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) I needed real time updates or every 1 - 5 min of the information about which devices are connected and which devices have just disconnected. 1)我需要实时更新或每隔1到5分钟有关哪些设备已连接以及哪些设备刚刚断开连接的信息。

2) I had very high ping from my PC to my iPhone on the same network (still don't know why). 2)我在同一个网络上从我的电脑到我的iPhone有很高的ping(仍然不知道为什么)。

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. 现在我想使用在iOS上暂停或在Android后台运行的Phonegap应用程序(在后台运行)中使用地理定位来实现它。

Any help would be appreciated. 任何帮助,将不胜感激。

Update 25 May 2019 2019年5月25日更新

My original answer below is 6 years old and out-of-date with respect to current mobile OS versions. 我在下面的原始答案是6岁,与当前的移动操作系统版本相比已经过时了。 For example partial wakelocks no longer work on modern Android versions. 例如,部分唤醒锁不再适用于现代Android版本。

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 . 今天我的推荐是(如果你有一个严肃的商业应用而不是业余爱好项目)使用Transistorsoftcordova-background-geolocation插件的付费版本。 The free version works for iOS but for Android a license is required which costs in the order of several hundred US dollars. 免费版适用于iOS,但对于Android,需要许可证,其成本约为数百美元。 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. 不过我认为价格是值得的:在我10年多的创建位置感知移动应用程序的经验中,这是我遇到的最复杂,最可靠的解决方案。 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. 对于许可证的费用,您可以访问不断更新和维护的私人仓库,以与新版本的Android和iOS兼容。

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. 如果您正在寻找免费/开源解决方案,我会选择cordova-plugin-background-geolocation ,它是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. 由于是免费/开源的,它的更新频率不如付费的Transistorsoft插件。 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 ). 我遇到了一些问题,因为Android最近版本中对背景位置有了更严格的限制,需要很长时间才能解决或者根本没有修复(请参阅插件的问题列表 )。

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

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. 首先要说的是,创建一个在后台运行时接收位置更新的Phonegap应用程序是完全可能的,但不是微不足道的。 I've done it myself and released apps on both the Android and iOS platforms. 我自己做了,并在Android和iOS平台上发布了应用程序。

If you need accurate and regular position updates, I'd suggest using the GPS receiver on the target devices. 如果您需要准确和定期更新位置,我建议您在目标设备上使用GPS接收器。 In Phonegap, you can do this setting the "highAccuracy" flag when requesting position updates. 在Phonegap中,您可以在请求位置更新时设置“highAccuracy”标志。 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: watchPosition()函数将在设备从GPS接收器接收更新时提供新的位置信息,因此您可以使用以下内容:

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

See the Phonegap geolocation API documentation for more details (note the permissions that are required to make this work on Android and iOS). 有关详细信息,请参阅Phonegap geolocation API文档 (请注意在Android和iOS上使其工作所需的权限)。

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. 要让您的应用在Android或iOS上在后台运行,您需要设置各自的本机开发环境: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. 您将无法使用Phonegap Build,因为在这两种情况下都需要自定义设置才能使其正常工作。 See the Phonegap platform guides for how to do this. 有关如何执行此操作,请参阅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. 要让您的应用在Android上保持在后台运行,您需要编写自定义Android服务,或者您可以执行我所做的操作并使用Phonegap插件获取“部分唤醒锁”( 请参阅此处 )以保持您的应用在屏幕关闭时,背景和接收位置更新。 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. 请注意,此插件的原始版本已过时,不适用于更新版本的Phonegap,也不支持部分唤醒锁。 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 . 但是,我已经更新并扩展它供我自己使用:你可以在我对这个问题的回答中找到它的源代码。

To keep your app running in the background on iOS, you need to do things slightly differently; 要让您的应用在iOS上的后台运行,您需要做的事情略有不同; you don't need a plugin, just a project setting. 你不需要一个插件,只需要一个项目设置。

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". 您可以手动编辑项目.plist并添加键“UIBackgroundModes”键,其值为“location”,或者在XCode中打开项目,添加“Required Background Modes”键,其值为“App register for location”更新”。 This will cause iOS to trigger the JS callback function you have registered with watchPosition() each time a location update is received. 这将导致iOS在每次收到位置更新时触发您使用watchPosition()注册的JS回调函数。 See here for more about iOS project keys and background modes . 有关iOS项目键和背景模式的更多信息, 请参见此处

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MDM解决方案如何跟踪android设备位置? - How MDM solutions track android device location? 使用Phonegap的Android设备视频 - Android device videos using Phonegap 在Android和iOS上都没有定义Phonegap设备 - Phonegap device is undefined on both android and iOS 如何扩展MapActivity代码以根据Android Studio中的设备ID或登录凭据和GPS位置跟踪另一个设备的位置 - How to extend MapActivity Code to track Location of Another Device on Basis of Device ID Or Login Credentials and GPS Location in Android Studio 如何在Android中使用Phonegap处理Jquery mobile的设备后退按钮? - How to handle device back button for Jquery mobile using Phonegap in Android? 如何使用Phonegap和jQuery Mobile在Android设备中存储cookie值? - How to store cookies values in an Android device using Phonegap and jQuery Mobile? 如何按字母顺序对使用phonegap从android设备获取的联系人进行排序? - How to order the contact alphabetically that are fetched from android device using phonegap? PhoneGap无法在Android设备上运行 - PhoneGap not Working on Android device phonegap android中的设备宽度 - Device width in phonegap android 如何在建筑物中跟踪Android设备的位置? - How to track position of an Android device in a building?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM