0

我有一个线程接收从服务器发送的地图点,这些点被添加到MapView. 这很好用,但是如果我与MapView(例如放大、平移等)进行交互,我会得到一个ConcurrentModificationException,那么如何在向 中添加点时允许访问用户MapView

02-20 01:44:26.566: E/AndroidRuntime(9448):     FATAL EXCEPTION: main
02-20 01:44:26.566: E/AndroidRuntime(9448):     java.util.ConcurrentModificationException
02-20 01:44:26.566: E/AndroidRuntime(9448):     at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:41)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at com.google.android.maps.MapView.onDraw(MapView.java:532)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.View.draw(View.java:13707)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.View.draw(View.java:13591)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.View.draw(View.java:13589)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.View.draw(View.java:13589)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.View.draw(View.java:13589)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.View.draw(View.java:13710)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.widget.FrameLayout.draw(FrameLayout.java:467)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.Choreographer.doFrame(Choreographer.java:532)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.os.Handler.handleCallback(Handler.java:725)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.os.Looper.loop(Looper.java:137)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at java.lang.reflect.Method.invoke(Method.java:511)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-20 01:44:26.566: E/AndroidRuntime(9448):     at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

1

您是否尝试过使用新的地图 API?就我个人而言,我发现它更容易使用,也很容易从旧 API 更改为新 API。

链接:https ://developers.google.com/maps/documentation/android/

我发现特别改进的一件事是地图上的绘图。不再有很多溢出等等。使用新 API 添加引脚的示例如下:

    final GoogleMap map = this.getMap();
    final MarkerOptions marker = new MarkerOptions();
    marker.position(latlng);
    marker.draggable(false);
    marker.icon(BitmapDescriptorFactory.fromResource(drawable));
    map.addMarker(marker);
于 2013-02-20T01:55:25.497 回答
0

虽然在后台线程中从服务器接收积分是合适的,但您实际上应该在 UI 线程中添加/更新积分。这应该可以防止任何并发修改问题。并且只要添加/更新过程不太繁重,就不会显着干扰用户体验。

于 2013-02-20T02:39:54.567 回答
0

我试图提取和清理一些使用旧 api 的旧代码。亮点是:

  1. 创建尚未出现在地图上的叠加层
  2. 绘制和以其他方式准备覆盖
  3. 将叠加层添加到地图
  4. 强制地图重绘

将此方法添加到您的活动中

// check if overlay we are adding was already added
final List<Overlay> listOfOverlays = this.mMapView.getOverlays();
if ((this.mPinsOverlay != null) && listOfOverlays.contains(this.mPinsOverlay))
{
    listOfOverlays.remove(this.mPickupOverlay);
}

// create overlay
this.mPinsOverlay = new PinsOverlay();

// add overlay to map
listOfOverlays.add(this.mPinsOverlay);

// force map to redraw
this.mMapView.postInvalidate();

自定义覆盖类,不需要但保持整洁。

public class PinsOverlay extends Overlay
{
    public PinsOverlay()
    {
        super();
    }

    @Override
    public boolean draw(final Canvas canvas, final MapView mapView, final boolean shadow, final long when)
    {
        super.draw(canvas, mapView, shadow);

        // draw your pins here

        return true;
    }
}
于 2013-02-20T04:32:28.907 回答