locationManager.getLastKnownLocation请求后返回null

时间:2018-11-07 10:39:23

标签: java android locationmanager

我尝试通过GPS获取当前位置并显示它。

首先,我初始化LocationManager,所以我总是使用方法getLocation()

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, LocationListener {

    private static final int REQUEST_LOCATION = 1;
    private static int MY_PERMISSIONS_REQUEST_ACCESS;

    TextView locationText;

    LocationManager locationManager;
    String lattitude,longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Button buttonLocate = (Button) findViewById(R.id.buttonLocate);

        locationText = (TextView)findViewById(R.id.locationText);

        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

        buttonLocate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(locationManager != null) {
                    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                        // ..DO SMTH
                    } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                        getLocation();
                    }

                }
            }
        });

    }

在方法getLocation()中,我正在检查权限,如果授予了权限,我将请求位置更新,并尝试获取经度和纬度,这是我的问题,因为位置始终为空,所以我无法获得坐标!  代码有什么问题,我该如何解决?...

void getLocation() {

    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
            (MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

    } else {

    // REQUEST!

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);

        Location location;


        if (locationManager != null) {
               location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                 if (location != null) {
                        // LOCATION IS ALWAYS NULL!!!

                double latti = location.getLatitude();
                double longi = location.getLongitude();
                lattitude = String.valueOf(latti);
                longitude = String.valueOf(longi);

                locationText.setText("Your current location is" + "\n" + "Lattitude = " + lattitude
                        + "\n" + "Longitude = " + longitude);

            } else {

                Toast.makeText(this, "Unble to Trace your location", Toast.LENGTH_SHORT).show();

            }
        } 
    }

1 个答案:

答案 0 :(得分:2)

getLastKnownLocation返回设备的最后一个已知位置。如果没有最后一个已知的位置缓存,它将返回null。

getLastKnownLocation为空时,您需要请求位置更新。

要获取位置更新,您必须实施onLocationChanged

public void onLocationChanged(Location location) {
    // Called when a new location is found by the network location provider.
    Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
}