Introducing Itemized Overlays and Overlay Items

Overlayitems are used to supply simple maker functionality to your Map Views via the ItemizedOverlay class.

itemizedOverlays provide a convenient shortcut for adding markers to a map, letting you assign a marker image and associated text to a particular geographical position. The ItemizedOverlay instance handles the drawing, placement, click handling, focus control, and layout optimization of each Overlayitem marker for you.


To add an itemizedOverlay marker layer to your map, start by creating a new class that extends ItemizedOverlay<OverlayItem>, as shown in Listing 8-12.

ItemizedOverlay is a generic class that lets you create extensions based on any Overlayltem-iferaW subclass.

Within the constructor you need to call through to the superclass after defining the bounds for your default marker. You must then call populate to trigger the creation of each OverlayItem; populate must be called whenever the data used to create the items changes.

Within the implementation, override size to return the number of markers to display and createItem to create a new item based on the index of each marker.

LISTING 8-12: Creating a new Itemized Overlay Available for download on import; import;

import; import;

public class MyltemizedOverlay extends ItemizedOverlay<OverlayItem> {

public MyItemizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); populate();

©Override protected OverlayItem createItem(int index) { switch (index) { case 1:

Double lat = 37.422006*1E6; Double lng = -122.084095*1E6;

GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue()); OverlayItem oi;

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

return null;

©Override public int size() {

// Return the number of markers in the collection return 1;

To add an ItemizedOverlay implementation to your map, create a new instance (passing in the Draw-able marker image to use for each marker) and add it to the map's Overlay list.

List<Overlay> overlays = mapView.getOverlays(); MyItemizedOverlay markers = new

MyItemizedOverlay(r.getDrawable(R.drawable.marker)); overlays.add(markers);

Note that the map markers placed by the Itemized Overlay use state to indicate if they are selected. Use the stateListDrawable described in Chapter 4 to indicate when a marker has been selected.

In Listing 8-12, the list of Overlay items is static and defined in code. More typically your Overlay items will be a dynamic ArrayList to which you will want to add and remove items at run time.

Listing 8-13 shows the skeleton class for a dynamic Itemized Overlay implementation, backed by an ArrayList, and supporting the addition and removal of items at run time.

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


  • nasih
    What is itemizes overlay on android?
    1 year ago

Post a comment