Skip to content Skip to sidebar Skip to footer

MapBox Markers Stopped Showing In The Map After Using Dynamic Data Because The Data Comes Late

i have implemented a mapbox and it works so fine with static markers data , but when i used dynamic data that comes from retrofit 2 , the markers are not showing anymore , here an

Solution 1:

You are correct :D The problem lies with the timing.

Once the map has loaded you request the data for your markers. But you do not wait for this data to arrive, and you create the markers in the onLoad() callback of the map, instead of waiting for the data to arrive and then creating the markers in the onResponse() callback of the requests.

To get the markers to show, create them in the respective onResponse() callbacks of your http requests:

fillDataShops();
fillDataCircuits();
fillDataCommunities();
fillDataCyclists();

Example:

public void fillDataCircuits(){

        Call<List<Circuit>> call = apiService.getCircuits();
        call.enqueue(new Callback<List<Circuit>>() {
            @Override
            public void onResponse(Call<List<Circuit>> call, Response<List<Circuit>> response) {
                if(response.isSuccessful()){
                    circuits.addAll(response.body());
                    // Create Markers here
                }
            }
            @Override
            public void onFailure(Call<List<Circuit>> call, Throwable t) {
                Log.e("ERROR: ", t.getMessage());
            }
        });

    }

Post a Comment for "MapBox Markers Stopped Showing In The Map After Using Dynamic Data Because The Data Comes Late"