onMapReady performs faster than the function that recovers the location

Asked

Viewed 291 times

0

hello, I’m trying to use map help in my app. I recover the coordinates through Json, the problem is that the function that leads to the coordinate (onMapReady) performs faster than the function that recovers the coordinates.

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();
}

}

Can someone help me solve this problem?

1 answer

0


I found a solution, I don’t know if it’s the right one but it worked for me.

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    recuperarEndereco();
}

private void recuperarEndereco() {
    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();

                            mEmpresa.setEmpresa(URLDecoder.decode(
                                    URLEncoder.encode(obj.getString("empresa"), "iso8859-1"), "UTF-8"));
                            mEmpresa.setEndereco(URLDecoder.decode(
                                    URLEncoder.encode(obj.getString("endereco"), "iso8859-1"), "UTF-8"));
                            mEmpresa.setBairro(URLDecoder.decode(
                                    URLEncoder.encode(obj.getString("bairro"), "iso8859-1"), "UTF-8"));
                            mEmpresa.setCidade(URLDecoder.decode(
                                    URLEncoder.encode(obj.getString("cidade"), "iso8859-1"), "UTF-8"));
                            mEmpresa.setEstado(URLDecoder.decode(
                                    URLEncoder.encode(obj.getString("estado"), "iso8859-1"), "UTF-8"));
                            mEmpresa.setLatitude(URLDecoder.decode(
                                    URLEncoder.encode(obj.getString("latitude"), "iso8859-1"), "UTF-8"));
                            mEmpresa.setLongitude(URLDecoder.decode(
                                    URLEncoder.encode(obj.getString("longitude"), "iso8859-1"), "UTF-8"));

                            double dLatitude = Double.parseDouble(mEmpresa.getLatitude());
                            double dLongitude = Double.parseDouble(mEmpresa.getLongitude());

                            // Add a marker in Sydney and move the camera
                            LatLng localizacao = new LatLng(dLatitude, dLongitude);
                            mMap.addMarker(new MarkerOptions().position(localizacao).title(mEmpresa.getEmpresa()).
                                    snippet(mEmpresa.getEndereco() + " - " + mEmpresa.getBairro() + " - " + mEmpresa.getCidade() + "/" + mEmpresa.getEstado())).showInfoWindow();
                            mMap.moveCamera(CameraUpdateFactory.newLatLng(localizacao));
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(localizacao, 16));

                        } catch (JSONException e) {
                            e.printStackTrace();
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                    mEmpresa = new Empresa();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    AppController.getInstance().addToRequestQueue(recuperarLocalizacao);
    chamou = true;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.