0

I am trying to add some MapOverlays to my MapView and I get the following Error:

java.util.ConcurrentModificationException
at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:44)
at com.google.android.maps.MapView.onDraw(MapView.java:530)
at android.view.View.draw(View.java:13458)
...
at dalvik.system.NativeStart.main(Native Method)

This is my doInBackground() method of my AsyncTask:

Drawable marker = myContext.getResources().getDrawable(
                R.drawable.marker);
        Bitmap bitmap = ((BitmapDrawable) marker).getBitmap();
        Drawable marker_new = new BitmapDrawable(myContext.getResources(),
                Bitmap.createScaledBitmap(
                        bitmap, MainActivity.MARKER_SIZE * 10,
                        MainActivity.MARKER_SIZE * 10, true));

        mapOverlays = map.getOverlays();

        int minLat = Integer.MAX_VALUE;
        int maxLat = Integer.MIN_VALUE;
        int minLon = Integer.MAX_VALUE;
        int maxLon = Integer.MIN_VALUE;
        double fitFactor = 1.1;

        MainActivity.mapListAddress.clear();
        MainActivity.mapListTitle.clear();
        MainActivity.mapListLati.clear();
        MainActivity.mapListLongi.clear();
        MainActivity.parseMaps();

        for (int i = 0; i < MainActivity.mapListAddress.size(); i++) {
            // String pointAddress = MainActivity.mapListAddress.get(i);
            String pointTitel = MainActivity.mapListTitle.get(i);

            MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(
                    marker_new, map);

            double latitude = MainActivity.mapListLati.get(i) * 1e6;
            double longitude = MainActivity.mapListLongi.get(i) * 1e6;

            Geocoder geocoder;
            List<Address> addresses = null;
            geocoder = new Geocoder(Map.this, Locale.getDefault());
            try {
                addresses = geocoder.getFromLocation(MainActivity.mapListLati.get(i),
                        MainActivity.mapListLongi.get(i), 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);
            String pointAddress = address + "\n" + city + "\n" + country;

            GeoPoint p = new GeoPoint((int) latitude, (int) longitude);

            overlayitem = new OverlayItem(p, pointTitel,
                    pointAddress);

            itemizedOverlay.setBalloonBottomOffset(40);
            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);

            int lat = p.getLatitudeE6();
            int lon = p.getLongitudeE6();

            maxLat = Math.max(lat, maxLat);
            minLat = Math.min(lat, minLat);
            maxLon = Math.max(lon, maxLon);
            minLon = Math.min(lon, minLon);

            map.getController().zoomToSpan((int) (Math.abs(maxLat - minLat) * fitFactor),
                    (int) (Math.abs(maxLon - minLon) * fitFactor));
            map.getController().animateTo(new GeoPoint((maxLat + minLat) / 2,
                    (maxLon + minLon) / 2));
        }
        return null;

Why does this error occur and what am I doing wrong in my code?

Siddharth
  • 9,349
  • 16
  • 86
  • 148
user754730
  • 1,341
  • 5
  • 31
  • 62
  • http://stackoverflow.com/questions/9326129/java-util-concurrentmodificationexception-on-mapview – Niranj Patel Oct 30 '12 at 09:49
  • Wow... That's actually what I've done. I googled it but wasn't able to find a solution which fits my problem. And I also tried some things with threads which in my case also didn't work... – user754730 Oct 30 '12 at 09:54
  • Why the downvote? Who ever did this please explain. Lets not downvote correct and good questions please. – Siddharth Nov 07 '12 at 13:20

1 Answers1

2

Try to synchronize the mapOverlays.add(itemizedOverlay);. I think this is where the issue is, multiple threads trying to add at the same time.

Siddharth
  • 9,349
  • 16
  • 86
  • 148