现在的位置: 首页 > 综合 > 正文

Android位置策略(三)

2013年08月21日 ⁄ 综合 ⁄ 共 2852字 ⁄ 字号 评论关闭

确定启动监听位置更新的时机

你可能想要在应用程序一启动就要监听位置更新,或者是在用户激活某个特定功能之后才开始监听位置更新。要注意的时,长时间的监听位置的变化会消耗大量的电池电量,但是短期的监听可能不会提供足够的精度。

如上诉Demo,通过调用requestLocationUpdates()方法能够开始监听位置的更新:

LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or, use GPS location data:
// LocationProvider locationProvider = LocationManager.GPS_PROVIDER;

locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);

获取快速修正的最后确定的位置 你

你的位置监听器接受到第一个位置所要花费的时间经常要用户等待很长时间,你应该通过调用getLastKnownLocation(String)方法来利用被缓存的位置,直到有更精确的位置提供给你的位置监听器:

LocationProvider locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER

Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

确定终止监听位置更新的时机

决定不再需要新位置时机的逻辑,根据你的应用程序的需要,它的范围从很简单到很复杂。在获取位置和使用该位置的短暂间隙,来改善估算的精度。始终要注意长时间的监听要消耗大量的电池电量,一旦获取你所需要的信息,就应该通过调用removeUpdates(PendingIntent)方法来终止监听位置的更新:

// Remove the listener you previously added
locationManager.removeUpdates(locationListener);

保持当前的最佳估算值

你可能期望最近的位置修正是最精确的。但是,因为位置变化修正的精度,不总是最近的就是最好的。你应该包括基于几种标准的选择位置修正的逻辑。这些标准还要根据应用程序所使用的场景和区域来进行测试。

以下是验证位置修正的精度的几个步骤:

1.检测新获取的位置是否比之前的更新;

2.检测新获取的位置是否比之前的更精确或更差;

3.检查新的位置是来源于哪个提供器,并判断它是否更加真实可信。

以下示例详细说明了这些检查的逻辑:

private static final int TWO_MINUTES = 1000 * 60 * 2;

/** Determines whether one Location reading is better than the current Location fix
  * @param location  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix, to which you want to compare the new one
  */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.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 true;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.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(location.getProvider(),
            currentBestLocation.getProvider());

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

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
      return provider2 == null;
    }
    return provider1.equals(provider2);
}

抱歉!评论已关闭.