Pra quem fizer o curso um pouco atrasado, como eu, vai enfrentar um problema nesta atividade. A classe FusedLocationApi será marcada como deprecated. Para solucionar esse problema, eu fiz esse código:
public class Localizador extends LocationCallback implements GoogleApiClient.ConnectionCallbacks {
private final GoogleApiClient client;
private MapaFragment mapaFragment;
private final Context context;
public Localizador(Context context, MapaFragment mapaFragment) {
this.mapaFragment = mapaFragment;
this.context = context;
client = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.build();
client.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
LocationRequest request = new LocationRequest();
request.setSmallestDisplacement(50);
request.setInterval(1000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
FusedLocationProviderClient locationClient = LocationServices.getFusedLocationProviderClient(context);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
String[] permissoes = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
};
ActivityCompat.requestPermissions(
mapaFragment.getActivity(), permissoes, MapaActivity.LOCATION_PERMISSION_CODE);
return;
}
locationClient.requestLocationUpdates(request, this, null);
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationResult(LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
LatLng coordenada = new LatLng(location.getLatitude(), location.getLongitude());
mapaFragment.centralizaEm(coordenada);
}
}
}
Vocês chegaram em alguma solução parecida? Alguém tem alguma sugestão melhor?