微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Android LocationManager标准

我需要从网络和GPS提供商处接收位置更改.

如果GPS提供商不可用或没有位置(卫星可见性差)我会从网络提供商处获得GPS提供商的位置.

是否有可能根据我的需要选择使用标准?

解决方法

实际上 Android Developers – Making Your App Location Aware一个很好的示例代码来满足您的需求.

在其代码中,如果您使用两个提供商(来自GPS和网络),它将进行比较:

...
} else if (mUseBoth) {
    // Get coarse and fine location updates.
    mFineProviderButton.setBackgroundResource(R.drawable.button_inactive);
    mBothProviderButton.setBackgroundResource(R.drawable.button_active);
    // Request updates from both fine (gps) and coarse (network) providers.
    gpsLocation = requestUpdatesFromProvider(
            LocationManager.GPS_PROVIDER,R.string.not_support_gps);
    networkLocation = requestUpdatesFromProvider(
            LocationManager.NETWORK_PROVIDER,R.string.not_support_network);

    // If both providers return last kNown locations,compare the two and use the better
    // one to update the UI.  If only one provider returns a location,use it.
    if (gpsLocation != null && networkLocation != null) {
        updateUILocation(getBetterLocation(gpsLocation,networkLocation));
    } else if (gpsLocation != null) {
        updateUILocation(gpsLocation);
    } else if (networkLocation != null) {
        updateUILocation(networkLocation);
    }
}
...

它实现了最佳位置提供者的想法(通过确定在指定时间段内的准确性,如:

/** Determines whether one Location reading is better than the current Location fix.
  * Code taken from
  * http://developer.android.com/guide/topics/location/obtaining-user-location.html
  *
  * @param newLocation  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix,to which you want to compare the new
  *        one
  * @return The better Location object based on recency and accuracy.
  */
protected Location getBetterLocation(Location newLocation,Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return newLocation;
    }

    // Check whether the new location fix is newer or older
    long timedelta = newLocation.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timedelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timedelta < -TWO_MINUTES;
    boolean isNewer = timedelta > 0;

    // If it's been more than two minutes since the current location,use the new location
    // because the user has likely moved.
    if (isSignificantlyNewer) {
        return newLocation;
    // If the new location is more than two minutes older,it must be worse
    } else if (isSignificantlyOlder) {
        return currentBestLocation;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(newLocation.getProvider(),currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return newLocation;
    } else if (isNewer && !isLessAccurate) {
        return newLocation;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return newLocation;
    }
    return currentBestLocation;
}

有关完整代码,请查看上面提供的链接.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息 by:授客 QQ:1033553122 1、 查看内存信息 1)查看所有内存信息 命令: dumpsys meminfo 例: C:\Users\laiyu&gt;adb shell shell@android:/ $
Monkey Android app稳定性测试工具之Monkey使用教程 by:授客 QQ:1033553122 由于篇幅问题,仅提供百度网盘下载链接: Android app稳定性测试工具之Monkey使用教程.pdf
Android 常见adb命令 by:授客 QQ:1033553122 1、 查看所有已链接的设备 命令: adb devices 例: C:\Users\laiyu&gt;adb devices List of devices attached 5d3b5aac device 设备命令 2、 复制
这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android岛屿数量算...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Andro...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android数据压缩的方法是什么”文章能帮助大家解决疑惑...
这篇“Android怎么使用Intent传大数据”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅...