如何使用PhoneGap的跟踪设备位置(iOS和Android)设备 [英] How to track the device location (iOS and Android) device using Phonegap

查看:267
本文介绍了如何使用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)我有非常高萍从我的电脑我的iPhone在同一网络(仍然不知道为什么)。

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

现在我想使用地理定位从PhoneGap的应用程序(在后台运行)悬浮在iOS或者Android中后台运行做到这一点。

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.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

说的第一件事就是创建一个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
});

见<一href="http://docs.phonegap.com/en/2.9.0/cordova_geolocation_geolocation.md.html#Geolocation">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的后台运行,您将需要设置各自的原生开发环境:Eclipse中为Android,X code为iOS。您将无法使用的PhoneGap构建,因为需要在这两种情况下的自定义设置,使其工作。请参阅<一href="http://docs.phonegap.com/en/2.9.0/guide_getting-started_index.md.html#Platform%20Guides">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上的后台运行,您可能需要编写一个定制的Andr​​oid服务,或者你可以做我所做的,并使用PhoneGap的插件来获得部分wakelock(看到这里),以确保您的应用程序在后台运行并接收位置更新,而屏幕是关闭的。需要注意的是这个插件的原始版本外的日期,并且不与PhoneGap的较新版本的工作,也不会支持部分wakelocks。不过,我已经更新和扩展它为我所用:你可以找到源头code它在我的回答<一href="http://stackoverflow.com/questions/17250808/plugin-phonegap-power-management/17251197#17251197">this问题。

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和位置的值中添加键值UIBackgroundModes键,或与您的项目在X code打开,添加所需的背景模式键和一个的应用程序注册了位置更新的价值。这将导致的iOS触发已注册使用watchPosition()被接收的位置更新每次对JS回调函数。 <一href="http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html">See这里所有关于项目的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)设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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