google maps – Get the position of the beginning of the activity and every second and store it in a variable

I have to get the location of a user continuously every second so that when the user presses the button, he calls button click and he (for the purposes of this question) will indicate the precise GPS position of the user.

Here is my code:

Public class, Local_Map Etend FragmentActivity implements OnMapReadyCallback, LocationListener {

private GoogleMap mapping;
final static int LOCATION_PERMISSION_REQUEST_CODE = 1;
private LatLng user_location;

@Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_local_map);
// Get the SupportMapFragment and receive a notification when the card is ready to use.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager ()
.findFragmentById (R.id.map);
mapFragment.getMapAsync (this);
}

@Override
public void onMapReady (GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMyLocationButtonClickListener (onMyLocationButtonClickListener);
enableMyLocationIfPermitted ();
mMap.setMinZoomPreference (14);
}

Private Void enableMyLocationIfPermitted () {
if (ContextCompat.checkSelfPermission (this,
Manifest.permission.ACCESS_FINE_LOCATION)
! = PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions (this,
new channel[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION}
LOCATION_PERMISSION_REQUEST_CODE);
} else if (mMap! = null) {
mMap.setMyLocationEnabled (true);

LocationManager LM = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
String provider = LM.getBestProvider (new Criteria (), false);
Location loc = LM.getLastKnownLocation (provider);

LatLng pos_gps;
if (loc == null) {
pos_gps = new LatLng (16.482655, -238.846006);
} other {
pos_gps = new LatLng (loc.getLatitude (), loc.getLongitude ());
user_location = pos_gps;
}

CameraPosition cameraPosition = new CameraPosition.Builder ()
.target (pos_gps) // Set the center of the map to the location of the user
.zoom (16) // Set the zoom
.to build(); // Create a CameraPosition from the generator
mMap.animateCamera (CameraUpdateFactory.newCameraPosition (cameraPosition));
}
}
private void showDefaultLocation () {
Toast.makeText (this, "Location Permissions Not Granted" +
"Please reinstall the application",
Toast.LENGTH_SHORT) .show ();
LatLng redmond = new LatLng (47.6739881, -122.121512);
mMap.moveCamera (CameraUpdateFactory.newLatLng (redmond));
}

@Override
public void onRequestPermissionsResult (int requestCode, @NoNull String[] authorizations
@NonNull int[] grantResults) {
switch (requestCode) {
case LOCATION_PERMISSION_REQUEST_CODE: {
if (grantResults.length> 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
enableMyLocationIfPermitted ();
} other {
showDefaultLocation ();
}
return;
}

}
}




private GoogleMap.OnMyLocationButtonClickListener onMyLocationButtonClickListener =
new GoogleMap.OnMyLocationButtonClickListener () {
@Override
Public Boolean onMyLocationButtonClick () {
mMap.setMinZoomPreference (14);
returns false;
}
};

public void buttonclick (Show the view) {
Toast.makeText (getApplicationContext (), "Current location:  n" + user_location, Toast.LENGTH_LONG) .show ();
}

@Override
public void onLocationChanged (Rental location) {
LatLng position_gps = new LatLng (location.getLatitude (), location.getLongitude ());
user_location = gps_position;
mMap.moveCamera (CameraUpdateFactory.newLatLngZoom (gps_position, 15));
}

@Override
public void onStatusChanged (string provider, int status, bundle extras) {

}

@Override
public void onProviderEnabled (string provider) {

}

@Override
public void onProviderDisabled (string provider) {

}
}

Here are some problems encountered:

1.) At startup, the location is NULL

2.) Even if the user is several kilometers away, the GPS coordinates are always the same. [Although the blue dot, which indicates the phone, is moving]

How to force the location to be updated constantly?