1

i have to put some overlays on map on specific point, i fetch the point coordinate from an API. i do the fetching in a asynctask. i succeeded to put the overlays, put when i do zoom on the map or span through it, the response is very slow. here's my overlayitem class and my asynctask

public class Marker extends ItemizedOverlay<OverlayItem> {
    private Context con;

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();


    public Marker(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
        // TODO Auto-generated constructor stub
    }

    public Marker(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        con=context;
        // TODO Auto-generated constructor stub
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return mOverlays.size();
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }
}

...

private class TeleportdAPIParser extends AsyncTask<Void, Object, Void> {

        private final HttpTransport http= AndroidHttp.newCompatibleTransport();
        private HttpRequestFactory fact;
        private HttpRequest request; 
        private HttpResponse response; 
        private String urlString;
        private JsonFactory jsonFactory; 
        private JsonParser jp;
        MapView mapView;
        Marker marker;
        List<Overlay> mapOverlays;
        ImageAdapter adapter;



        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mapView=(MapView) findViewById(R.id.mapView);
            Drawable drawable = getResources().getDrawable(R.drawable.puce);
            marker = new Marker(drawable);
            mapOverlays = mapView.getOverlays();
            adapter=new ImageAdapter();
            GridView gridview=(GridView) findViewById(R.id.gridView);


        }


        @Override
        protected  Void doInBackground(Void... params) {

            fact=http.createRequestFactory();
            jsonFactory = new JsonFactory();
            TPortItem tpi= new TPortItem();
            TPortItemList tpl=new TPortItemList();


                urlString="http://v1.api.teleportd.com:8080/search?apikey=1c5a31ccf46c54572e484e103c97239bd&loc=%5B48.87,2.34,5.0,5.0%5D";
                try {
                    //urlString=URLEncoder.encode(urlString,"UTF-8");
                    request = fact.buildGetRequest(new GenericUrl(urlString));
                    HttpHeaders header=new HttpHeaders();
                    header.setAcceptEncoding("gzip");
                    request.setHeaders(header);
                    response = request.execute();
                    jp = jsonFactory.createJsonParser(new BufferedInputStream(response.getContent()));


                    while(jp.nextValue()!=JsonToken.NOT_AVAILABLE){

                        jp.nextValue();
                        while(jp.getCurrentToken()!=JsonToken.END_OBJECT && jp.getCurrentToken()!=null){                            

                                if(jp.getCurrentName().equals("sha")){
                                    tpi.sha=jp.getText();
                                }

                                if(jp.getCurrentName().equals("date")){
                                    tpi.date=jp.getIntValue();          
                                }

                                if(jp.getCurrentName().equals("age")){
                                    tpi.age=jp.getIntValue();
                                }

                                if(jp.getCurrentName().equals("thumb")){
                                    tpi.thumb=jp.getText();
                                    if(adapter.URLS.size()<10)
                                        adapter.URLS.add(tpi.thumb);

                                }

                                if(jp.getCurrentName().equals("rank")){
                                    tpi.rank=jp.getIntValue();
                                }

                                if(jp.getCurrentName().equals("grade")){
                                    tpi.grade=jp.getIntValue();
                                }

                                if(jp.getCurrentName().equals("loc")){
                                    jp.nextValue().toString();
                                    tpi.loc[0]=(int) (jp.getFloatValue()*1E6);
                                    jp.nextValue().toString();
                                    tpi.loc[1]=(int) (jp.getFloatValue()*1E6);
                                    jp.nextValue();
                                    publishProgress(new GeoPoint (tpi.loc[0],tpi.loc[1]));

                                }

                                jp.nextValue(); 
                            }



                        //tpl.i.add(new TPortItem(tpi.sha, tpi.loc,tpi.age, tpi.date,tpi.thumb, tpi.rank, tpi.grade));


                    }   

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            return null;
        }

        @Override
        protected void onProgressUpdate(Object... values) {
            super.onProgressUpdate(values);
            marker.addOverlay(new OverlayItem((GeoPoint) values[0], "Hola, Mundo!", "I'm in Mexico City!"));
            mapOverlays.add(marker); // adding the whole overlays (list) on the maps


        }


        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            mapView.invalidate();
            //gridview.setAdapter(adapter);
        }
}
JJD
  • 50,076
  • 60
  • 203
  • 339
mauriyouth
  • 61
  • 1
  • 10

1 Answers1

3

There is a limit to the number of overlay items that MapView can handle. There is no official word on what this limit is, but after some while, you will see sluggish behaviour during zoom/pan.

Some solutions are :

  • LazyLoading of MapView
  • Load only as many markers that are visible, do this based on the zoom level and span.
  • 10 overlays with 99 points are faster than 1 overlay with 990 points.
Reno
  • 33,594
  • 11
  • 89
  • 102
  • Is **mapview-overlay-manager** outdated since the latest commit dates back to Sep 28, 2010? Is there an up-to-date alternative you know off? Is lazy loading integrated in the SDK? – JJD Jul 18 '12 at 19:24
  • @JJD I could not find any lazy loading in mapview integrated in the SDK. Do you feel that the 2010 implementation lacks something? – Reno Jul 18 '12 at 22:38