我已经创建了一个应用程序,它可以从数据库中获取区域和城市等详细信息,并使用地理编码器获取详细信息,我获得了经度和纬度值。现在,我希望在一定时间后向位置提供商提供位置更新。我只能使用LocationManager获得解决方案。请给我解决方案,是否有可能获得我期待的位置更新?并建议我能做什么?
答案 0 :(得分: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;
}
}