title: How do I get current location using FusedLocationProviderClient
body: <p>Im trying to get device current location using FusedLocationProviderClient in a fragment. I want the user to see current device location if they accept permissions.
  The problem is in the getDeviceLocation() which checks whether getting location was successful or not.
  The logcat does not show me any errors.</p>
  <p>Here is the java code:</p>
  <pre><code>public class DirectionsFragment extends Fragment implements OnMapReadyCallback  {
  
      GoogleMap mMap;
      Boolean mLocationPermissionGranted=false;
      public String TAG=&quot;DirectionsFragment&quot;;
      private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
      private static final String COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
      private static final int LOCATION_PERMISSION_CODE = 1234;
      private FusedLocationProviderClient mFusedLocationProviderClient;
      static final float DEFAULT_ZOOM = 15f;
      private Context mContext=getContext();
    OnMapReadyCallback callback=new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            Toast.makeText(getContext(),&quot;Map is Ready&quot;,Toast.LENGTH_SHORT).show();
            mMap = googleMap;
            if (mLocationPermissionGranted) {
                getDeviceLocation();
                if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  
                    return;
                }
  
                    mMap.setMyLocationEnabled(true);
                mMap.getUiSettings().setMyLocationButtonEnabled(true);
                mMap.getUiSettings().setZoomControlsEnabled(true);
                mMap.getUiSettings().setZoomControlsEnabled(true);
            }
        }
    };
  
      @Override
      public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  
  
          super.onViewCreated(view, savedInstanceState);
          getLocatonPermission();
  
      }
  
      private void getDeviceLocation() {
  
          Log.d(getTag(), &quot;getDeviceLocation:getting device  current location&quot;);
          mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getContext());
          try {
              if (mLocationPermissionGranted=true) {
  
                  if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &amp;&amp; ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                      return;
                  }
                  Task location = mFusedLocationProviderClient.getLastLocation();
                  location.addOnCompleteListener(new OnCompleteListener() {
                      @Override
                      public void onComplete(@NonNull Task task) {
                          if (task.isSuccessful()){
                              Log.d(TAG,&quot;onComplete:found Location&quot;);
                              Location currentLocation=(Location)task.getResult();
                                  moveCamera(new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude()),DEFAULT_ZOOM);
                          }else{
                              Log.d(TAG,&quot;onComplete:current location is null&quot;);
                              Toast.makeText(mContext, &quot;unable to get current location&quot;, Toast.LENGTH_SHORT).show();
  
                          }
                      }
                  });
              }
          }catch (SecurityException sexc){
              Log.e(TAG,&quot;getDeviceLocation:SecurityException:&quot;+sexc.getMessage());
          }
      }
      //What happens on map zoom
  private  void moveCamera(LatLng latLng,float zoom){
  Log.d(TAG,&quot;moveCamera:moving the camera to: lat:&quot;+latLng.latitude+&quot;,&quot;+latLng.longitude);
  mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,zoom));
  }
  
      public  void initMap(){
          SupportMapFragment mapFragment=(SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
              if (mapFragment!=null){
                  mapFragment.getMapAsync(callback);
              }
  
      }
  
      private void getLocatonPermission() {
  
          String[] permissions={Manifest.permission.ACCESS_FINE_LOCATION,
                  Manifest.permission.ACCESS_COARSE_LOCATION
          } ;
          if (ContextCompat.checkSelfPermission(getContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  
              if (ContextCompat.checkSelfPermission(getContext(), COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                  mLocationPermissionGranted = true;
                  
                  initMap();
              } else {
                  ActivityCompat.requestPermissions(getActivity(), permissions, LOCATION_PERMISSION_CODE);
              }
          }  else {
              ActivityCompat.requestPermissions(getActivity(), permissions, LOCATION_PERMISSION_CODE);
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
          mLocationPermissionGranted=false;
  
          switch (requestCode){
  
              case LOCATION_PERMISSION_CODE:{
                  if (grantResults.length&gt;0){
                      for (int i=0;i&lt;grantResults.length;++i){
                          if (grantResults[i]!=PackageManager.PERMISSION_GRANTED){
                              mLocationPermissionGranted=false;
                              return;
                          }
                      }
  
                      mLocationPermissionGranted=true;
  
                      initMap();
                  }
              }
          }
      }
  
      @Override
      public void onMapReady(GoogleMap googleMap) {
  
      }
  }
  </code></pre>
  <p>XML code</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
  
      &lt;fragment
          android:id=&quot;@+id/map&quot;
          class=&quot;com.google.android.gms.maps.MapFragment&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>