Question

I need to add a given number of markers to a map using a for loop. The Log messages tell me the function to add each marker is called, but only one marker (two at most) are displayed on the map. My two functions are the following:

private void paintInMap(String description){
    map.clear(); // to erase previous markers
    String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
    String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
    String[] route = description.split(", "); // split the different places of the route description

    for(int i=0; i<route.length; i++){
        for(int j=0; j<zones.length; j++{
            if(route[i].equals(zones[j])){
                LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
                placeMarker(latLng, zones[j]);
            }
        }
    }
}

and:

private void placeMarker(LatLng coordinates, String name){
    map.addMarker(new MarkerOptions()
        .title(name)
        .icon(BitMapDescriptorFactory.fromResource(R.drawable.gpsmap))
        .position(coordinates)
        .flat(true)
        .rotation(90));
    Log.d("PLACE", name+" added to map");
}

Apparently my code is correct, but on runtime it only displays one (or two) markers. I have checked the Log messages and the function is being called, but the markers do no appear. Moreover, one of the markers appears in an unspecified location (which corresponds to the first value of the coordinates array by the way)

Is this a runtime bug in Eclipse? How can I solve this?

Was it helpful?

Solution 2

I finally solved it by running the for loop in the UI thread instead of doing it using an AsyncTask

......
route = descriptionRoute.split(", ");
coordinates = getCoordinates(coord);
String[] zonas = getResources().getStringArray(R.array.array_zonas_madrid);
String[] coord = getResources().getStringArray(R.array.array_coordinates);
for(int i=0; i<route.length; i++){
    for(int j=0; j<zonas.length; j++){
        if(route[i].equals(zonas[j])){
            LatLng latLng = getCoordinates(coord[j]);
            placeMarker(latLng, zonas[j]);
        }
    }
}
....

OTHER TIPS

    map.clear(); // to erase previous markers
    new AsyncTask<String, MarkerOptions, Void>() {

        private void placeMarker(LatLng coordinates, String name) {
            publishProgress(new MarkerOptions()
                .title(name)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.gpsmap))
                .position(coordinates)
                .flat(true)
                .rotation(90));
            Log.d("PLACE", name + " added to map");
        }

        @Override
        protected Void doInBackground(String... params) {

            String[] zones = getResources().getStringArray(R.array.zonas); // array of place names
            String[] coord = getResources().getStringArray(R.array.coordinates); // array of place coordinates (placed in the same order)
            String[] route = params[0].split(", "); // split the different places of the route description

            for (int i=0; i<route.length; i++) {
                for (int j=0; j<zones.length; j++) {
                    if (route[i].equals(zones[j])) {
                        LatLng latLng = getCoordinates(coord[j]); // call function to get coordinates from String
                        placeMarker(latLng, zones[j]);
                    }
                }
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(MarkerOptions... markers) {
            map.addMarker(markers[0]);
        }
    }.execute(description);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top