[26523] | 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 | * CirclesOverlay is a special Overlay to display a series of circles on top of
|
---|
| 12 | * the map. The radius of a circle is specified in meters and will be
|
---|
| 13 | * automatically converted to pixels at each redraw.
|
---|
| 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 #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
|
---|
| 40 | * overlay.
|
---|
| 41 | */
|
---|
| 42 | public CirclesOverlay(Paint fillPaint, Paint outlinePaint) {
|
---|
| 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 | path.reset();
|
---|
| 57 | super.requestRedraw();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Sets the parameters of the circle.
|
---|
| 62 | *
|
---|
| 63 | * @param center
|
---|
| 64 | * the geographical coordinates of the centre point.
|
---|
| 65 | * @param radius
|
---|
| 66 | * the radius of the circle in meters.
|
---|
| 67 | */
|
---|
| 68 | public synchronized void addCircleData(GeoPoint center, float radius) {
|
---|
| 69 | this.center.add(center);
|
---|
| 70 | this.radius.add(radius);
|
---|
| 71 | this.cachedCenterPosition.add(new Point());
|
---|
| 72 |
|
---|
| 73 | this.cachedZoomLevel = Byte.MIN_VALUE;
|
---|
| 74 | super.requestRedraw();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Sets the paint objects which will be used to draw the overlay.
|
---|
| 79 | *
|
---|
| 80 | * @param fillPaint
|
---|
| 81 | * the paint object which will be used to fill the overlay.
|
---|
| 82 | * @param outlinePaint
|
---|
| 83 | * the paint object which will be used to draw the outline of the
|
---|
| 84 | * overlay.
|
---|
| 85 | */
|
---|
| 86 | public synchronized void setPaint(Paint fillPaint, Paint outlinePaint) {
|
---|
| 87 | this.fillPaint = fillPaint;
|
---|
| 88 | this.outlinePaint = outlinePaint;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | @Override
|
---|
| 92 | protected synchronized void drawOverlayBitmap(Canvas canvas,
|
---|
| 93 | Point drawPosition, Projection projection, byte drawZoomLevel) {
|
---|
| 94 | if (this.center.size() == 0 || this.radius.size() == 0) {
|
---|
| 95 | // no valid parameters to draw the circle
|
---|
| 96 | return;
|
---|
| 97 | } else if (this.fillPaint == null && this.outlinePaint == null) {
|
---|
| 98 | // no paint to draw
|
---|
| 99 | return;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | // make sure that the cached centre position is valid
|
---|
| 103 | if (drawZoomLevel != this.cachedZoomLevel) {
|
---|
| 104 | for (int i = 0; i < center.size(); i++) {
|
---|
| 105 | Point cachedPoint = projection.toPoint(this.center.get(i),
|
---|
| 106 | this.cachedCenterPosition.get(i), drawZoomLevel);
|
---|
| 107 | this.cachedCenterPosition.set(i, cachedPoint);
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | this.cachedZoomLevel = drawZoomLevel;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | // assemble the path
|
---|
| 114 | this.path.reset();
|
---|
| 115 | for (int i = 0; i < cachedCenterPosition.size(); i++) {
|
---|
| 116 | float radius_meters = projection.metersToPixels(this.radius.get(i),
|
---|
| 117 | drawZoomLevel);
|
---|
| 118 |
|
---|
| 119 | this.path.addCircle(this.cachedCenterPosition.get(i).x
|
---|
| 120 | - drawPosition.x, this.cachedCenterPosition.get(i).y
|
---|
| 121 | - drawPosition.y, 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 | } |
---|