确定位置提供者 - Android

时间:2014-10-30 14:27:49

标签: android location-client

我使用LocationClient方法检索用户的当前位置。它工作得很好,但有时候因为明显的原因(我在里面,使用WIFI等)不准确。我想知道是否确定使用什么位置提供程序来获取位置。 getProvider()返回“融合”,无论我在内部还是外部。我只需要告诉用户他没有得到准确的位置,可能是因为他在里面。更好的是,告诉用户使用什么Provider来获取他的位置。

2 个答案:

答案 0 :(得分:0)

您可以使用isProviderEnabled方法检查提供程序是否可用:

        mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
        mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

答案 1 :(得分:0)

检查以下代码以获取位置提供商列表 -

List<String> matchingProviders = locationManager.getAllProviders();
for (String provider: matchingProviders) {
  Location location = locationManager.getLastKnownLocation(provider);
  if (location != null) {
    float accuracy = location.getAccuracy();
    long time = location.getTime();

    if ((time > minTime && accuracy < bestAccuracy)) {
      bestResult = location;
      bestAccuracy = accuracy;
      bestTime = time;
    }
    else if (time < minTime && 
             bestAccuracy == Float.MAX_VALUE && time > bestTime){
      bestResult = location;
      bestTime = time;
    }
  }
}

希望这会对你有所帮助。