【问题标题】:Should I use android LocationManager.getBestProvider every time I ask for location updates?每次我要求位置更新时,我应该使用 android LocationManager.getBestProvider 吗?
【发布时间】: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() 方法来获取位置信息吗?

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

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

什么是正确的方法?

【问题讨论】:

标签: android android-mapview locationmanager


【解决方案1】:

【讨论】:

  • 链接断开。这就是为什么发布链接作为答案是不好的!
【解决方案2】:

你总是可以使用这个,但一定要检查 Location.getTime() -> 这会告诉你它是固定的时间,所以你可以很容易区分新鲜和陈旧位置修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多