开发者

ItemizedOverlay only shows FIRST ENTRY

i followed the tutorial on Google Dev to create an ItemSizedOverlay. My idea is to get several positions from an ArrayList called list. So i used a for() to read out the ArrayList list and add the positions to the ItemSizedOverlay called PowerStationOverlay - but the strange thing is that this gives me only the first entry of my list - the other ones are not shown on the map...

Id I read in static positions like in the tutorial - it works fine, but when i read in the positions through the ArrayList and the for() which should iterate over it - then it doesnt work.

Im confused i dont understand this - whats wrong?

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.powerstation);
    MapView mapView = (MapView) findViewById(R.id.mapview);

    List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
    PowerStationOverlay itemizedoverlay = new PowerStationOverlay(drawable);
    ArrayList<locations.PowerStation> list = new ArrayList<locations.PowerStation>()开发者_Python百科;
    list = myDbHelper.getPowerStations();
    int countPowerStations = list.size();
    GeoPoint gp;

    for(locations.PowerStation ps : list){
        int lat = (int) ps.getLat();
        int lon = (int) ps.getLon();

            gp = new GeoPoint(lat, lon);

            itemizedoverlay.addOverlay(new OverlayItem(gp, "Test", "Test"));

    }

mapOverlays.add(itemizedoverlay);

}

And here is the ItemSizedOverlay called PowerStationOverlay.

    public class PowerStationOverlay extends ItemizedOverlay<OverlayItem>
{
    Context mContext;

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

    public PowerStationOverlay(Drawable defaultMarker) {
        super(boundCenterBottom(defaultMarker));
    }

    public PowerStationOverlay(Drawable defaultMarker, Context context) {
        super(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) {
  OverlayItem item = mOverlays.get(index);

  return true;
}

}


Do not call populate() on every addOverlay() call. Call populate() once your ArrayList is populated. Here is a sample project demonstrating this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜