2013-02-20 44 views
0

我有一個接收從服務器發送的地圖點的線程,並將這些點添加到MapView。這工作正常,但如果我與MapView交互(例如,放大,平移等),我得到ConcurrentModificationException,所以我怎樣才能允許訪問用戶,而添加點MapView我如何將點添加到Google地圖視圖,同時仍允許用戶與地圖交互?

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) 

回答

0

雖然這是適當的,從服務器接收點在後臺線程,你實際上應該在UI線程中添加/更新的點。這應該防止任何併發的修改問題。只要添加/更新過程不會太重,就不會顯着干擾用戶體驗。

+0

謝謝,我曾經嘗試過,但也在那裏做網絡的東西,一旦我只是做UI的東西。 – 2013-02-20 02:52:02

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); 
+0

如果這是我的決定,我會使用新的地圖,但這是一個不幸的任務。 – 2013-02-20 02:15:33

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; 
    } 
} 
相關問題