GoogleMap.addGroundOverlay() takes a lot of UiThread time

332 views Asked by At

I have an ArrayList:

ArrayList<GroundOverlayOptions> goArray;

- ca. 2-3k of elements. When i try to display it like this:

for (GroundOverlayOptions temp : goArray) {
   map.addGroundOverlay(temp);
}

it takes a lot of UiThread time (ca. 10-15s) and it creates ANR bugs. map.addGroudOverlay() must be run on UiThread. Is there any workaround for this?

content of goArray:

float longitude, float latitude, float width,float height, int color
{
final Bitmap bitmap = Bitmap
            .createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    bitmap.setPixel(0, 0, color);
    final BitmapDescriptor image = BitmapDescriptorFactory
            .fromBitmap(bitmap);
    final LatLngBounds bounds = new LatLngBounds(new LatLng(latitude
            - height / 2, longitude - width / 2), new LatLng(latitude
            + height / 2, longitude + width / 2));
    long startTime = System.currentTimeMillis();
    GroundOverlayOptions go = new GroundOverlayOptions().image(image)
            .positionFromBounds(bounds).transparency(0.33f);
    goArray.add(go);
}
1

There are 1 answers

2
Álisson Morais On

Why dont you try AsyncTask to do that?

private class MapTask extends AsyncTask<GroundOverlayOptions, Integer, Void> {
    protected Void onPreExecute() {
        //Use something like ProgressBar to keep user informed
    }

    protected Void doInBackground(GroundOverlayOptions... maps) {
        //Will need to use maps[0] 
        //because that method treat this param as an Array
        for (GroundOverlayOptions temp : maps[0]) {
            map.addGroundOverlay(temp);
        }

        return null;
    }

    protected void onProgressUpdate(Integer... progress) {
        //Update ProgressBar
    }
 }

The place where you doing the loop call AsyncTask to handle this. Pass the goArray to AsyncTask.

new MapTask().execute(goArray);