Android获取最佳位置
我想要用户的确切位置。因此,我创建了两个侦听器:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGPSListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mNetworkListener);
我正在侦听来自网络和 GPS 提供商的位置更新,并希望检查两者获得的位置的准确性,以便我可以选择准确的位置。我只是想问我是否使用正确的方法来实现这一目标......???如果没有,请提供一些指导如何做到这一点......???
我的示例代码是:
if(MyGPSListener.currentLocation != null) {
if(MyNetworkListener.currentLocation != null) {
if(MyGPSListener.currentLocation.getAccuracy() <= MyNetworkListener.currentLocation.getAccuracy()) {
useGPSLocation();
}
else if(MyGPSListener.currentLocation.getAccuracy() > MyNetworkListener.currentLocation.getAccuracy()) {
useNetworkLocation();
}
}
else {
useGPSLocation();
}
}
else if(MyNetworkListener.currentLocation != null){
useNetworkLocation();
}
I want the exact location of user. So I created two listeners as:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGPSListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mNetworkListener);
I am listening for location updates from both the network and gps providers and wanted to check the accurary of locations obtained by both so that I can pick the accurate location. I just want to ask am I using the right way of acheiving that...??? if not, please provide some guideline how to do this...???
My example code is:
if(MyGPSListener.currentLocation != null) {
if(MyNetworkListener.currentLocation != null) {
if(MyGPSListener.currentLocation.getAccuracy() <= MyNetworkListener.currentLocation.getAccuracy()) {
useGPSLocation();
}
else if(MyGPSListener.currentLocation.getAccuracy() > MyNetworkListener.currentLocation.getAccuracy()) {
useNetworkLocation();
}
}
else {
useGPSLocation();
}
}
else if(MyNetworkListener.currentLocation != null){
useNetworkLocation();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该查看:http://developer.android。 com/guide/topics/location/obtaining-user-location.html
特别是函数:isBetterLocation
我可能会使用类似 isBetterLocation 的东西。因为了解位置更新的时间实际上非常重要。至少如果你真的想知道用户在哪里而不是用户在哪里。
但是您肯定可以在您描述的自己的函数中实现时间检查。
You should take a look at: http://developer.android.com/guide/topics/location/obtaining-user-location.html
Especially the function: isBetterLocation
I would probably use something like the isBetterLocation. Since it's actually quite important to see how old the location updates are. At least if you really want to know where the user is know and not where the user were.
But you can surely implement a time check in your own function which you described.
您可以使用 https://github.com/balwinderSingh1989/androidBestLocationTracker
Android Best Location Tracker 是一个 Android 库它可以帮助您通过名为 BaseLocationStrategy 的对象获得用户最佳位置,该对象将使用精度算法提供准确的位置。
它增加了对最新 Android api 级别的支持,并且可以轻松在后台获取位置。
使用方法请参考项目的“READ ME”。
You can use https://github.com/balwinderSingh1989/androidBestLocationTracker
Android Best Location Tracker is an Android library that helps you get user best location with a object named BaseLocationStrategy that would give a accurate location using accuracy alogrithm.
It has added support till latest android api levels and can fetch location in background with ease.
For usage, refer "READ ME" of project.
你可以查看Google的官方文档。
定义最佳性能模型
并验证位置的准确性修复:
You can check Google's offical document.
Define a model for the best performance
And Validate the accuracy of a location fix: