Advertisement
Guest User

getLocation

a guest
Mar 24th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. private void startLocationUpdates() {
  2.  
  3.         FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
  4.  
  5.         if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
  6.                 && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  7.             // TODO: Consider calling
  8.             //    ActivityCompat#requestPermissions
  9.             // here to request the missing permissions, and then overriding
  10.             //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
  11.             //                                          int[] grantResults)
  12.             // to handle the case where the user grants the permission. See the documentation
  13.             // for ActivityCompat#requestPermissions for more details.
  14.             final String[] permissions = new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION};
  15.             ActivityCompat.requestPermissions(context, permissions, RC_FINE_LOCATION);
  16.             return;
  17.         }
  18.         Task<Location> locationAccess = mFusedLocationClient.getLastLocation().addOnSuccessListener(context, new OnSuccessListener<Location>() {
  19.  
  20.             @Override
  21.             public void onSuccess(Location location) {
  22.                 // Got last known location. In some rare situations this can be null.
  23.                 if (location != null) {
  24.                     // Logic to handle location object
  25.                 }
  26.             }
  27.         });
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement