我正在使用 Google API 和 MapActivity、MapView 等。
当我需要获取当前位置时,我会在第一时间使用此代码:
myLocationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
// Sets the criteria for a fine and low power provider
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
crit.setPowerRequirement(Criteria.POWER_LOW);
// Gets the best matched provider, and only if it's on
String provider = myLocationManager.getBestProvider(crit, true);
// If there are no location providers available
if (provider == null){
OnClickListener listener = new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Closes the activity
finish();
}
};
Utils.getInstance().showAlertDialog(this, R.string.warning_dialog_title,
R.string.no_location_providers_error_message,
android.R.drawable.ic_dialog_alert, false, listener);
}
// Location services is enabled
else{
myLocationListener = new MyLocationListener();
myLocationManager.requestLocationUpdates(
provider,
0,
0,
myLocationListener);
// TODO - put a marker in my location
GeoPoint currentGeoPoint = MapUtils.getInstance().
getCurrentLocation(myLocationManager, provider);
// Center to the current location
centerLocation(currentGeoPoint, MAX_ZOOM);
}
我还有一个“回到当前位置按钮”——当用户四处移动 map 并想要快速返回到他的当前位置时。
我的问题是,
我是否应该getBestProvider()
方法每次获取位置信息?
我可以保存最后选择的提供商,但条件可能每秒都在变化:
- 用户关闭了 GPS
- 没有 GPS 信号
- NETWORK_PROVIDER 在当前情况下更好
- 等..
什么是正确的方法?
最佳答案
关于android - 每次请求位置更新时都应该使用 android LocationManager.getBestProvider 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12920451/