問題描述
我想知道用戶何時到達或離開某個位置.我嘗試使用無線網絡(檢查移動設備)來執行此操作,但由于多種原因我無法做到.
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)設備的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!