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