Listing 1110 The Handler Wind and Waves uses to add overlays to the Map View

private final Handler handler = new Handler() {

public void handleMessage(final Message msg) {


f (mapView.getOverlays() mapView. getOverlays (). remove (buoyOverlay) ; <1-' already present if (mapView.getOverlays () .contains (buoyOverlay) ) { O R_em°ve Overlay if

buoyOverlay = new BuoyItemizedOverlay(buoys, defaultMarker, Q Create

MapViewActivity. this) ; <-1 BuoyItemizedOverlay mapView. getOverlays (). add (buoyOverlay) ; <1—n Add Overlay ^ ^ } © to MapView

A MapView contains a Collection of Overlay elements, and so you can remove previous elements if you need to. We use the remove method to clean up any existing Buoy-OverlayItem class O before we create Q and add a new one ©. This way we aren't simply adding more items on top of each other; rather we are resetting the data.

The built-in Overlay subclasses have handled our requirements here perfectly, which is very helpful. The ItemizedOverlay and OverlayItem classes have allowed us to complete the Wind and Waves application without having to make our own Overlay subclasses directly. Keep in mind, if you need to, you can go to that level and implement your own draw, tap, touch, and so on methods within your custom Overlay.

With our sample application now complete and providing us with buoy data using a MapActivity and MapView, we next need to address one additional maps-related concept that we haven't yet encountered but is nonetheless very important—geocoding.

0 0

Post a comment