0

我正在制作一个应用程序,在该应用程序中,我使用 JSON 将位置获取到地图中,但我仍在获取地图,但没有 JSON 数据来显示多个位置,我的代码没问题,它工作正常,我只是想知道你们所有的专家在哪里我需要将此行显示 JSON 数据到 Mapview:

 new DownloadWebPageTask().execute();

以下是我的代码:-

public class MapView extends MapActivity {
public GeoPoint point;
TapControlledMapView mapView=null; // use the custom TapControlledMapView
List<Overlay> mapOverlays;
Drawable drawable;
SimpleItemizedOverlay itemizedOverlay;

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_view);
//  new DownloadWebPageTask().execute();
    mapView = (TapControlledMapView) findViewById(R.id.viewmap);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(false);

    // dismiss balloon upon single tap of MapView (iOS behavior) 
    mapView.setOnSingleTapListener(new OnSingleTapListener() {      

        public boolean onSingleTap(MotionEvent e) {
            itemizedOverlay.hideAllBalloons();
            return true;

        }
    });

    mapOverlays = mapView.getOverlays();        
    drawable = getResources().getDrawable(R.drawable.ic_launcher);
    itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);         
    itemizedOverlay.setShowClose(false);
    itemizedOverlay.setShowDisclosure(true);
    itemizedOverlay.setSnapToCenter(false);

    class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            HttpClient client = new DefaultHttpClient();
            // Perform a GET request for a JSON list
            HttpUriRequest request = new HttpGet("http://***.in/map.json");
            // Get the response that sends back
            HttpResponse response = null;
            try {
                response = client.execute(request);
            } catch (ClientProtocolException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Convert this response into a readable string
            String jsonString = null;
            try {
                jsonString = StreamUtils.convertToString(response.getEntity().getContent());
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            // Create a JSON object that we can use from the String
            JSONObject json = null;
            try {
                json = new JSONObject(jsonString);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            try{
                JSONArray jsonArray = json.getJSONArray("maps");
                Log.e("log_tag", "Opening JSON Array ");
                for(int i=0;i < jsonArray.length();i++){                      
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String latitude =  jsonObject.getString("latitude");
                    String longitude =  jsonObject.getString("longitude");
                    String title =  jsonObject.getString("title");
                    String country = jsonObject.getString("country");
                    double lat = Double.parseDouble(latitude);
                    double lng = Double.parseDouble(longitude);
                    Log.e("log_tag", "ADDING GEOPOINT"+title); 
                    point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
                    OverlayItem overlayItem = new OverlayItem(point, title, country);
                    itemizedOverlay.addOverlay(overlayItem);
               }
           }catch(JSONException e)        {
               Log.e("log_tag", "Error parsing data "+e.toString());
           } 


           return jsonString;
       }

           @Override
        protected void onPostExecute(String result) {
                    itemizedOverlay.populateNow(); 

           mapOverlays.add(itemizedOverlay);
           if (savedInstanceState == null) {
               MapController controller = mapView.getController();
               controller.setCenter(point);
               controller.setZoom(7);
           } else {
               // example restoring focused state of overlays
               int focused;
               focused = savedInstanceState.getInt("focused_1", -1);
               if (focused >= 0) {
                   itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
               }
           }
              }

    }

    }
4

1 回答 1

0

很明显,您应该在初始化地图后调用 execute 方法。我认为你的代码应该是这样的:

public class Test extends MapActivity {
    public GeoPoint point;
    MapView mapView=null; // use the custom TapControlledMapView
    List<Overlay> mapOverlays;
    Drawable drawable;
    SimpleItemizedOverlay itemizedOverlay;

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_view);
        //  new DownloadWebPageTask().execute();
        mapView = (TapControlledMapView) findViewById(R.id.viewmap);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(false);

        // dismiss balloon upon single tap of MapView (iOS behavior) 
        mapView.setOnSingleTapListener(new OnSingleTapListener() {      

            public boolean onSingleTap(MotionEvent e) {
                itemizedOverlay.hideAllBalloons();
                return true;

            }
        });

        mapOverlays = mapView.getOverlays();        
        drawable = getResources().getDrawable(R.drawable.ic_launcher);
        itemizedOverlay = new SimpleItemizedOverlay(drawable, mapView);         
        itemizedOverlay.setShowClose(false);
        itemizedOverlay.setShowDisclosure(true);
        itemizedOverlay.setSnapToCenter(false);

        new DownloadWebPageTask().execute();

    }

    class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            HttpClient client = new DefaultHttpClient();
            // Perform a GET request for a JSON list
            HttpUriRequest request = new HttpGet("http://***.in/map.json");
            // Get the response that sends back
            HttpResponse response = null;
            try {
                response = client.execute(request);
            } catch (ClientProtocolException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Convert this response into a readable string
            String jsonString = null;
            try {
                jsonString = StreamUtils.convertToString(response.getEntity().getContent());
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            // Create a JSON object that we can use from the String
            JSONObject json = null;
            try {
                json = new JSONObject(jsonString);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            try{
                JSONArray jsonArray = json.getJSONArray("maps");
                Log.e("log_tag", "Opening JSON Array ");
                for(int i=0;i < jsonArray.length();i++){                      
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String latitude =  jsonObject.getString("latitude");
                    String longitude =  jsonObject.getString("longitude");
                    String title =  jsonObject.getString("title");
                    String country = jsonObject.getString("country");
                    double lat = Double.parseDouble(latitude);
                    double lng = Double.parseDouble(longitude);
                    Log.e("log_tag", "ADDING GEOPOINT"+title); 
                    point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
                    OverlayItem overlayItem = new OverlayItem(point, title, country);
                    itemizedOverlay.addOverlay(overlayItem);
                }
            }catch(JSONException e)        {
                Log.e("log_tag", "Error parsing data "+e.toString());
            } 


            return jsonString;
        }

        @Override
        protected void onPostExecute(String result) {
            itemizedOverlay.populateNow(); 

            mapOverlays.add(itemizedOverlay);
            if (savedInstanceState == null) {
                MapController controller = mapView.getController();
                controller.setCenter(point);
                controller.setZoom(7);
            } else {
                // example restoring focused state of overlays
                int focused;
                focused = savedInstanceState.getInt("focused_1", -1);
                if (focused >= 0) {
                    itemizedOverlay.setFocus(itemizedOverlay.getItem(focused));
                }
            }
        }

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}
于 2012-10-31T08:07:03.547 回答