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