First of all I would apologize for ask again this question, but I don't found a solution for my problem yet.
I have a app where I can locate myself with a MyLocationOverlay, and I draw my friends' position with a itemizedOverlay. But the app crashes with a ConcurrentModificationExcepion
For my location I use MyLocationOverlay with this single code (previusly initialized):
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(myLocOverlay);
For my friends' positión I used a ItemizedOverlay and a method to draw the position. The ItemizedOverlay this is:
package app.localizadroid.ACT;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import android.content.Context;
import android.graphics.drawable.Drawab开发者_JAVA技巧le;
import android.widget.Toast;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class Tab_Activity_map_HelloItemizedOverlay extends Tab_Activity_map_BalloonItemizedOverlay<OverlayItem> {
private List<OverlayItem> mOverlays;
private Context c;
public Tab_Activity_map_HelloItemizedOverlay(Drawable defaultMarker, MapView mapView) {
super(boundCenter(defaultMarker), mapView);
c = mapView.getContext();
mOverlays = new ArrayList();
// TODO Auto-generated constructor stub
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlay(OverlayItem overlay) {
mOverlays.remove(overlay);
populate();
}
public void clear() {
mOverlays.clear();
populate();
}
protected boolean onBalloonTap(int index, OverlayItem item) {
Toast.makeText(c, "onBalloonTap for overlay index " + index, Toast.LENGTH_LONG).show();
return true;
}
}
And this is the method to draw the position:
public void createAndShowMyItemizedOverlay(Location newLocation, String User, String time) {
List overlays = mapView2.getOverlays();
// first remove old overlay
if (overlays.size() > 0) {
for (Iterator iterator = overlays.iterator(); iterator.hasNext();) {
iterator.next();
iterator.remove();
}
}
// transform the location to a geopoint
GeoPoint geopoint = new GeoPoint((int) (newLocation.getLatitude() * 1E6), (int) (newLocation.getLongitude() * 1E6));
// initialize icon
Drawable icon = getResources().getDrawable(R.drawable.marker_friends);
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
// create my overlay and show it
//Tab_Activity_map_MyItemizedOverlay overlay= new Tab_Activity_map_MyItemizedOverlay(icon);
Tab_Activity_map_HelloItemizedOverlay overlay = new Tab_Activity_map_HelloItemizedOverlay(icon, mapView2);
OverlayItem item = new OverlayItem(geopoint, User + "\n" + time, null);
overlay.addOverlay(item);
//overlay.addItem(item);
mapView2.getOverlays().add(overlay);
// redraw map
mapView2.postInvalidate();
}
Using two differents mapViews (mapView and mapView2) because otherwise the draw method erase the myLocationOverlay dot when it does mapView2.postInvalidate();
As I said I obtain a ConcurrentModificationException. I found some solutions but I don't know how to apply... It is very important to me because it is for the end of my career working. Thank you so much!
Change:
// first remove old overlay
if (overlays.size() > 0) {
for (Iterator iterator = overlays.iterator(); iterator.hasNext();) {
iterator.next();
iterator.remove();
}
}
To:
overlays.clear();
精彩评论