Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
android
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

android
 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);
            }


What I have tried:

I try (( mMap.setMyLocationEnabled(true); )) and it works fine
but programmatically does not work and always return null value to the variable currentLocation
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900