2011-12-19 86 views
0

我正在創建一個地圖應用程序,它接收來自JSON數組的座標列表。我@Override dispatchTouchEvent找到地圖上的座標,然後執行一個ASYNCTask來獲得新的點。Android MapView JSON數組添加點數組

它加載的點但滾動後,它強制關閉。最理想的是清除地圖上的當前點並在觸摸事件之後從新位置重新加載新點。

這是到目前爲止我的代碼:

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
    int actionType = ev.getAction(); 
    switch (actionType) { 
    case MotionEvent.ACTION_UP: 
     Projection proj = mapView.getProjection(); 
     GeoPoint loc = proj.fromPixels((int) ev.getX(), (int) ev.getY()); 
     String lng = Double.toString(loc.getLongitudeE6()/1e6); 
     String lat = Double.toString(loc.getLatitudeE6()/1e6); 

     new mapStations().execute(); 

     Toast.makeText(getApplicationContext(), 
       "Lat: " + lat + " Lng: " + lng, Toast.LENGTH_SHORT).show(); 
    } 

    return super.dispatchTouchEvent(ev); 
} 

private class mapStations extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     try { 
      JSONObject obj = new JSONObject(API.nearByStations(lat, lng, 0)); 
      JSONArray stations = obj.getJSONArray("stations"); 
      for (int j = 0; j < stations.length(); j++) { 
       JSONObject jsonObject = stations.getJSONObject(j); 
       add(jsonObject.getDouble("lat"), jsonObject.getDouble("lng")); 

      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
} 

public void add(double lat, double lng) { 

    mapOverlays = mapView.getOverlays(); 
    drawable = this.getResources().getDrawable(R.drawable.androidmarker); 
    itemizedOverlay = new MapsOverlay(drawable); 

    GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * 1e6)); 
    OverlayItem overlayitem = new OverlayItem(point, "", ""); 

    itemizedOverlay.addOverlay(overlayitem); 
    mapOverlays.add(itemizedOverlay); 
} 

的logcat:

12-18 22:52:01.076: E/AndroidRuntime(25072): FATAL EXCEPTION: main 
12-18 22:52:01.076: E/AndroidRuntime(25072): java.util.ConcurrentModificationException 
12-18 22:52:01.076: E/AndroidRuntime(25072): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:576) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at com.google.android.maps.OverlayBundle.draw(OverlayBundle.java:41) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at com.google.android.maps.MapView.onDraw(MapView.java:530) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.View.draw(View.java:6880) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.View.draw(View.java:6883) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.widget.FrameLayout.draw(FrameLayout.java:357) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.View.draw(View.java:6883) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.widget.FrameLayout.draw(FrameLayout.java:357) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1646) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.drawChild(ViewGroup.java:1644) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.View.draw(View.java:6883) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.widget.FrameLayout.draw(FrameLayout.java:357) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2106) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewRoot.draw(ViewRoot.java:1562) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewRoot.performTraversals(ViewRoot.java:1298) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.view.ViewRoot.handleMessage(ViewRoot.java:1911) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.os.Looper.loop(Looper.java:130) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at android.app.ActivityThread.main(ActivityThread.java:3821) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at java.lang.reflect.Method.invokeNative(Native Method) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at java.lang.reflect.Method.invoke(Method.java:507) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-18 22:52:01.076: E/AndroidRuntime(25072): at dalvik.system.NativeStart.main(Native Method) 

在後來的筆記,我想對每一個點和一個水龍頭事件不同的圖標,彈出詳細資料關於這一點用戶點擊。我昨晚在這個晚上花了大約六個小時,但無法弄清楚。 logcat實際上並沒有多大意義。

回答

1

您應該在UI線程中添加項目,而不是在後臺。

+0

我不能相信我過來看了那個作爲最後一道防線。你知道一個學習如何添加不同引腳和標籤事件的好地方嗎?我需要這些引腳來顯示不同的信息,具體取決於JSON帶回的內容。 – 2011-12-19 18:46:28

0

嘗試使用AsyncTask的onPostExecute方法。它與UI線程相關,因此您可以修改MapView。

讓我們將add方法從doInBackground移動到onPostExecute。

此外,你應該添加

mapView.invalidate() 

方法onPostExecute()