Using ItemizedOverlay and OverlayItem in Android Beta 0.9

ItemizedOverlays ever managed to use ItemizedOverlays in Android Beta 0.9? I can't get it to work, but I'm not sure I did something wrong or this feature is not yet available.

I tried using ItemizedOverlay and OverlayItem . Their purpose is to simulate map markers (as seen in Google Maps Mashups), but I had problems displaying them on the map.

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

After I implemented my own ItemizedOverlay (and redefined createItem ), it seems that creating a new instance of my class works (I can extract OverlayItems from it), but adding it to the Overlay list of the map does not make it look like this as it should.

This is the code that I use to add the ItemizedOverlay class as Overlay 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; } } 
+52
java android overlay android-mapview
Aug 25 '08 at 16:39
source share
1 answer

To complete the picture, I will repeat the discussion of the Reto post in Android groups here .

It seems that if you set the borders for your drawing, this 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 torn from the demo on MarcelP.info . In addition, here is a good instruction .

+40
Sep 05 '08 at 19:58
source share



All Articles