Recipe Adding Markers on a

The itemizedOverlay class provides a way to draw markers and layovers on top of a MapView. It manages a set of OverlayItem elements, such as an image, in a list and handles the drawing, placement, click handling, focus control, and layout optimization for each element. Create a class that extends ItemizedOverlay and override the following:

■ addOverlay()—Adds an Overlayltem to the ArrayList.This calls populate(), which reads the item and prepares it to be drawn.

■ createItem()—Called by populate() to retrieve the given Overlayltem.

■ size()—Returns the number of Overlayltem elements in the ArrayList.

■ onTap() —Callback method when a marker is clicked.

The newly created class is given in Listing 10.11 and provides the result in Figure 10.5. Listing 10.11 src/com/cookbook/adding_markers/MyMarkerLayerjava package com.cookbook.adding_markers;

import java.util.ArrayList;


import android.content.Dialoglnterface;


import; import;

public class MyMarkerLayer extends ItemizedOverlay {

private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

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

public void addOverlayItem(OverlayItem overlay) { mOverlays.add(overlay); populate();

@Override protected OverlayItem createItem(int i) { return mOverlays.get(i);

@Override public int size() {

return mOverlays.size();

@Override protected boolean onTap(int index) {

AlertDialog.Builder dialog =

new AlertDialog.Builder(MyLocation.mContext); dialog.setTitle(mOverlays.get(index).getTitle()); dialog.setMessage(mOverlays.get(index).getSnippet()); dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel();

dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel();;

return super.onTap(index);



Figure 10.5 Adding a clickable marker to a map.

A few comments on the MyMarkerLayer class that are highlighted in Listing 10.11:

■ An OverlayItem container mOverlays is declared to save all the items passed to the Overlay.

■ A binding point for where all overlaid items are attached to the map needs to be defined before any overlay item is drawn.To specify the bottom center of the map as that point, boundCenterBottom is added to the class constructor.

■ The required methods are overridden: addOverlay(), createItem(), size(), and onTap(). Here, the onTap() method provides a dialog box when the item is clicked.

■ The populate() method is added to the end of the constructor and addOverlay() .This tells the MyMarkerLayer class to prepare all OverlayItem elements and draw each one on the map.

Now, this ItemizedOverlay can be added to the MapActivity created in the previous recipe. As highlighted in Listing 10.12, the activity:

1. Retrieves the existing map overlay items using the getOverlays() method from MapView. The marker layer is added to this container at the end of the function.

2. Defines an instance of the MyMarkerLayer to handle the overlay items.

3. Retrieves the latitude and longitude (in degrees) of the address.This defines the point of interest using a GeoPoint class. GeoPoint takes input in microdegrees, so the latitude and longitude each needs to be multiplied by one million (1E6).

4. Uses a map controller to animate to the GeoPoint and zoom the view. Also, it enables user-controlled zoom using setBuiltInZoomControls().

5. Defines an OverlayItem as a message at the GeoPoint of interest.

6. Adds the item to the MyMarkerLayer using the addOverlayItem() method. It then puts the now defined MyMarkerLayer into the existing overlay list retrieved in step 1.

Character Building Thought Power

Character Building Thought Power

Character-Building Thought Power by Ralph Waldo Trine. Ralph draws a distinct line between bad and good habits. In this book, every effort is made by the writer to explain what comprises good habits and why every one needs it early in life. It draws the conclusion that habits nurtured in early life concretize into impulses in future for the good or bad of the subject.

Get My Free Ebook

Post a comment