当有人在地图上移动别针时,getLastKnownLocation是不好的做法吗?

时间:2016-09-15 01:42:20

标签: android google-maps gps

我已经实现了GoogleMap.OnMyLocationChangeListener并附加到我的地图对象OnMyLocationChangeListener,这样无论何时在地图上进行任何移动,我都可以在回调方法中收到这些更改的反馈

@Override
    public void onMyLocationChange(android.location.Location location) {
}

我正在阅读Android开发者策略文档(https://developer.android.com/guide/topics/location/strategies.html)并了解到有一个函数可以确定一个位置读取是否优于当前位置修复

我决定做的是检查每个地图更改的更好位置。为了做到这一点,我从onMyLocationChange回调传递lastKnownGPSLocation和location对象。根据我的知识,调用getLastKnownLocation可能会很昂贵并且电池耗尽。这就是我正在做的事情。

if (isBetterLocation(mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER), location) {
    // update my current lat/long
}

isBetterLocation()的函数如下:

/** 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);
}

这是打算使用这个功能的吗?我注意到其他人将此作为服务使用。提前谢谢。

1 个答案:

答案 0 :(得分:2)

根据此documentation,在处理位置数据时,Android设备可用的位置数据包括使用多种技术精确定位的设备的当前位置。移动的方向和方法,以及设备是否已经移过预定义的地理边界或地理围栏。然后,根据应用程序的需要,您可以在处理位置数据时选择这些方法。

  • 我的Location layer提供了一种在地图上显示设备位置的简单方法。它不提供数据。

  • 建议所有针对位置数据的程序化请求使用Google Play services Location API

  • LocationSource interface允许您提供自定义位置提供商。

此外,从此documentation开始,Google Play services location APIs现在是在应用中添加位置感知的首选方式。只需阅读上面的文档链接,即可了解有关如何实现此目的的更多信息和示例代码。