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