Android的图形页面JSON阵列添加点的数组 [英] Android MapView JSON Array Adding Array of points

查看:83
本文介绍了Android的图形页面JSON阵列添加点的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建接收从JSON数组COORDS列表的地图应用程序。我 @覆盖dispatchTouchEvent 来找到地图中COORDS,然后执行的AsyncTask来获得新的点。

I am creating a map application that receives a list of coords from a JSON array. I @Override dispatchTouchEvent to find the coords from the map and then execute an ASYNCTask to get the new points.

据加载点,但它周围的滚动力结束后。什么是理想的是将触摸事件之后,从一个新的位置,清除地图上的当前点和重新加载新的点。

It loads the points but after scrolling around it force closes. What would be ideal is to clear the current points on the map and reload new points from a new location after the touch event.

下面是我的code迄今:

Here is my code so far:

@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中:

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中并没有真正告诉了。

In a later note I would like to have different icons for each point and a tap event to bring up more details about that point the user taps. I spent about six hours on this last night and could not figure it out. The logcat doesn't really tell much.

推荐答案

您应该添加的项目在UI线程中,而不是在后台。

You should add the items in the UI thread, not in background.

这篇关于Android的图形页面JSON阵列添加点的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆