Efficient Map Overlays in Android Google Map

Ahsan picture Ahsan · May 17, 2010 · Viewed 26.7k times · Source

I want to do the following and am kind of stuck on these for a few days:

  1. I was trying to draw polylines (I have encoded polylines, but have managed to decode those) that move when I move the map.
    The only solution that I found was for Geopoints to be transformed into screen coordinates... which won't move if I move the map.

  2. I used HelloItemizedOverlay to add about 150 markers and it gets very very slow.
    Any idea what to do? I was thinking about threads (handler).

  3. I was looking for some sort of a timer function that executes a given function periodically, say, every 1 minute or so.

  4. I was also looking for ways to clear the Google map from all the markers/lines, etc.

Answer

Ahsan picture Ahsan · May 24, 2010

Answers given below :

1) Here's a solution that I used :

/** Called when the activity is first created. */
private List<Overlay> mapOverlays;

private Projection projection;  

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    linearLayout = (LinearLayout) findViewById(R.id.zoomview);
    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    mapOverlays = mapView.getOverlays();        
    projection = mapView.getProjection();
    mapOverlays.add(new MyOverlay());        

}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

class MyOverlay extends Overlay{

    public MyOverlay(){

    }   

    public void draw(Canvas canvas, MapView mapv, boolean shadow){
        super.draw(canvas, mapv, shadow);

    Paint   mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);

        GeoPoint gP1 = new GeoPoint(19240000,-99120000);
        GeoPoint gP2 = new GeoPoint(37423157, -122085008);

        Point p1 = new Point();
        Point p2 = new Point();

    Path    path = new Path();

    Projection  projection.toPixels(gP1, p1);
        projection.toPixels(gP2, p2);

        path.moveTo(p2.x, p2.y);
        path.lineTo(p1.x,p1.y);

        canvas.drawPath(path, mPaint);
    }

courtesy: Drawing a line/path on Google Maps

2) Here's what worked for me :

createMarkers()
{ 
    for(elem:bigList)
    { 
        GeoPoint geoPoint = new GeoPoint((int)(elem.getLat()*1000000), (int) (elem.getLon()*1000000)); 
        OverlayItem overlayItem = new OverlayItem(geoPoint, elem.getName(), elem.getData()); 
        itemizedOverlay.addOverlay(overlayItem); 
    } 

    itemizedOverlay.populateNow(); 
    mapOverlays.add(itemizedOverlay); //outside of for loop 
} 

and in MyOverlay:

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

public void populateNow()
{
    populate(); 
}

courtesy: stackoverflow.com unknown link

3) The best way is to use a timer class. A very detailed description of the timer class and how to use it is given at this link :

http://life.csu.edu.au/java-tut/essential/threads/timer.html

4) I used this code :

if(!mapOverlays.isEmpty()) 
{ 
    mapOverlays.clear(); 
    mapView.invalidate(); 
} 

Hope these answers help atleast one other person. Thanks.