LocationTracker.java 文件源码

java
阅读 20 收藏 0 点赞 0 评论 0

项目:custom-maps 作者:
@Override
public void onLocationChanged(Location location) {
  if (location == null || isQuitting) {
    return;
  }

  long now = System.currentTimeMillis();
  if (currentLocation != null) {
    // Never overwrite recent GPS location with a NETWORK location
    if (location.getProvider().equals(LocationManager.NETWORK_PROVIDER) &&
        currentLocation.getProvider().equals(LocationManager.GPS_PROVIDER) &&
        now < lastGpsTime + GPS_EXPIRATION) {
      return;
    }
    // Ignore updates that come out of order from the same location provider
    if (location.getProvider().equals(currentLocation.getProvider()) &&
        location.getTime() < currentLocation.getTime()) {
      return;
    }
  }
  // Update magnetic declination once every minute
  // -- diff between "true north" and magnetic north in degrees
  if (declinationTime + 60000 < now) {
    declinationTime = now;
    GeomagneticField magneticField = new GeomagneticField(
        (float) location.getLatitude(), (float) location.getLongitude(),
        location.hasAltitude() ? (float) location.getAltitude() : 100f, // use 100m if not known
        declinationTime);
    compassDeclination = Float.valueOf(magneticField.getDeclination());
  }
  // If there is no bearing (lack of motion or network location), use latest compass heading
  if (!location.hasBearing() || !location.hasSpeed() || location.getSpeed() < 0.5f) {
    location.setBearing(compassHeading);
  }

  // Apply geoid height correction to altitude, if reported
  if (location.hasAltitude() && GeoidHeightEstimator.isInitialized()) {
    int geoidCm =
        GeoidHeightEstimator.computeGeoidHeight(location.getLatitude(), location.getLongitude());
    location.setAltitude(location.getAltitude() - Math.round(geoidCm / 100f));
  }

  // Update current location
  if (currentLocation != null) {
    currentLocation.set(location);
  } else {
    currentLocation = new Location(location);
  }

  if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
    lastGpsTime = now;
  }
  updateSpeedAndAltitude(location);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号