データベースから地域や都市などの詳細を取得するアプリケーションを作成し、ジオコーダーを使用してその詳細を使用して、経度と緯度の値を取得しました。ここで、特定の時間後にロケーション プロバイダーにロケーションの更新を提供したいと考えています。LocationManager を使用してのみ解決策を得ることができました。解決策を教えてください。期待どおりに位置情報の更新を取得できますか? 私に何ができるか教えてください。
1 に答える
0
Location 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 に答える