Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with large number of markers on the map

I am working on an Android app that already exists on iPhone.

In the app, there is a Map activity that has (I counted) around 800 markers in four groups marked by drawable in four different colors. Each group can be turned on or off. Information about markers I have inside List. I create a mapOverlay for each group, then I attach that overlay to the map. I strongly believe that coding part I did properly. But I will attach my code anyway...

The thing is, my Nexus One can't handle map with all those markers. It takes around 15 seconds just to draw 500 markers. Then when all drawn, map is not quite smooth. It is sort of hard to zoom and navigate around. It can be done, but experience is bad and I would like to see if something can be done there. I know if I avoid String > Double conversion, I could save some time, but I doubt that would be significant.

iPhone seems doesn't have problems showing all these markers. It takes roughly about 1-2 seconds to show all of them and zooming and panning is not that bad. Slow down is noticeable but still acceptable. I personally think it is no good to draw all those markers, but app is designed by somebody else and I am not supposed to make any drastic changes.

I am not sure what to do here. It seems I will have to come up with different functionality, maybe use GPS location, if known, and draw only markers within some radius, or, if location not known, use center of the screen(map) and draw markers around that. I will have to have reasonable explanation for my bosses in case I make these changes.

I appreciate if anybody has any idas.

And the code:

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.pin_blue);
    drawable = this.getResources().getDrawable(R.drawable.pin_blue);
    ArrList = appState.GetSleepArrList();
    ItemizedOverlay itemizedoverlay = new ItemizedOverlay(drawable, this); 
    ...
    ...
       for (int m = 0; m < ArrList.size(); m++) {
            tName = ArrList.get(m).get("name").toString();
            tId = ArrList.get(m).get("id").toString();
            tLat = ArrList.get(m).get("lat").toString();;
            tLng = ArrList.get(m).get("lng").toString();;
            try {
                lat = Double.parseDouble(tLat);
                lng = Double.parseDouble(tLng);
                p1 = new GeoPoint(
                        (int) (lat * 1E6), 
                        (int) (lng * 1E6));
                OverlayItem overlayitem = new OverlayItem(p1, tName, tId);
                itemizedoverlay.addOverlay(overlayitem);
            } catch (NumberFormatException e) {
                Log.d(TAG, "NumberFormatException" + e);    
            }
        }
        mapOverlays.add(itemizedoverlay);
        mapView.postInvalidate();

................................

public class ItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    public HelloItemizedOverlay(Drawable defaultMarker, Context context)
    {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    public void addOverlay(OverlayItem overlay)
    {
        mOverlays.add(overlay);
        populate();
    }
    @Override
    protected OverlayItem createItem(int i)
    {
        return mOverlays.get(i);
    }
    @Override
    public int size()
    {
        return mOverlays.size();
    }
    @Override
    protected boolean onTap(int index)
    {
        final OverlayItem item = mOverlays.get(index);
                    ... EACH MARKER WILL HAVE ONCLICK EVENT THAT WILL PRODUCE CLICABLE 
                    ... BALOON WITH MARKER'S NAME.          
        return true;
    }       
}    
like image 673
bobetko Avatar asked Jan 17 '11 04:01

bobetko


2 Answers

Well. It turned out I call populate() every time after adding an marker. I took populate() out of addOverlay() method and I am calling it after loop finishes. All markers now show almost instantaneously. The only problem now is that the map is very unresponsive when at lower zoom (showing a lot of markers at once). I don't see solution for this unless I figure out how to decrease number of markers on the map when map is zoomed out... thinking to group them somehow.... currently working on this...

like image 134
bobetko Avatar answered Oct 16 '22 06:10

bobetko


There are two ways you can do this route simplification.

1) If you got the heading of all the points. You can keep a heading tolerance , say if the heading diffference between point A and B is 0.5 Just draw a line from A to B and ignore the points between.

2) If you dont have a heading. Make a triangle as shown in the fig. If h lies in between your limits, skip the points in between and draw a line from A to B enter image description here

You can also use some other route simplification algorithms.


See also :

Route simplification - Douglas Puecker Algorithm

like image 33
Reno Avatar answered Oct 16 '22 06:10

Reno