I am creating a map application that gets a list of coordinates from a JSON array. I @Override dispatchTouchEvent
to find the coordinates from the map, and then run ASYNCTask to get the new points.
It loads points, but after scrolling around it, the force closes. It would be ideal to clear the current points on the map and reload new points from the new location after the touch event.
Here is my code:
@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)
In a later note, I would like to have different icons for each point and a βtapβ event to display more detailed information about this issue. I spent about six hours on this last night and could not understand. The logarithm doesn't really say much.
Brandon wilson
source share