我已经制作了一个应用程序,它从数据库中获取区域和城市等详细信息,并使用地理编码器详细了解我获得的经度和纬度值。现在,我想在一定时间后向位置提供商提供位置更新。我只能使用LocationManager获得解决方案。请给我提供解决方案,是否有可能获得我期待的位置更新?并建议我可以做什么?从Geocoder获取经度和纬度值的位置更新
回答
我不是很确定你想要如何更新你的位置,通过使用位置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;
}
}
我能够使用Geocoder从数据库的详细信息中成功获取位置详细信息{longitude and latitude}。我现在想要的是如何使用位置提供商获取这些详细信息的更新,以便自动设置标记以覆盖更新的位置?以上 – 2012-04-25 03:07:59
代码正是有关如何注册位置监听器和请求updates.In您的活动,做 1.'SFLocationManager.getInstance(getApplicationContext())的addObserver(本);''中onCreate' 2.' 。SFLocationManager.getInstance(getApplicationContext())registerLocationListener();''中onStart' 3.'SFLocationManager.getInstance(getApplicationContext())registerLocationListener();''中onStop' 4.'SFLocationManager.getInstance( 5.在'Activity'中实现'Observer',然后你将在'update'中接收'Location'数据。 – Longerian 2012-04-25 05:51:37
- 1. 获取位置(经度和纬度)
- 2. 获取经度和纬度值更快
- 3. 从Google GeoCoder Json获取经度和纬度? [C#]
- 4. 设置位置纬度/经度从edittext获取值
- 5. 从经纬度获取位置名称
- 6. 从经纬度获取位置地址
- 7. 如何将经度和纬度值从geocoder值添加到nsmutablearray?
- 8. 如何从android的位置服务获取纬度和经度
- 9. 使用Javascript从位置获取经度和纬度的函数
- 10. 如何获取与Google地图相同的Google Geocoder API几何值(经度和纬度)URL经纬度值
- 11. android从输入位置获取纬度和经度
- 12. 从用户指定位置获取纬度和经度
- 13. 如何从Facebook用户位置获取经度和纬度?
- 14. 获取纬度和经度
- 15. 获取经度和纬度
- 16. 获取经度和纬度值
- 17. 获取当前位置的经纬度
- 18. 从国名获取经度和纬度
- 19. 从GpsMyLocationProvider获取经度和纬度(OSMdroid)
- 20. 获取纬度和经度从PIN码
- 21. 从PostCode获取经度和纬度
- 22. 从restfb获取纬度和经度
- 23. 如何从iPhone中的UIWebView获取触摸位置的纬度和经度值?
- 24. 获取来自控制器的纬度和经度值在rails-geocoder gem
- 25. 获取当前位置的纬度和经度的问题
- 26. 如何从纬度/经度获取附近的位置?
- 27. 从给定的地址名称获取纬度和经度。 NOT Geocoder
- 28. 如何获取位置的经纬度和从后台服务更新
- 29. 如何获取经度和纬度后的用户位置
- 30. 获取用户位置的经度和纬度
欢迎来到StackOverflow!我真的不明白这个问题是什么。你说你有使用LocationManager的解决方案......那么如果你有解决方案,那么问题是什么?另请阅读[问]以了解如何最好地制定您的问题! :) – Jack 2012-04-25 01:39:29
我使用地理编码器获取经度和纬度,并与这些我已经在覆盖层上设置标记...我的问题是如何更新经度和纬度值?我曾见过LocationManager示例,但我无法理解如何使用位置提供程序获取位置更新。这对我来说是个问题。很抱歉给您造成不便。 – 2012-04-25 03:03:30