Estou tentando usar o Google Maps, e settar uma localização nele (puxando a Latitude e Longitude do usuário). Ele puxa e exibe num Toast, mas ele não setta isso no método onMapReady e nem no Marker, e se eu colocar um Log no onResume(), ele mostra como zero.
public class LocationHomeFragment extends BaseFragment implements MvpView, OnMapReadyCallback {
private static final String ARG_PARAM1 = "Title";
private static final int PERM_LOCAL = 2;
private GoogleMap mMap;
private double[] pos;
private FusedLocationProviderClient mFusedLocationClient;
public static Fragment newInstance(String title) {
LocationHomeFragment fragment = new LocationHomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, title);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_location_home, container, false);
ButterKnife.bind(this, view);
SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map_localizacao_home);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
return view;
}
@Override
public void onResume() {
super.onResume();
// AQUI ELE PUXA COMO ZERO PARA AMBOS
pos = mostraPosicao();
Log.i("LOG", "onResume(): Lat -> " + pos[0]);
Log.i("LOG", "onResume(): Lng -> " + pos[1]);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// // permisao aceita
mostraPosicao();
Log.i("LOG", "Aceitou");
CommonUtils.toast(getContext(), "Aceitou");
} else {
Log.i("LOG", "Recusou");
CommonUtils.toast(getContext(), "Recusou");
}
if (requestCode == PERM_LOCAL) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mostraPosicao();
} else {
Log.i("LOG", "else1");
}
} else {
Log.i("LOG", "else2");
}
}
public double[] mostraPosicao() {
double[] d = new double[2];
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, PERM_LOCAL);
} else {
mFusedLocationClient.getLastLocation().addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
d[0] = location.getLatitude();
Toast.makeText(getActivity(), "Latitude: " + String.valueOf(location.getLatitude()), Toast.LENGTH_SHORT).show();
Log.i("LOG", "Latitude: " + location.getLatitude());
d[1] = location.getLongitude();
Toast.makeText(getActivity(), "Longitude: " + location.getLongitude(), Toast.LENGTH_SHORT).show();
Log.i("LOG", "Longitude: " + location.getLongitude());
} else {
Log.i("LOG", "Localização vindo nula");
}
}
});
}
return d;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Log.i("LOG", "MapReady");
googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
LatLng posicao = new LatLng(-23.6179537, -46.688076);
Log.i("LOG", "Lat: " + pos[0]);
Log.i("LOG", "Lng: " + pos[1]);
// googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(posicao, 13));
mMap.addMarker(new MarkerOptions()
.position(posicao)
.title("Local")
.snippet("Endereço do local")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bigpin)));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(posicao, 13));
}
});
}
}