Oi Lucas, tudo bem ?
Fiz alguns testes e vi que as dependencias estão desatualizadas.
Então substitui pelas versões novas :
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'
A parte do mapa fica basicamente igual, já o gps mudou bastante, ficou melhor lol
public class GPS extends LocationCallback {
private FusedLocationProviderClient client;
private GPSDelegate delegate;
public GPS(GPSDelegate delegate) {
this.delegate = delegate;
this.client = LocationServices.getFusedLocationProviderClient(delegate.getContext());
}
@SuppressLint("MissingPermission")
public void fazBusca() {
LocationRequest req = new LocationRequest();
req.setInterval(1000);
req.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
req.setSmallestDisplacement(10);
client.requestLocationUpdates(req, this, null);
}
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
delegate.lidaCom(locationResult.getLastLocation());
}
public void cancelaBusca() {
client.removeLocationUpdates(this);
}
}
Usei nesse exemplo um padrão de projeto chamado delegate, se não o conhecer pode ler esse post que fiz
Basicamente essa interface está assim:
public interface GPSDelegate {
Context getContext();
void lidaCom(Location lastLocation);
}
Logo tornei meu fragment em um delegate:
public class MapaFragment extends SupportMapFragment implements OnMapReadyCallback, GPSDelegate {
private GoogleMap map;
private GPS gps;
@Override
public void onResume() {
super.onResume();
getMapAsync(this);
gps = new GPS(this);
gps.fazBusca();
}
@Override
public void onPause() {
super.onPause();
gps.cancelaBusca();
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
}
@Override
public void lidaCom(Location lastLocation) {
MarkerOptions marker = new MarkerOptions();
LatLng latlng = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
marker.position(latlng);
map.addMarker(marker);
}
}
Espero que tenha ficado claro, o que precisar pode dar um toque :D