aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/maps/PositionDrawer.java
blob: c7d1734b5538893928b16d50110cd0ce3a165717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package cgeo.geocaching.maps;

import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.R;
import cgeo.geocaching.location.Geopoint;
import cgeo.geocaching.maps.interfaces.GeoPointImpl;
import cgeo.geocaching.maps.interfaces.MapItemFactory;
import cgeo.geocaching.maps.interfaces.MapProjectionImpl;
import cgeo.geocaching.settings.Settings;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Point;
import android.location.Location;

import java.util.ArrayList;

public class PositionDrawer {

    private Location coordinates = null;
    private GeoPointImpl location = null;
    private float heading = 0f;
    private Paint accuracyCircle = null;
    private Paint historyLine = null;
    private Paint historyLineShadow = null;
    private final Point center = new Point();
    private final Point left = new Point();
    private Bitmap arrow = null;
    private int widthArrowHalf = 0;
    private int heightArrowHalf = 0;
    private PaintFlagsDrawFilter setfil = null;
    private PaintFlagsDrawFilter remfil = null;
    private final PositionHistory positionHistory = new PositionHistory();
    private final MapItemFactory mapItemFactory;

    public PositionDrawer() {
        this.mapItemFactory = Settings.getMapProvider().getMapItemFactory();
    }

    void drawPosition(final Canvas canvas, final MapProjectionImpl projection) {
        if (coordinates == null || location == null) {
            return;
        }

        if (accuracyCircle == null) {
            accuracyCircle = new Paint();
            accuracyCircle.setAntiAlias(true);
            accuracyCircle.setStrokeWidth(1.0f);
        }

        if (historyLine == null) {
            historyLine = new Paint();
            historyLine.setAntiAlias(true);
            historyLine.setStrokeWidth(3.0f);
            historyLine.setColor(0xFFFFFFFF);
        }

        if (historyLineShadow == null) {
            historyLineShadow = new Paint();
            historyLineShadow.setAntiAlias(true);
            historyLineShadow.setStrokeWidth(7.0f);
            historyLineShadow.setColor(0x66000000);
        }

        if (setfil == null) {
            setfil = new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG);
        }
        if (remfil == null) {
            remfil = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG, 0);
        }

        canvas.setDrawFilter(setfil);

        final double latitude = coordinates.getLatitude();
        final double longitude = coordinates.getLongitude();
        final float accuracy = coordinates.getAccuracy();

        final float[] result = new float[1];

        Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result);
        final float longitudeLineDistance = result[0];

        final Geopoint leftCoords = new Geopoint(latitude, longitude - accuracy / longitudeLineDistance);
        final GeoPointImpl leftGeo = mapItemFactory.getGeoPointBase(leftCoords);
        projection.toPixels(leftGeo, left);
        projection.toPixels(location, center);
        final int radius = center.x - left.x;

        accuracyCircle.setColor(0x66000000);
        accuracyCircle.setStyle(Style.STROKE);
        canvas.drawCircle(center.x, center.y, radius, accuracyCircle);

        accuracyCircle.setColor(0x08000000);
        accuracyCircle.setStyle(Style.FILL);
        canvas.drawCircle(center.x, center.y, radius, accuracyCircle);

        positionHistory.rememberTrailPosition(coordinates);

        if (Settings.isMapTrail()) {
            // always add current position to drawn history to have a closed connection
            final ArrayList<Location> paintHistory = new ArrayList<>(positionHistory.getHistory());
            paintHistory.add(coordinates);

            final int size = paintHistory.size();
            if (size > 1) {
                int alphaCnt = size - 201;
                if (alphaCnt < 1) {
                    alphaCnt = 1;
                }

                final Point pointNow = new Point();
                final Point pointPrevious = new Point();
                final Location prev = paintHistory.get(0);
                projection.toPixels(mapItemFactory.getGeoPointBase(new Geopoint(prev)), pointPrevious);

                for (int cnt = 1; cnt < size; cnt++) {
                    final Location now = paintHistory.get(cnt);
                    projection.toPixels(mapItemFactory.getGeoPointBase(new Geopoint(now)), pointNow);

                    final int alpha;
                    if ((alphaCnt - cnt) > 0) {
                        alpha = 255 / (alphaCnt - cnt);
                    }
                    else {
                        alpha = 255;
                    }

                    historyLineShadow.setAlpha(alpha);
                    historyLine.setAlpha(alpha);

                    canvas.drawLine(pointPrevious.x, pointPrevious.y, pointNow.x, pointNow.y, historyLineShadow);
                    canvas.drawLine(pointPrevious.x, pointPrevious.y, pointNow.x, pointNow.y, historyLine);

                    pointPrevious.set(pointNow.x, pointNow.y);
                }
            }
        }

        if (arrow == null) {
            arrow = BitmapFactory.decodeResource(CgeoApplication.getInstance().getResources(), R.drawable.my_location_chevron);
            widthArrowHalf = arrow.getWidth() / 2;
            heightArrowHalf = arrow.getHeight() / 2;
        }

        final int marginLeft = center.x - widthArrowHalf;
        final int marginTop = center.y - heightArrowHalf;

        final Matrix matrix = new Matrix();
        matrix.setRotate(heading, widthArrowHalf, heightArrowHalf);
        matrix.postTranslate(marginLeft, marginTop);

        canvas.drawBitmap(arrow, matrix, null);

        canvas.setDrawFilter(remfil);
    }

    public ArrayList<Location> getHistory() {
        return positionHistory.getHistory();
    }

    public void setHistory(final ArrayList<Location> history) {
        positionHistory.setHistory(history);
    }

    public void setHeading(final float bearingNow) {
        heading = bearingNow;
    }

    public float getHeading() {
        return heading;
    }

    public void setCoordinates(final Location coordinatesIn) {
        coordinates = coordinatesIn;
        location = mapItemFactory.getGeoPointBase(new Geopoint(coordinates));
    }

    public Location getCoordinates() {
        return coordinates;
    }

}