1 | package org.mapsforge.android.maps;
|
---|
2 |
|
---|
3 | import android.graphics.Canvas;
|
---|
4 | import android.graphics.Color;
|
---|
5 | import android.graphics.Paint;
|
---|
6 | import android.graphics.Paint.Style;
|
---|
7 | import android.graphics.Point;
|
---|
8 | import android.graphics.Rect;
|
---|
9 | import android.graphics.drawable.Drawable;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * ItemizedMarkerOverlay is an abstract base class to display a list of
|
---|
13 | * OverlayMarkerItems.
|
---|
14 | *
|
---|
15 | * @param <Item>
|
---|
16 | * the type of items handled by this Overlay.
|
---|
17 | */
|
---|
18 | public abstract class ItemizedMarkerOverlay<Item extends OverlayMarkerItem>
|
---|
19 | extends Overlay {
|
---|
20 | private static final int ARRAY_LIST_INITIAL_CAPACITY = 8;
|
---|
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
|
---|
25 | * bottom row.
|
---|
26 | *
|
---|
27 | * @param balloon
|
---|
28 | * the drawable whose bounds should be set.
|
---|
29 | * @return the given drawable.
|
---|
30 | */
|
---|
31 | protected static Drawable boundCenter(Drawable balloon) {
|
---|
32 | balloon.setBounds(balloon.getIntrinsicWidth() / -2,
|
---|
33 | balloon.getIntrinsicHeight() / -2,
|
---|
34 | balloon.getIntrinsicWidth() / 2,
|
---|
35 | 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
|
---|
41 | * bounding box.
|
---|
42 | *
|
---|
43 | * @param balloon
|
---|
44 | * the drawable whose bounds should be set.
|
---|
45 | * @return the given drawable.
|
---|
46 | */
|
---|
47 | protected static Drawable boundCenterBottom(Drawable balloon) {
|
---|
48 | balloon.setBounds(balloon.getIntrinsicWidth() / -2,
|
---|
49 | -balloon.getIntrinsicHeight(), balloon.getIntrinsicWidth() / 2,
|
---|
50 | 0);
|
---|
51 | return balloon;
|
---|
52 | }
|
---|
53 |
|
---|
54 | private int bottom;
|
---|
55 | private Drawable defaultMarker;
|
---|
56 | private Drawable itemMarker;
|
---|
57 | private final Point itemPosition;
|
---|
58 | private int left;
|
---|
59 | private Rect markerBounds;
|
---|
60 | private int numberOfItems;
|
---|
61 | private Item overlayItem;
|
---|
62 | private int right;
|
---|
63 | private int top;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Constructs a new ItemizedOverlay.
|
---|
67 | *
|
---|
68 | * @param defaultMarker
|
---|
69 | * the default marker for each item.
|
---|
70 | */
|
---|
71 | public ItemizedMarkerOverlay(Drawable defaultMarker) {
|
---|
72 | this.defaultMarker = defaultMarker;
|
---|
73 | this.itemPosition = new Point();
|
---|
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,
|
---|
94 | Point drawPosition, 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(
|
---|
114 | this.overlayItem.getPoint(),
|
---|
115 | this.overlayItem.cachedMapPosition, drawZoomLevel);
|
---|
116 | this.overlayItem.cachedZoomLevel = drawZoomLevel;
|
---|
117 | }
|
---|
118 |
|
---|
119 | // calculate the relative item position on the display
|
---|
120 | this.itemPosition.x = this.overlayItem.cachedMapPosition.x
|
---|
121 | - drawPosition.x;
|
---|
122 | this.itemPosition.y = this.overlayItem.cachedMapPosition.y
|
---|
123 | - drawPosition.y;
|
---|
124 |
|
---|
125 | // get the correct marker for the item
|
---|
126 | if (this.overlayItem.getMarker() == null) {
|
---|
127 | this.itemMarker = this.defaultMarker;
|
---|
128 | } else {
|
---|
129 | this.itemMarker = this.overlayItem.getMarker();
|
---|
130 | }
|
---|
131 |
|
---|
132 | // get the position of the marker
|
---|
133 | this.markerBounds = this.itemMarker.copyBounds();
|
---|
134 |
|
---|
135 | // calculate the bounding box of the marker
|
---|
136 | this.left = this.itemPosition.x + this.markerBounds.left;
|
---|
137 | this.right = this.itemPosition.x + this.markerBounds.right;
|
---|
138 | this.top = this.itemPosition.y + this.markerBounds.top;
|
---|
139 | this.bottom = this.itemPosition.y + this.markerBounds.bottom;
|
---|
140 |
|
---|
141 | // check if the bounding box of the marker intersects with the
|
---|
142 | // canvas
|
---|
143 | if (this.right >= 0 && this.left <= canvas.getWidth()
|
---|
144 | && this.bottom >= 0 && this.top <= canvas.getHeight()) {
|
---|
145 | // set the position of the marker
|
---|
146 | this.itemMarker.setBounds(this.left, this.top, this.right,
|
---|
147 | this.bottom);
|
---|
148 |
|
---|
149 | // draw the item marker on the canvas
|
---|
150 | this.itemMarker.draw(canvas);
|
---|
151 |
|
---|
152 | // restore the position of the marker
|
---|
153 | this.itemMarker.setBounds(this.markerBounds);
|
---|
154 |
|
---|
155 | // Add text label next to marker
|
---|
156 | Paint paint_text = new Paint(Paint.ANTI_ALIAS_FLAG);
|
---|
157 |
|
---|
158 | paint_text.setColor(Color.BLACK);
|
---|
159 | paint_text.setStyle(Style.FILL);
|
---|
160 | paint_text.setTextSize(20);
|
---|
161 |
|
---|
162 | canvas.drawText(overlayItem.getTitle(), itemPosition.x - 10,
|
---|
163 | itemPosition.y - 28, paint_text);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | protected String getThreadName() {
|
---|
170 | return THREAD_NAME;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Handles a tap event.
|
---|
175 | * <p>
|
---|
176 | * The default implementation of this method does nothing and returns false.
|
---|
177 | *
|
---|
178 | * @param index
|
---|
179 | * the position of the item.
|
---|
180 | *
|
---|
181 | * @return true if the event was handled, false otherwise. try to make this
|
---|
182 | * Ontap work --option 1
|
---|
183 | */
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * This method should be called after items have been added to the Overlay.
|
---|
187 | */
|
---|
188 | protected final void populate() {
|
---|
189 | super.requestRedraw();
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|