getLastKnownLocation()在设备上返回null

时间:2012-08-24 12:30:49

标签: android google-maps

在下面的代码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();
      }
    }
  }
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我首先关注这一行。您的requestLocationUpdates的{​​{1}}设置为0。

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

minDistance

修改

此外,我会在您的locationManager.requestLocationUpdates(provider, 1000, 15, new LocationListener() { 方法中添加Log。这样做对我有所帮助。

以下是我早期LocationListener脚本的示例:

LocationListener