Question

Has anyone managed to use ItemizedOverlays in Android Beta 0.9? I can't get it to work, but I'm not sure if I've done something wrong or if this functionality isn't yet available.

I've been trying to use the ItemizedOverlay and OverlayItem classes. Their intended purpose is to simulate map markers (as seen in Google Maps Mashups) but I've had problems getting them to appear on the map.

I can add my own custom overlays using a similar technique, it's just the ItemizedOverlays that don't work.

Once I've implemented my own ItemizedOverlay (and overridden createItem), creating a new instance of my class seems to work (I can extract OverlayItems from it) but adding it to a map's Overlay list doesn't make it appear as it should.

This is the code I use to add the ItemizedOverlay class as an Overlay on to my MapView.

// Add the ItemizedOverlay to the Map
private void addItemizedOverlay() {
  Resources r = getResources();
  MapView mapView = (MapView)findViewById(R.id.mymapview);
  List<Overlay> overlays = mapView.getOverlays();

  MyItemizedOverlay markers = new MyItemizedOverlay(r.getDrawable(R.drawable.icon));
  overlays.add(markers);

  OverlayItem oi = markers.getItem(0);
  markers.setFocus(oi);
  mapView.postInvalidate();
}

Where MyItemizedOverlay is defined as:

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
  public MyItemizedOverlay(Drawable defaultMarker) {
    super(defaultMarker);
    populate();
  }

  @Override
  protected OverlayItem createItem(int index) {
    Double lat = (index+37.422006)*1E6;
    Double lng = -122.084095*1E6;
    GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());

    OverlayItem oi = new OverlayItem(point, "Marker", "Marker Text");
    return oi;
  }

  @Override
  public int size() {
    return 5;
  } 
}
Was it helpful?

Solution

For the sake of completeness I'll repeat the discussion on Reto's post over at the Android Groups here.

It seems that if you set the bounds on your drawable it does the trick:

Drawable defaultMarker = r.getDrawable(R.drawable.icon);

// You HAVE to specify the bounds! It seems like the markers are drawn
// through Drawable.draw(Canvas) and therefore must have its bounds set
// before drawing.
defaultMarker.setBounds(0, 0, defaultMarker.getIntrinsicWidth(),
    defaultMarker.getIntrinsicHeight());

MyItemizedOverlay markers = new MyItemizedOverlay(defaultMarker);
overlays.add(markers);

By the way, the above is shamelessly ripped from the demo at MarcelP.info. Also, here is a good howto.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top