1

I tried to get the current location using android studio but always the variable currentLocation has null value

and the android studio draw red line under

    Location currentLocation= LocationServices.FusedLocationApi.getLastLocation(mLocationclient);

and asked to check the permission I tried two ways with checking permission and without but the same result return null value

please who can help me? this is the code I have used


    private GoogleApiClient mLocationclient;
mLocationclient=new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            mLocationclient.connect();

            Location currentLocation= LocationServices.FusedLocationApi.getLastLocation(mLocationclient);
            if(currentLocation==null)
                Toast.makeText(this,"could not connect",Toast.LENGTH_SHORT).show();
            else {
                LatLng ll=new LatLng(
                        currentLocation.getLatitude(),
                        currentLocation.getLongitude()
                );
                CameraUpdate update=CameraUpdateFactory.newLatLngZoom(ll,50);
                mMap.animateCamera(update);
            }


please help me.

2 Answers2

2

actually the answered answer is very helpful so here is what I did with this problem the problem was because I have wrote the full code for go to the current location in the onCreate() implemented method and into this position the getLastLocation is null what I did I jast wrote the connect

private GoogleApiClient mLocationclient;            

mLocationclient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
            mLocationclient.connect();

then I go to the current location listen to button clickListener like this code I have coded into the clickListener

LatLng ll=null;
    Location currentLocation= LocationServices.FusedLocationApi.getLastLocation(mlocationclient);
    if(currentLocation==null)
        Toast.makeText(context,"could not connect",Toast.LENGTH_SHORT).show();
    else {
            ll=new LatLng(
                currentLocation.getLatitude(),
                currentLocation.getLongitude()
        );
    }



 CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, 17);


mMap.moveCamera(update);
0

There are several reasons you are getting null. Most likely, it doesn't have a previous location to pull from. Remember that getLastLocation doesn't actually go out and get a fresh location; it just grabs the last one that your phone has. Your phone will pick up location data from a variety of sources/methods, and this is the data being used. From my experience it is often up to a few minutes old.

I would:

1) Check that you're not using a sim 2) Check that your GPS is working on your phone (is Google Maps working?) 3) Try forcing a loction update (LocationManager.requestLocationUpdate(...) as seen here) and debug from there.

I believe check permission is a warning as long as you also support SDK <21.

Community
  • 1
  • 1
Eric S.
  • 1,502
  • 13
  • 31