-3

我正在开发安卓应用程序。我陷入了困境。 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); 
4

2 回答 2

0

locationManager.getLastKnownLocation如果提供程序被禁用,将返回 null,这是文档

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

于 2013-07-25T07:31:36.903 回答
0

试试这个它对我有用......

  mGoogleMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {

                    @Override
                    public boolean onMyLocationButtonClick()
                        {

                            Location myLocation = mGoogleMap.getMyLocation();
                            onLocationChanged(myLocation);
                            return false;
                        }
                });
于 2014-02-27T07:09:41.563 回答