問(wèn)題描述
我想知道用戶何時(shí)到達(dá)或離開(kāi)某個(gè)位置.我嘗試使用無(wú)線網(wǎng)絡(luò)(檢查移動(dòng)設(shè)備)來(lái)執(zhí)行此操作,但由于多種原因我無(wú)法做到.
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) 我需要實(shí)時(shí)更新或每 1 到 5 分鐘更新一次有關(guān)哪些設(shè)備已連接以及哪些設(shè)備剛剛斷開(kāi)連接的信息.
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) 我在同一網(wǎng)絡(luò)上從我的 PC 到我的 iPhone 的 ping 非常高(仍然不知道為什么).
2) I had very high ping from my PC to my iPhone on the same network (still don't know why).
現(xiàn)在我想使用在 iOS 上暫停或在 Android 中在后臺(tái)運(yùn)行的 Phonegap 應(yīng)用程序(在后臺(tái)運(yùn)行)的地理定位.
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 歲,相對(duì)于當(dāng)前的移動(dòng)操作系統(tǒng)版本已經(jīng)過(guò)時(shí).例如,部分喚醒鎖不再適用于現(xiàn)代 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.
今天我的建議是(如果你有一個(gè)嚴(yán)肅的商業(yè)應(yīng)用程序而不是一個(gè)愛(ài)好項(xiàng)目)使用付費(fèi)版本的 cordova-background-geolocation
插件 by 晶體管軟件.免費(fèi)版適用于 iOS,但對(duì)于 Android 則需要許可證,費(fèi)用約為數(shù)百美元.但是我認(rèn)為這個(gè)價(jià)格是值得的:在我 10 多年創(chuàng)建位置感知移動(dòng)應(yīng)用程序的經(jīng)驗(yàn)中,這是我遇到的最復(fù)雜和最可靠的解決方案.只需支付許可證費(fèi)用,您就可以訪問(wèn)不斷更新和維護(hù)的私有存儲(chǔ)庫(kù),以兼容新版本的 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.
如果您正在尋找免費(fèi)/開(kāi)源解決方案,我會(huì)選擇 cordova-plugin-background-geolocation 這是 Transistorsoft 原始插件的開(kāi)源分支.然而,我對(duì)這個(gè)插件的體驗(yàn)好壞參半;由于是免費(fèi)/開(kāi)源的,它沒(méi)有像付費(fèi)的 Transistorsoft 插件那樣頻繁更新.由于最近版本的 Android 中對(duì)后臺(tái)位置的新的更嚴(yán)格的限制,我遇到了問(wèn)題,這需要很長(zhǎng)時(shí)間才能解決或根本沒(méi)有得到修復(fù)(請(qǐng)參閱插件的 問(wèn)題列表).
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 日)
首先要說(shuō)的是,創(chuàng)建一個(gè)在后臺(tái)運(yùn)行時(shí)接收位置更新的 Phonegap 應(yīng)用程序是完全可能的,但并非易事.我自己完成了,并在 Android 和 iOS 平臺(tái)上發(fā)布了應(yīng)用程序.
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.
如果您需要準(zhǔn)確且定期的位置更新,我建議您在目標(biāo)設(shè)備上使用 GPS 接收器.在 Phonegap 中,您可以在請(qǐng)求位置更新時(shí)設(shè)置highAccuracy"標(biāo)志.watchPosition() 函數(shù)將在設(shè)備接收到來(lái)自 GPS 接收器的更新時(shí)提供新的位置信息,因此您可以像這樣使用它:
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
});
請(qǐng)參閱 Phonegap 地理位置 API 文檔了解更多詳細(xì)信息(請(qǐng)注意在 Android 和 iOS 上進(jìn)行此操作所需的權(quán)限).
See the Phonegap geolocation API documentation for more details (note the permissions that are required to make this work on Android and iOS).
要讓您的應(yīng)用在 Android 或 iOS 上的后臺(tái)運(yùn)行,您需要設(shè)置各自的原生開(kāi)發(fā)環(huán)境:適用于 Android 的 Eclipse,適用于 iOS 的 XCode.您將無(wú)法使用 Phonegap Build,因?yàn)樵谶@兩種情況下都需要自定義設(shè)置才能使其正常工作.請(qǐng)參閱 Phonegap 平臺(tái)指南了解如何做到這一點(diǎn).
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.
要讓您的應(yīng)用在 Android 后臺(tái)運(yùn)行,您需要編寫自定義 Android 服務(wù),或者您可以像我一樣使用 Phonegap 插件獲取部分喚醒鎖"(看這里) 讓您的應(yīng)用程序在后臺(tái)運(yùn)行并在屏幕關(guān)閉時(shí)接收位置更新.請(qǐng)注意,此插件的原始版本已過(guò)時(shí)-date 并且不適用于更新版本的 Phonegap,也不支持部分喚醒鎖.但是,我已經(jīng)對(duì)其進(jìn)行了更新和擴(kuò)展以供我自己使用:您可以在我對(duì) 這個(gè)問(wèn)題.
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.
為了讓您的應(yīng)用在 iOS 上保持在后臺(tái)運(yùn)行,您需要做些不同的事情;您不需要插件,只需一個(gè)項(xiàng)目設(shè)置.
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.
您可以手動(dòng)編輯項(xiàng)目 .plist 并添加值為location"的鍵UIBackgroundModes"鍵,或者在 XCode 中打開(kāi)項(xiàng)目后,添加值為App"的Required Background Modes"鍵注冊(cè)位置更新".這將導(dǎo)致 iOS 在每次收到位置更新時(shí)觸發(fā)您使用 watchPosition() 注冊(cè)的 JS 回調(diào)函數(shù).查看這里了解更多關(guān)于iOS 項(xiàng)目密鑰和后臺(tái)模式.
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.
希望這會(huì)有所幫助!
這篇關(guān)于如何使用 Phonegap 跟蹤設(shè)備位置(iOS 和 Android)設(shè)備的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!