Drawable marker

super (defaultMarker) ; <l this.items = items; this.context = context; this.populate();

@Override public BuoyOverlayItem createItem(int i) {

} O createItem

@Override protected boolean onTap(int i) { © Get data final BuoyData bd = this . items .get (i) .buoyData; <-I and display

LayoutInflater inflater = LayoutInflater.from(this.context); View bView = inflater.inflate(R.layout.buoy_selected, null); TextView title = (TextView) bView.findViewById(R.id.buoy_title);

. . . rest of view inflation omitted for brevity new AlertDialog.Builder(this.context) .setView(bView)

.setPositiveButton("More Detail", new DialogInterface.OnClickListener() { public void onClick(DialogInterface di, int what) { Intent intent =

new Intent(context, BuoyDetailActivity.class) ; BuoyDetailActivity.buoyData = bd; context.startActivity(intent);

.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface di, int what) { di.dismiss();

return true;

@Override public int size () { 0 Override return this . items . size () ; <-1 size method

H Include other methods if needed

@Override public void draw(Canvas canvas, MapView mapView, boolean b) { < super.draw(canvas, mapView, false);

The BuoyItemizedOverlay class extends ItemizedOverlay O and includes a Collection of BuoyOverlayItem elements ©. In the constructor we pass the Drawable marker to the parent class ©. This marker is what is drawn on the screen in the overlay to represent each point on the map.

ItemizedOverlay takes care of many of the details we would have to tackle ourselves if we weren't using it (if we were just making our own Overlay with multiple points drawn on it). This includes the drawing of items and focus and event handling. For every element in the Collection of items an ItemizedOverlay holds, it invokes the onCreate method O, and it supports facilities like onTap ©, where we can react when a particular overlay item is selected by the user. In our code we inflate some views and display an AlertDialog with information about the respective buoy when a BuoyOverlayltem is tapped. From the alert, the user can navigate to more detailed information if desired.

The size method tells ItemizedOverlay how many elements it needs to process ©, and even though we aren't doing anything special with it in our case, there are also methods like onDraw that can be customized if necessary ©.

When working with a MapView you create the Overlay instances you need, then add them on top of the map. Wind and Waves uses a separate Thread to retrieve the buoy data in the MapViewActivity (the data-retrieval code is not shown but is included in the code download for this chapter), and when ready we send a Message to a Handler to add the BuoyItemizedOverlay to the MapView. These details are shown in listing 11.10.

0 0

Post a comment