How to Effectively Use ItemizedOverlay
and OverlayItem
in Android Beta 0.9
If you’re a developer working with Android, you might have encountered challenges when trying to implement ItemizedOverlay
and OverlayItem
features in your map applications, particularly in Android Beta 0.9. This article aims to explore a common problem in this context and present a detailed solution that will help you get those map markers up and running smoothly.
The Problem
While trying to utilize ItemizedOverlay
to create markers on your map (similar to those seen in Google Maps), you may have noticed that markers do not appear as expected. Here’s the scenario you might be familiar with:
- You’ve set up your
MyItemizedOverlay
class which extendsItemizedOverlay
. - When you attempt to add this overlay to your
MapView
, it doesn’t display any markers despite successfully extracting theOverlayItems
.
Here’s a snippet of the code you might be using to add the overlay:
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();
}
Class Definition
Your MyItemizedOverlay
class could be defined as follows:
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;
}
}
The Solution: Setting Bounds on Your Drawable
The key issue often lies in the absence of defined bounds for the Drawable
associated with your markers. Here’s how to resolve this:
Step 1: Set Drawable Bounds
Before you add your ItemizedOverlay
, make sure you specify the bounds for your drawable. This is crucial because the draw
method of drawable relies on these bounds to render correctly on the canvas. Here’s how you can do it:
Drawable defaultMarker = r.getDrawable(R.drawable.icon);
// Set the bounds for your drawable
defaultMarker.setBounds(0, 0, defaultMarker.getIntrinsicWidth(),
defaultMarker.getIntrinsicHeight());
MyItemizedOverlay markers = new MyItemizedOverlay(defaultMarker);
overlays.add(markers);
Why are Bounds Important?
- Rendering Mechanics: The markers are drawn through the
Drawable.draw(Canvas)
method, which requires bounds to be specified so that the drawable knows where to render itself on the canvas. - Visibility: Without proper bounds, your markers may not appear on the map, leading to confusion during the development process.
Additional Resources
For further reference and a detailed walkthrough, you can check out the following resources:
Conclusion
By properly setting the bounds on your Drawable
, you should find that your ItemizedOverlay
works as intended, displaying the markers on your map. This fix is essential for developers facing similar issues in Android Beta 0.9, ensuring a smoother development experience as you implement map features in your applications.
If you have further questions or want to share your experiences with implementing ItemizedOverlay
, feel free to comment below!