android - 每次请求位置更新时都应该使用 android LocationManager.getBestProvider 吗?

标签 android android-mapview locationmanager

我正在使用 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()方法每次获取位置信息?

我可以保存最后选择的提供商,但条件可能每秒都在变化:

  1. 用户关闭了 GPS
  2. 没有 GPS 信号
  3. NETWORK_PROVIDER 在当前情况下更好
  4. 等..

什么是正确的方法?

最佳答案

关于android - 每次请求位置更新时都应该使用 android LocationManager.getBestProvider 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12920451/

相关文章:

android - Nutiteq MapView 对长按没有反应

Android GPS networkProvider 不工作

java - 使用 Date 类将给定日期的字符串与当前日期进行比较时出现 Null 对象引用异常

android - 如何在android中点击覆盖时显示弹出窗口?

android - 来自广播接收者的通知

Android MapView 与 DrawerLayout 重叠

Android:如何使 GPS 试图找到锁超时?

java - Android GPS 需要一段时间才能准确

android - onBind() 永远不会在服务中被调用

android - 如何使用 pjsua2 android 发送带有自定义 header 的 SIP 消息