Android获取最佳位置

发布于 2024-12-06 16:52:33 字数 1014 浏览 1 评论 0原文

我想要用户的确切位置。因此,我创建了两个侦听器:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混浊又暗下来 2024-12-13 16:52:33

您应该查看: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.

但可醉心 2024-12-13 16:52:33

您可以使用 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.

枕头说它不想醒 2024-12-13 16:52:33

你可以查看Google的官方文档。
定义最佳性能模型

并验证位置的准确性修复:

  1. 检查检索到的位置是否比之前估计的位置新得多。
  2. 检查该位置声称的精度是否比之前的估计更好或更差。
  3. 检查新位置来自哪个提供商,并确定您是否更信任它。

输入图片此处描述

You can check Google's offical document.
Define a model for the best performance

And Validate the accuracy of a location fix:

  1. Check if the location retrieved is significantly newer than the previous estimate.
  2. Check if the accuracy claimed by the location is better or worse than the previous estimate.
  3. Check which provider the new location is from and determine if you trust it more.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文