Misca
Misca

Reputation: 469

ItemizedOverlay with only one call to populate();

I want to optimize my code by calling populate(); only after adding all the OverlayItems.

public class ZoneBase extends ItemizedOverlay {

//------- Class base members

//Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
boolean shadow=false;
int gameType;
Context mContext;

//----------------------------------

    public ZoneBase(Drawable Marker,ZoneProperties z,Context context,boolean ev) {

    super(boundCenter(Marker));

    OverlayItem overlayitem = new OverlayItem(z.point, "Hello", "Stefan cel Mare!");

    addOverlay(overlayitem);

    Marker=null;
    if(ev)
        gameType = z.gameType;
    //System.gc();

    mContext = context;
}

In the contructor i have to call the super-method with the drawable first but what i would really need it would be to pass to this class an array and create the drawable inside a for and add it, then i would call populate();. Maybe i should extend some other class rather than ItemizedOverlay. Thank you! :)

Upvotes: 0

Views: 510

Answers (1)

Grigori A.
Grigori A.

Reputation: 2608

If you want to set different markers you should do it inside the constructor. Here is an example.

private someMethod() {
    final MapView mapView = (MapView) findViewById(...);
    final LiveOverlay liveOverlay = new LiveOverlay();
    mapView.getOverlays().clear();
    mapView.getOverlays().add(liveOverlay);
}

private class LiveOverlay extends ItemizedOverlay<OverlayItem> {

    private LiveOverlay() {
        super(null);

        final ArrayList<OverlayItem> locationsList = new ArrayList<OverlayItem>();
        for (...) {
            final GeoPoint geoPoint = new GeoPoint(...);
            final OverlayItem overlayItem = new OverlayItem(geoPoint, ...);
            final Drawable marker = ...;
            marker.setBounds(...);
            overlayItem.setMarker(marker);
            locationsList.add(overlayItem);
        }
        setLastFocusedIndex(-1);
        populate();
    }

    @Override
    public int size() {
        return locationsList.size();
    }

    @Override
    protected OverlayItem createItem(int i) {
        return locationsList.get(i);
    }

    @Override
    public void draw(Canvas canvas, MapView mView, boolean shadow) {
        super.draw(canvas, mView, false);
    }
}

Upvotes: 1

Related Questions