【发布时间】:2012-10-16 17:42:59
【问题描述】:
我正在使用 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);
}
我还有一个“返回当前位置按钮” - 用于当用户四处移动地图并想要快速返回其当前位置时。
我的问题是,
我应该每次都使用getBestProvider() 方法来获取位置信息吗?
我可以保存最后选择的提供者,但条件可能每秒都在变化:
- 用户关闭了 GPS
- 没有 GPS 信号
- NETWORK_PROVIDER 在当前情况下更好
- 等等..
什么是正确的方法?
【问题讨论】:
-
stackoverflow.com/questions/3145089/…。检查链接。您可能会找到问题的答案。
-
这仍然没有回答我的问题。我的代码也有效。但是我应该还是不应该每次都调用 getBestProvider ?在我看来是的..
-
developer.android.com/guide/topics/location/…。您可能应该通过此链接。
-
谢谢!!成功了:-)
标签: android android-mapview locationmanager