1 | package org.mapsforge.android.maps;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 |
|
---|
5 | import android.graphics.Canvas;
|
---|
6 | import android.graphics.Paint;
|
---|
7 | import android.graphics.Path;
|
---|
8 | import android.graphics.Point;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * RouteOverlayResetable is a special Overlay to display a sequence of way
|
---|
12 | * nodes. To draw an arbitrary polygon, add a way node sequence where the first
|
---|
13 | * and the last way node are equal.
|
---|
14 | * <p>
|
---|
15 | * All rendering parameters like color, stroke width, pattern and transparency
|
---|
16 | * can be configured via the two {@link android.graphics.Paint Paint} objects in
|
---|
17 | * the {@link #RouteOverlay(Paint,Paint)} constructor.
|
---|
18 | */
|
---|
19 |
|
---|
20 | public class RouteOverlayResetable extends Overlay {
|
---|
21 |
|
---|
22 | private static final String THREAD_NAME = "RouteOverlayResetable";
|
---|
23 |
|
---|
24 | private Point[] cachedWayPositions;
|
---|
25 | private byte cachedZoomLevel;
|
---|
26 | private Paint fillPaint;
|
---|
27 | private Paint outlinePaint;
|
---|
28 | private final Path path;
|
---|
29 | private GeoPoint[] wayNodes;
|
---|
30 | private ArrayList<String> nodeID;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Constructs a new RouteOverlay.
|
---|
34 | *
|
---|
35 | * @param fillPaint
|
---|
36 | * the paint object which will be used to fill the overlay.
|
---|
37 | * @param outlinePaint
|
---|
38 | * the paint object which will be used to draw the outline of the
|
---|
39 | * overlay.
|
---|
40 | */
|
---|
41 | public RouteOverlayResetable(Paint fillPaint, Paint outlinePaint) {
|
---|
42 | this.path = new Path();
|
---|
43 | this.cachedWayPositions = new Point[0];
|
---|
44 | this.nodeID = new ArrayList<String>();
|
---|
45 | setPaint(fillPaint, outlinePaint);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void reset() {
|
---|
49 | this.path.reset();
|
---|
50 | this.cachedWayPositions = new Point[0];
|
---|
51 | this.wayNodes = new GeoPoint[0];
|
---|
52 | super.requestRedraw();
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Sets the paint objects which will be used to draw the overlay.
|
---|
57 | *
|
---|
58 | * @param fillPaint
|
---|
59 | * the paint object which will be used to fill the overlay.
|
---|
60 | * @param outlinePaint
|
---|
61 | * the paint object which will be used to draw the outline of the
|
---|
62 | * overlay.
|
---|
63 | */
|
---|
64 | public synchronized void setPaint(Paint fillPaint, Paint outlinePaint) {
|
---|
65 | this.fillPaint = fillPaint;
|
---|
66 | this.outlinePaint = outlinePaint;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Sets the way nodes of the route.
|
---|
71 | *
|
---|
72 | * @param wayNodes
|
---|
73 | * the geographical coordinates of the way nodes.
|
---|
74 | */
|
---|
75 | public synchronized void setRouteData(GeoPoint[] wayNodes) {
|
---|
76 | this.wayNodes = wayNodes;
|
---|
77 | if (this.wayNodes != null
|
---|
78 | && this.wayNodes.length != this.cachedWayPositions.length) {
|
---|
79 | this.cachedWayPositions = new Point[this.wayNodes.length];
|
---|
80 | }
|
---|
81 | this.cachedZoomLevel = Byte.MIN_VALUE;
|
---|
82 | super.requestRedraw();
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Sets the way nodes of the route. Sets the nodeId of each way
|
---|
87 | * nodes--03-22-2012
|
---|
88 | *
|
---|
89 | * @param wayNodes
|
---|
90 | * the geographical coordinates of the way nodes.
|
---|
91 | */
|
---|
92 | public synchronized void setRouteData(GeoPoint[] wayNodes,
|
---|
93 | ArrayList<String> nodeId) {
|
---|
94 | this.wayNodes = wayNodes;
|
---|
95 | if (this.wayNodes != null
|
---|
96 | && this.wayNodes.length != this.cachedWayPositions.length) {
|
---|
97 | this.cachedWayPositions = new Point[this.wayNodes.length];
|
---|
98 | }
|
---|
99 | this.nodeID = nodeId;
|
---|
100 | this.cachedZoomLevel = Byte.MIN_VALUE;
|
---|
101 | super.requestRedraw();
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | protected synchronized void drawOverlayBitmap(Canvas canvas,
|
---|
106 | Point drawPosition, Projection projection, byte drawZoomLevel) {
|
---|
107 | if (this.wayNodes == null || this.wayNodes.length < 1) {
|
---|
108 | // no way nodes to draw
|
---|
109 | return;
|
---|
110 | } else if (this.fillPaint == null && this.outlinePaint == null) {
|
---|
111 | // no paint to draw
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | // make sure that the cached way node positions are valid
|
---|
116 | if (drawZoomLevel != this.cachedZoomLevel) {
|
---|
117 | for (int i = 0; i < this.cachedWayPositions.length; ++i) {
|
---|
118 | this.cachedWayPositions[i] = projection.toPoint(
|
---|
119 | this.wayNodes[i], this.cachedWayPositions[i],
|
---|
120 | drawZoomLevel);
|
---|
121 | }
|
---|
122 | this.cachedZoomLevel = drawZoomLevel;
|
---|
123 | }
|
---|
124 |
|
---|
125 | // assemble the path
|
---|
126 | this.path.reset();
|
---|
127 | this.path.moveTo(this.cachedWayPositions[0].x - drawPosition.x,
|
---|
128 | this.cachedWayPositions[0].y - drawPosition.y);
|
---|
129 | for (int i = 1; i < this.cachedWayPositions.length; ++i) {
|
---|
130 | this.path.lineTo(this.cachedWayPositions[i].x - drawPosition.x,
|
---|
131 | this.cachedWayPositions[i].y - drawPosition.y);
|
---|
132 | }
|
---|
133 |
|
---|
134 | // draw the path on the canvas
|
---|
135 | if (this.fillPaint != null) {
|
---|
136 | canvas.drawPath(this.path, this.fillPaint);
|
---|
137 | }
|
---|
138 | if (this.outlinePaint != null) {
|
---|
139 | canvas.drawPath(this.path, this.outlinePaint);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | protected String getThreadName() {
|
---|
145 | return THREAD_NAME;
|
---|
146 | }
|
---|
147 | }
|
---|