source: other-projects/tipple-android/tipple-ar/tipple-standalone-hpg/src/org/mapsforge/android/maps/ItemizedMarkerOverlay.java@ 26528

Last change on this file since 26528 was 26528, checked in by davidb, 11 years ago

Code developed by the Tipple-AR Smoke and Mirrors team, based on the main tipple trunk

File size: 5.4 KB
Line 
1package org.mapsforge.android.maps;
2
3
4
5import org.mapsforge.android.maps.overlay.Overlay;
6import org.mapsforge.core.GeoPoint;
7
8import android.graphics.Canvas;
9import android.graphics.Color;
10import android.graphics.Paint;
11import android.graphics.Paint.Style;
12import android.graphics.Point;
13import android.graphics.Rect;
14import android.graphics.drawable.Drawable;
15
16/**
17 * ItemizedMarkerOverlay is an abstract base class to display a list of OverlayMarkerItems.
18 *
19 * @param <Item>
20 * the type of items handled by this Overlay.
21 */
22public abstract class ItemizedMarkerOverlay<Item extends OverlayMarkerItem> extends Overlay
23{
24 private static final String THREAD_NAME = "ItemizedMarkerOverlay";
25
26 /**
27 * Sets the bounds of the given drawable so that (0,0) is the center of the bottom row.
28 *
29 * @param balloon
30 * the drawable whose bounds should be set.
31 * @return the given drawable.
32 */
33 protected static Drawable boundCenter(Drawable balloon) {
34 balloon.setBounds(balloon.getIntrinsicWidth() / -2, balloon.getIntrinsicHeight() / -2,
35 balloon.getIntrinsicWidth() / 2, balloon.getIntrinsicHeight() / 2);
36 return balloon;
37 }
38
39 /**
40 * Sets the bounds of the given drawable so that (0,0) is the center of the bounding box.
41 *
42 * @param balloon
43 * the drawable whose bounds should be set.
44 * @return the given drawable.
45 */
46 protected static Drawable boundCenterBottom(Drawable balloon) {
47 balloon.setBounds(balloon.getIntrinsicWidth() / -2, -balloon.getIntrinsicHeight(),
48 balloon.getIntrinsicWidth() / 2, 0);
49 return balloon;
50 }
51
52 private int bottom;
53 private final Drawable defaultMarker;
54 private Drawable itemMarker;
55 private final Point itemPosition;
56 private int left;
57 private Rect markerBounds;
58 private int numberOfItems;
59 private Item overlayItem;
60 private int right;
61 private int top;
62
63 /**
64 * Constructs a new ItemizedOverlay.
65 *
66 * @param defaultMarker
67 * the default marker for each item.
68 */
69 public ItemizedMarkerOverlay(Drawable defaultMarker) {
70 this.defaultMarker = defaultMarker;
71 this.itemPosition = new Point();
72 }
73
74
75
76 /**
77 * Returns the numbers of items in this Overlay.
78 *
79 * @return the numbers of items in this Overlay.
80 */
81 public abstract int size();
82
83 /**
84 * Creates an item in the Overlay.
85 *
86 * @param i
87 * the index of the item.
88 * @return the item.
89 */
90 protected abstract Item createItem(int i);
91
92 @Override
93 protected synchronized void drawOverlayBitmap(Canvas canvas, Point drawPosition,
94 Projection projection, byte drawZoomLevel) {
95 this.numberOfItems = size();
96 if (this.numberOfItems < 1) {
97 // no items to draw
98 return;
99 }
100
101 // draw the Overlay items
102 for (int i = 0; i < this.numberOfItems; ++i) {
103 // get the current item
104 this.overlayItem = createItem(i);
105
106 // check if the item has a position
107 if (this.overlayItem.getPoint() == null) {
108 continue;
109 }
110
111 // make sure that the cached item position is valid
112 if (drawZoomLevel != this.overlayItem.cachedZoomLevel) {
113 this.overlayItem.cachedMapPosition = projection.toPoint(this.overlayItem
114 .getPoint(), this.overlayItem.cachedMapPosition, drawZoomLevel);
115 this.overlayItem.cachedZoomLevel = drawZoomLevel;
116 }
117
118 // calculate the relative item position on the display
119 this.itemPosition.x = this.overlayItem.cachedMapPosition.x - drawPosition.x;
120 this.itemPosition.y = this.overlayItem.cachedMapPosition.y - drawPosition.y;
121
122 // get the correct marker for the item
123 if (this.overlayItem.getMarker() == null) {
124 this.itemMarker = this.defaultMarker;
125 } else {
126 this.itemMarker = this.overlayItem.getMarker();
127 }
128
129 // get the position of the marker
130 this.markerBounds = this.itemMarker.copyBounds();
131
132 // calculate the bounding box of the marker
133 this.left = this.itemPosition.x + this.markerBounds.left;
134 this.right = this.itemPosition.x + this.markerBounds.right;
135 this.top = this.itemPosition.y + this.markerBounds.top;
136 this.bottom = this.itemPosition.y + this.markerBounds.bottom;
137
138 // check if the bounding box of the marker intersects with the canvas
139 if (this.right >= 0 && this.left <= canvas.getWidth() && this.bottom >= 0
140 && this.top <= canvas.getHeight()) {
141 // set the position of the marker
142 this.itemMarker.setBounds(this.left, this.top, this.right, this.bottom);
143
144 // draw the item marker on the canvas
145 this.itemMarker.draw(canvas);
146
147 // restore the position of the marker
148 this.itemMarker.setBounds(this.markerBounds);
149
150 // Add text label next to marker
151 Paint paint_text = new Paint(Paint.ANTI_ALIAS_FLAG);
152
153 paint_text.setColor(Color.BLACK);
154 paint_text.setStyle(Style.FILL);
155 paint_text.setTextSize(28);
156
157 canvas.drawText(overlayItem.getTitle(), itemPosition.x+16, itemPosition.y-23, paint_text);
158 }
159 }
160 }
161
162 @Override
163 protected String getThreadName() {
164 return THREAD_NAME;
165 }
166
167 /**
168 * Handles a tap event.
169 * <p>
170 * The default implementation of this method does nothing and returns false.
171 *
172 * @param index
173 * the position of the item.
174 *
175 * @return true if the event was handled, false otherwise.
176 */
177 //protected boolean onTap(int index) {
178 // return false;
179 //}
180
181 /**
182 * This method should be called after items have been added to the Overlay.
183 */
184 protected final void populate() {
185 super.requestRedraw();
186 }
187
188}
Note: See TracBrowser for help on using the repository browser.