Switching between network and GPS provider(在网络和 GPS 提供商之间切换)
问题描述
我想实现一个 locationListener,它将根据可用性在网络和 GPS 提供商之间切换.
I want to implement a locationListener which will switch between network and GPS providers based on availability.
例如,如果 GPS 未启用,我希望它使用网络,但一旦 GPS 开启,我希望它停止侦听来自网络的位置更新并开始侦听 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 关闭后立即开始监听来自网络的更新.
Similarly I want it to start listening for updates from network as soon as GPS is switched off.
这可能吗?
子问题
GPS 在提供位置定位方面与网络一样快吗?
Is GPS as fast as network in providing a location fix?
推荐答案
当然,您只需获取网络和 GPS 的提供程序,然后将您想要的任何一个传递给 locationManager.requestLocationUpdates()
.
Sure, you just get the providers for the network and GPS and pass whichever you want to locationManager.requestLocationUpdates()
.
当您想停止侦听某个提供程序时,请使用您在 locationManager.requestLocationUpdates()
中指定的侦听器对象调用 locationManager.removeUpdates()
.
When you want to stop listening to a certain provider, call locationManager.removeUpdates()
with the listener object you specified in locationManager.requestLocationUpdates()
.
网络:
Criteria crit = new Criteria();
crit.setPowerRequirement(Criteria.POWER_LOW);
crit.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = locationManager.getBestProvider(crit, false);
全球定位系统:
Criteria crit2 = new Criteria();
crit2.setAccuracy(Criteria.ACCURACY_FINE);
provider2 = locationManager.getBestProvider(crit2, false);
您可以使用 LocationManager.isProviderEnabled() 文档 查看是否启用/禁用了相应的提供程序.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 通常比网络慢得多,因为您必须找到 3 颗以上遥远的卫星等.
GPS is usually much slower than network since you have to find 3+ far-away satellites, etc.
这篇关于在网络和 GPS 提供商之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在网络和 GPS 提供商之间切换
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01