Android LocationManager 标准

Posted

技术标签:

【中文标题】Android LocationManager 标准【英文标题】:Android LocationManager Criteria 【发布时间】:2013-03-18 17:39:13 【问题描述】:

我需要接收来自网络和 GPS 提供商的位置更改。

如果 GPS 供应商不可用或没有位置(卫星能见度差),我会从网络供应商那里收到位置,否则从 GPS 供应商那里获得位置。

是否可以根据我的需要使用标准选择提供者?

【问题讨论】:

如果您使用 Criteria 类根据配置查找可用的最佳提供程序,那么将导致来自 GPS PROVIDER、NETWORK PROVIDER 或 PASSIVE 的任何人。在没有 Criteria 类的情况下,使用 requestLocationUpdates 获取与提供者一致的位置 LocationManager 不会在一次回调中通知我有关网络和 GPS 的信息?我需要使用两个具有不同回调的 requestLocation() 吗? 要注册您的位置经理以获取更新,您必须提供一个提供商,您想从该提供商那里获取更新。喜欢mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10 * 1000, 50, this); 【参考方案1】:

其实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;

完整代码请查看上面提供的链接。

【讨论】:

【参考方案2】:

对于搜索最佳位置提供商,请使用以下代码: 请参考https://developer.android.com/reference/android/location/Criteria.html 为了理解,标准将如何发挥作用?

public  String getProviderName() 
    LocationManager locationManager =(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setSpeedRequired(true);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(false);
    return locationManager.getBestProvider(criteria, true);

【讨论】:

【参考方案3】:

只是不给出任何标准,并尝试获得最佳提供者:

LocationManager mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String best = mgr.getBestProvider(criteria, true);
//since you are using true as the second parameter, you will only get the best of providers which are enabled.
Location location = mgr.getLastKnownLocation(best);

【讨论】:

以上是关于Android LocationManager 标准的主要内容,如果未能解决你的问题,请参考以下文章

Android - LocationManager 小数点后的数字

Android之LocationManager

Android - 使用 LocationManager 不会提供地理修复

Android:LocationManager 构造函数的循环器

当我得到 LocationManager 时发生异常。(Android)

Android开发之LocationManager和定位