Книга: Beginning Android

Drawing the ItemizedOverlay

Drawing the ItemizedOverlay

As the name suggests, ItemizedOverlay allows you to supply a list of points of interest to be displayed on the map — specifically, instances of OverlayItem. The overlay, then, handles much of the drawing logic for you. Here are the minimum steps to make this work:

• First, override ItemizedOverlayOverlayItem as your own subclass (in this example, SitesOverlay)

• In the constructor, build your roster of OverlayItem instances, and call populate() when they are ready for use by the overlay

• Implement size() to return the number of items to be handled by the overlay

• Override createItem() to return OverlayItem instances given an index

• When you instantiate your ItemizedOverlay subclass, provide it with a Drawable that represents the default icon (e.g., push-pin) to display for each item

The marker from the NooYawk constructor is the Drawable used for the last bullet — it shows a push-pin, as illustrated in Figure 34-1 earlier in this chapter.

You may also wish to override draw() to do a better job of handling the shadow for your markers. While the map will handle casting a shadow for you, it appears you need to provide a bit of assistance for it to know where the “bottom” of your icon is, so it can draw the shadow appropriately.

For example, here is SitesOverlay:

private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
 private List<OverlayItem> items = new ArrayList<OverlayItem>();
 private Drawable marker = null;
 public SitesOverlay(Drawable marker) {
  super(marker);
  this.marker = marker;
  items.add(new OverlayItem(getPoint(40.748963847316034,
   -73.96807193756104), "UN", "United Nations"));
  items.add(new OverlayItem(getPoint(40.76866299974387,
   -73.98268461227417), "Lincoln Center",
   "Home of Jazz at Lincoln Center"));
  items.add(new OverlayItem(getPoint(40.765136435316755,
   -73.97989511489868), "Carnegie Hall",
   "Where you go with practice, practice, practice"));
  items.add(new OverlayItem(getPoint(40.70686417491799,
   -74.01572942733765), "The Downtown Club",
   "Original home of the Heisman Trophy"));
  populate();
 }
 @Override
 protected OverlayItem createItem(int i) {
  return(items.get(i));
 }
 @Override
 public void draw(Canvas canvas, MapView mapView,
  boolean shadow) {
  super.draw(canvas, mapView, shadow);
  boundCenterBottom(marker);
 }
 @Override
 protected boolean onTap(int i) {
  Toast.makeText(NooYawk.this,
   items.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
  return(true);
 }
 @Override
 public int size() {
  return(items.size());
 }
}

Оглавление книги

Оглавление статьи/книги

Генерация: 1.100. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз