0

I am making an app in which i am fetching locations using JSON into Map,still i am getting map but without JSON Data to show multiple locations, my code is ok and it works fine, i just want to know by all of you experts where i need to put this line to show JSON data into Mapview:

 new DownloadWebPageTask().execute();

Below is my CODE:-

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));
               }
           }
              }

    }

    }
Stanley
  • 319
  • 1
  • 2
  • 8

1 Answers1

0

It is obvious you should call execute method after you initialize your map. I think your code should be like that:

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;
    }
}
Khawar Raza
  • 15,870
  • 24
  • 70
  • 127
  • Khawar could you please show me how and where by writing required lines, because i have tried several times but getting problem everytime – Stanley Oct 31 '12 at 09:03
  • Khawar Thanks buddy, you can't imagine where you and your answer stands for me, because i have done 90% project of mine, but facing two problems for last 6 days, one of them you have sorted today, if you are ok so can i ask another one which is last....Thanks..Thanks..Thanks..Thanks..Thanks..Thanks..Thanks>>>> God Bless You – Stanley Oct 31 '12 at 10:01
  • Thank a lot for Welcome, Actually bro, i am making a program in which fetching json data into listview, then showing selected listview item row in another activity with Image, Title, Cost and here i am also allowing user to enter a quantity now i want whenever user will click on Add to Order button, then selected item's title, cost and entered quantity need to show in another activity, i am able to show those records in another activity but only for single selection and not for complete session like ViewCart in Shopping Cart app..so i am not getting how can i do that....Please help – Stanley Oct 31 '12 at 12:01
  • I think u want to let user add multiple items by action sequence like: select item --> select quantity and add item to cart --> back to listview --> select item --> select quantity and add item to cart --> finally submit the order. Is this the sequence you want to implement...??? – Khawar Raza Oct 31 '12 at 12:09
  • Khawar buddy can i have your email address – Stanley Oct 31 '12 at 12:22