Android LocationManager.getLastKnownLocation()在Android 4.1.1上返回null

时间:2013-07-25 07:23:22

标签: android

我正在开发Android应用程序。我陷入了困境。 getLastKnownLocation(provider)为Android版本4.1.1返回null,而对于其他版本则可以。提供商已启用,其余的都可以。我不知道问题出在哪里。这是代码。

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    location = locationManager.getLastKnownLocation(provider);
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);
    mMap.clear();
    System.out.println("Current Location = "+latLng); 

2 个答案:

答案 0 :(得分:0)

如果禁用了提供程序,

locationManager.getLastKnownLocation将返回null,这里是文档

  

http://developer.android.com/reference/android/location/LocationManager.html#getLastKnownLocation(java.lang.String)

答案 1 :(得分:0)

试试这个为我工作......

  mGoogleMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {

                    @Override
                    public boolean onMyLocationButtonClick()
                        {

                            Location myLocation = mGoogleMap.getMyLocation();
                            onLocationChanged(myLocation);
                            return false;
                        }
                });