問題描述
我想實(shí)現(xiàn)一個(gè) locationListener,它將根據(jù)可用性在網(wǎng)絡(luò)和 GPS 提供商之間切換.
I want to implement a locationListener which will switch between network and GPS providers based on availability.
例如,如果 GPS 未啟用,我希望它使用網(wǎng)絡(luò),但一旦 GPS 開啟,我希望它停止偵聽來自網(wǎng)絡(luò)的位置更新并開始偵聽 GPS.
For example if GPS is not enabled I want it to use network but as soon as GPS is on then I want it to stop listening for location updates from network and start listening from GPS.
同樣,我希望它在 GPS 關(guān)閉后立即開始監(jiān)聽來自網(wǎng)絡(luò)的更新.
Similarly I want it to start listening for updates from network as soon as GPS is switched off.
這可能嗎?
子問題
GPS 在提供位置定位方面與網(wǎng)絡(luò)一樣快嗎?
Is GPS as fast as network in providing a location fix?
推薦答案
當(dāng)然,您只需獲取網(wǎng)絡(luò)和 GPS 的提供程序,然后將您想要的任何一個(gè)傳遞給 locationManager.requestLocationUpdates()
.
Sure, you just get the providers for the network and GPS and pass whichever you want to locationManager.requestLocationUpdates()
.
當(dāng)您想停止偵聽某個(gè)提供程序時(shí),請使用您在 locationManager.requestLocationUpdates()
中指定的偵聽器對象調(diào)用 locationManager.removeUpdates()
.
When you want to stop listening to a certain provider, call locationManager.removeUpdates()
with the listener object you specified in locationManager.requestLocationUpdates()
.
網(wǎng)絡(luò):
Criteria crit = new Criteria();
crit.setPowerRequirement(Criteria.POWER_LOW);
crit.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = locationManager.getBestProvider(crit, false);
全球定位系統(tǒng):
Criteria crit2 = new Criteria();
crit2.setAccuracy(Criteria.ACCURACY_FINE);
provider2 = locationManager.getBestProvider(crit2, false);
您可以使用 LocationManager.isProviderEnabled() 文檔 查看是否啟用/禁用了相應(yīng)的提供程序.LocationManager 文檔中提供了更多信息.
You can use LocationManager.isProviderEnabled() doc to see if the appropriate provider is enabled/disabled. There's more info available in the LocationManager docs.
GPS 通常比網(wǎng)絡(luò)慢得多,因?yàn)槟仨氄业?3 顆以上遙遠(yuǎn)的衛(wèi)星等.
GPS is usually much slower than network since you have to find 3+ far-away satellites, etc.
這篇關(guān)于在網(wǎng)絡(luò)和 GPS 提供商之間切換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!