olá, estou tentando utilizar ajuda de mapas em meu aplicativo. Eu recupero as coordenadas através de Json, o problema é que a função que leva até a coordenada (onMapReady) executa mais rápido que a função que recupera as coordenadas.
public class LocalizacaoFragment extends Fragment implements OnMapReadyCallback {
private Empresa mEmpresa;
private static GoogleMap mMap;
private static SupportMapFragment supportMapFragment;
private static String empresa;
private static String endereco;
private static String bairro;
private static String cidade;
private static String estado;
private static String latitude;
private static String longitude;
double dLatitude;
double dLongitude;
private String urlEmpresa;
private boolean chamou = false;
LocationManager lm;
private static View rootView;
public LocalizacaoFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.localizacao_fragment, container, false);
supportMapFragment = SupportMapFragment.newInstance();
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
fragmentManager.beginTransaction().replace(R.id.map, supportMapFragment).commitAllowingStateLoss();
}
supportMapFragment.getMapAsync(this);
} catch (InflateException e) {
}
return rootView;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
double dLatitude = Double.parseDouble(latitude);
double dLongitude = Double.parseDouble(longitude);
// Add a marker in Sydney and move the camera
LatLng localizacao = new LatLng(dLatitude, dLongitude);
mMap.addMarker(new MarkerOptions().position(localizacao).title(empresa).snippet(endereco + " - " + bairro + " - " + cidade + "/" + estado)).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLng(localizacao));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(localizacao, 16));
}
private void recuperarEndereco() {
chamou = true;
JsonArrayRequest recuperarLocalizacao = new JsonArrayRequest(urlEmpresa,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
mEmpresa = new Empresa();
empresa = URLDecoder.decode(
URLEncoder.encode(obj.getString("empresa"), "iso8859-1"), "UTF-8");
endereco = URLDecoder.decode(
URLEncoder.encode(obj.getString("endereco"), "iso8859-1"), "UTF-8");
bairro = URLDecoder.decode(
URLEncoder.encode(obj.getString("bairro"), "iso8859-1"), "UTF-8");
cidade = URLDecoder.decode(
URLEncoder.encode(obj.getString("cidade"), "iso8859-1"), "UTF-8");
estado = URLDecoder.decode(
URLEncoder.encode(obj.getString("estado"), "iso8859-1"), "UTF-8");
latitude = URLDecoder.decode(
URLEncoder.encode(obj.getString("latitude"), "iso8859-1"), "UTF-8");
longitude = URLDecoder.decode(
URLEncoder.encode(obj.getString("longitude"), "iso8859-1"), "UTF-8");
dLatitude = Double.parseDouble(latitude);
dLongitude = Double.parseDouble(longitude);
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
AppController.getInstance().addToRequestQueue(recuperarLocalizacao);
}
@Override
public void onAttach(Activity activity) {
Intent intent = activity.getIntent();
Bundle params = intent.getExtras();
if (params != null) {
urlEmpresa = params.getString("urlEmpresa");
}
recuperarEndereco();
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
Alguém pode me ajudar a solucionar esse problema?