首先,我很抱歉再次提出这个问题,但我还没有找到解决我的问题的方法。
我有一个应用程序,我可以在其中使用 MyLocationOverlay 定位自己,并使用 itemizedOverlay 绘制朋友的位置。但是该应用程序因 ConcurrentModificationExcepion 而崩溃
对于我的位置,我使用 MyLocationOverlay 和这个单一的代码(以前初始化):
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(myLocOverlay);
对于我朋友的位置,我使用了 ItemizedOverlay 和绘制位置的方法。ItemizedOverlay 这是:
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.Drawable;
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;
}
}
这是绘制位置的方法:
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();
}
使用两个不同的mapViews(mapView和mapView2),否则draw方法会在执行时擦除myLocationOverlay点mapView2.postInvalidate();
正如我所说,我获得了 ConcurrentModificationException。我找到了一些解决方案,但我不知道如何应用......这对我来说非常重要,因为它是我职业生涯的结束。太感谢了!