0

我制作了一个应用程序,它从数据库中获取区域和城市等详细信息,并使用地理编码器获取这些详细信息,我得到了经度和纬度值。现在我想在特定时间后向位置提供者提供位置更新。我只能使用 LocationManager 获得解决方案。请为我提供解决方案,是否可以像我期望的那样获得位置更新?并建议我能做什么?

4

1 回答 1

0

我不太确定您想如何更新您的位置,通过使用位置 API 或从您的数据库中获取位置并将其转换为经度和纬度?以我的方式,我使用前一个。所以代码是这样的:(让你的应用程序实现Observer

public class SFLocationManager extends Observable {

private static SFLocationManager lm;

private SFLocationManager(Context context) {
    this.context = context;
}

public static SFLocationManager getInstance(Context context) {
    if(lm == null) {
        lm = new SFLocationManager(context);
    }
    return lm;
}

private static final int minTime = 1000 * 10 * 10;
private static final float minDistance = 20.0f;

private Context context;

private LocationManager mLocationManager;

private Location mCurrentBestLocation;

private String mLocationProvider;

public String getLocationProvider() {
    return mLocationProvider;
}

public void registerLocationListener() {
    mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean isWIFIEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if(isGPSEnabled) {
        mLocationProvider = LocationManager.GPS_PROVIDER;
        mLocationManager.requestLocationUpdates(mLocationProvider, minTime, minDistance, mLocationListener);
    } else if(!isGPSEnabled && isWIFIEnabled) {
        mLocationProvider = LocationManager.NETWORK_PROVIDER;
        mLocationManager.requestLocationUpdates(mLocationProvider, minTime, minDistance, mLocationListener);
    } else if(!isGPSEnabled && !isWIFIEnabled) {
        mLocationProvider = null;
    }
}

public void removeLocationListener() {
    if(mLocationManager != null) {
        mLocationManager.removeUpdates(mLocationListener);
    }
}

private LocationListener mLocationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onLocationChanged(Location location) {
         if (location != null) {
                if(LocationUtil.isBetterLocation(location, mCurrentBestLocation)) {
                    mCurrentBestLocation = location;
                    notifyObservers(location);
                }
         }
    }
};

public Location getBestLocation(String provider) {
    if(mLocationManager == null) {
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }
    boolean isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean isWIFIEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    if(!isGPSEnabled && !isWIFIEnabled) {
        return null;
    } else {
        if(provider != null) {
            Location lastKnownLocation = mLocationManager.getLastKnownLocation(provider);
            if(lastKnownLocation != null) {
                if(LocationUtil.isBetterLocation(lastKnownLocation, mCurrentBestLocation)) {
                    mCurrentBestLocation = lastKnownLocation;
                }
            }
            return mCurrentBestLocation;
        } else {
            return null;
        }
    }
}

}


public class LocationUtil {

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
  */
public static 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 static boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
      return provider2 == null;
    }
    return provider1.equals(provider2);
}

public static boolean isLocationAvailable(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean isWIFIEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    return isGPSEnabled || isWIFIEnabled;
}

}
于 2012-04-25T01:55:40.217 回答