【问题标题】:getLastKnownLocation() return always null on devicegetLastKnownLocation() 在设备上总是返回 null
【发布时间】:2012-08-24 12:30:49
【问题描述】:

在getLastKnownLocation() 下面的代码中,即使我收到Provide = internet,在设备上总是返回null。它工作正常,但知道它返回一个空值。我使用的是 Galaxy Tab 2.2 版。

public void find_Location(Context con) {
  Log.d("Find Location", "in find_location");
  this.con=con;
  String location_context = Context.LOCATION_SERVICE;
  LocationManager locationManager =
    (LocationManager)con.getSystemService(location_context);
  List<String> providers = locationManager.getProviders(true);
  for (String provider : providers) {
    locationManager.requestLocationUpdates(provider, 1000, 0,new LocationListener() {
      public void onLocationChanged(Location location) {}
      public void onProviderDisabled(String provider){}
      public void onProviderEnabled(String provider){}
      public void onStatusChanged(String provider, int status,Bundle  extras){}
    });
    Location location = locationManager.getLastKnownLocation(provider);
    if (location != null) {
      lat = location.getLatitude();
      lng = location.getLongitude();
      Geocoder geocoder = new Geocoder(AdvanceSearch.this,
        Locale.getDefault());List<Address> addresses;
      try {
        addresses = geocoder.getFromLocation(lat,lng,100);
        countryname=addresses.get(0).getCountryName();
        eexit e = new eexit();
        statename= addresses.get(0).getAdminArea();
        cityname=addresses.get(0).getLocality();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}

提前感谢您的帮助。

【问题讨论】:

  • 您需要时间来修复。检查何时调用onLocationChanged。
  • stackoverflow.com/questions/1916568/… 看看这个 1。这与您的问题完全相同。
  • 我等了 4 分钟,但 onLocationChanged 从未调用过

标签: android google-maps


【解决方案1】:

我会首先关注这一行。您的requestLocationUpdates 的minDistance 设置为0。

使用minDistance,我认为这就是你的问题所在。

locationManager.requestLocationUpdates(provider, 1000, 15, new LocationListener() {

编辑:

此外,我会在您的 LocationListener 方法中添加一个 Log。这样做对我有很大帮助。

这是我早期的LocationListener 脚​​本示例:

public LocationListener jLocListener = new LocationListener() {
    //class findMe implements LocationListener {
    public void onLocationChanged(Location location) {
        try {
            lat = location.getLatitude();
            lon = location.getLongitude();
        } catch (Exception e) {
            Log.e("onLocationChanged", "FAILED: " + e.getMessage());
        }
    }
    public void onProviderDisabled(String provider) {
        Log.i("LocationListener", "onProviderDisabled");
    }
    public void onProviderEnabled(String provider) {
        Log.i("LocationListener", "onProviderEnabled");
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.i("LocationListener", "onStatusChanged");
    }

【讨论】:

  • 我收到此消息 /MapActivity(5867): 无法获取连接工厂客户端
  • 你在模拟器上试试这个吗?如果是这样,请确保模拟器在硬件部分下选择了 GPS 支持。你的清单中还有&lt;uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" /&gt; 和&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; 吗?
  • 我正在设备上检查它,通过此代码获取当前位置需要太长时间,这会影响它无法正常工作,并且它会减慢我的应用程序,有什么解决方法吗?
猜你喜欢
  • 1970-01-01
  • 2022-01-16
  • 2013-11-06
  • 2013-12-24
  • 2012-10-05
  • 2012-05-28
  • 1970-01-01
相关资源
最近更新 更多