我正在创建一个地图应用程序,它接收来自JSON数组的coords列表。我@Override dispatchTouchEvent
从地图中找到坐标,然后执行ASYNCTask获取新点。
它加载点但在滚动之后强制关闭。理想的是清除地图上的当前点并在触摸事件后从新位置重新加载新点。
到目前为止,这是我的代码:
@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并没有太多说明。
答案 0 :(得分:1)
您应该在UI线程中添加项目,而不是在后台。
答案 1 :(得分:0)
尝试使用AsyncTask的onPostExecute方法。它与UI线程有关,因此您可以修改MapView。
让我们将doInBackground中的add方法移动到onPostExecute。
此外,你应该添加
mapView.invalidate()
作为onPostExecute()
方法的最后一行