This is similar to my other answer , updated to use the recently introduced FusedLocationProviderClient class.
To use FusedLocationProviderClient in conjunction with a Google map:
Wait until the Google map is ready
If necessary, request Location permission at run time
Request location update after getting permission
Refresh Google Map as soon as the user's location is determined
First, make sure that you are using at least the 11th version of Google Play Services, since older versions do not have the FusedLocationProviderClient class (newer versions will also work):
dependencies { implementation 'com.google.android.gms:play-services-maps:11.6.0' implementation 'com.google.android.gms:play-services-location:11.6.0' //........ }
Please note that FusedLocationProviderClient is present in version 11.0.2, but due to errors in the initial implementation, it is recommended to use this class only in 11.6.0 and later. From the documentation:
Note. It is recommended that you use Google Play services version 11.6.0 or higher, which include bug fixes for this class.
Here is the full Activity class:
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback { GoogleMap mGoogleMap; SupportMapFragment mapFrag; LocationRequest mLocationRequest; Location mLastLocation; Marker mCurrLocationMarker; FusedLocationProviderClient mFusedLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().setTitle("Map Location Activity"); mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFrag.getMapAsync(this); } @Override public void onPause() { super.onPause(); //stop location updates when Activity is no longer active if (mFusedLocationClient != null) { mFusedLocationClient.removeLocationUpdates(mLocationCallback); } } @Override public void onMapReady(GoogleMap googleMap) { mGoogleMap = googleMap; mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(120000); // two minute interval mLocationRequest.setFastestInterval(120000); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { //Location Permission already granted mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); mGoogleMap.setMyLocationEnabled(true); } else { //Request Location Permission checkLocationPermission(); } } else { mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); mGoogleMap.setMyLocationEnabled(true); } } LocationCallback mLocationCallback = new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) { List<Location> locationList = locationResult.getLocations(); if (locationList.size() > 0) { //The last location in the list is the newest Location location = locationList.get(locationList.size() - 1); Log.i("MapsActivity", "Location: " + location.getLatitude() + " " + location.getLongitude()); mLastLocation = location; if (mCurrLocationMarker != null) { mCurrLocationMarker.remove(); } //Place current location marker LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(latLng); markerOptions.title("Current Position"); markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); mCurrLocationMarker = mGoogleMap.addMarker(markerOptions); //move map camera mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11)); } } }; public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99; private void checkLocationPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { // Show an explanation to the user *asynchronously* -- don't block // this thread waiting for the user response! After the user // sees the explanation, try again to request the permission. new AlertDialog.Builder(this) .setTitle("Location Permission Needed") .setMessage("This app needs the Location permission, please accept to use location functionality") .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //Prompt the user once explanation has been shown ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION ); } }) .create() .show(); } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION ); } } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_LOCATION: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // location-related task you need to do. if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); mGoogleMap.setMyLocationEnabled(true); } } else { // permission denied, boo! Disable the // functionality that depends on this permission. Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show(); } return; } // other 'case' lines to check for other // permissions this app might request } } }
The user will be prompted to accept Location permission:

The location will be updated when the application starts and every two minutes:
