简体   繁体   中英

locationManager.getLastKnownLocation returns null after request

I try to get the current location via GPS and show it.

First of all I initialise LocationManager, so I am always getting to method 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();
                    }

                }
            }
        });

    }

In the method getLocation() I am checking the permission and if they are granted I request location updates and try to get latitude and longitude and here is my problem, because the location is always null , so i can't get coordinates! What could be wrong with the code and how could I fix it?...

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();

            }
        } 
    }

getLastKnownLocation returns the device last known location. It will return null if there is no last known location cached.

You need to request for a location update when getLastKnownLocation is null.

In order to get the location update you have to implement 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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM