diff options
| -rw-r--r-- | main/src/cgeo/geocaching/maps/PositionHistory.java | 63 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/maps/PositionOverlay.java | 82 |
2 files changed, 89 insertions, 56 deletions
diff --git a/main/src/cgeo/geocaching/maps/PositionHistory.java b/main/src/cgeo/geocaching/maps/PositionHistory.java new file mode 100644 index 0000000..9b090fc --- /dev/null +++ b/main/src/cgeo/geocaching/maps/PositionHistory.java @@ -0,0 +1,63 @@ +package cgeo.geocaching.maps; + +import android.location.Location; + +import java.util.ArrayList; + +/** + * Map trail history + */ +public class PositionHistory { + + /** + * minimum distance between two recorded points of the trail + */ + private static final double MINIMUM_DISTANCE_METERS = 10.0; + + /** + * maximum number of positions to remember + */ + private static final int MAX_POSITIONS = 700; + + private ArrayList<Location> history = new ArrayList<Location>(); + + /** + * Adds the current position to the trail history to be able to show the trail on the map. + */ + void rememberTrailPosition(Location coordinates) { + if (coordinates.getAccuracy() >= 50f) { + return; + } + if (coordinates.getLatitude() == 0.0 && coordinates.getLatitude() == 0.0) { + return; + } + if (history.isEmpty()) { + history.add(coordinates); + return; + } + + Location historyRecent = history.get(history.size() - 1); + if (historyRecent.distanceTo(coordinates) <= MINIMUM_DISTANCE_METERS) { + return; + } + + history.add(coordinates); + + // avoid running out of memory + final int itemsToRemove = getHistory().size() - MAX_POSITIONS; + if (itemsToRemove > 0) { + for (int i = 0; i < itemsToRemove; i++) { + getHistory().remove(0); + } + } + } + + public ArrayList<Location> getHistory() { + return history; + } + + public void setHistory(ArrayList<Location> history) { + this.history = history; + } + +}
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/maps/PositionOverlay.java b/main/src/cgeo/geocaching/maps/PositionOverlay.java index c3a0834..39f4987 100644 --- a/main/src/cgeo/geocaching/maps/PositionOverlay.java +++ b/main/src/cgeo/geocaching/maps/PositionOverlay.java @@ -1,7 +1,6 @@ package cgeo.geocaching.maps; import cgeo.geocaching.R; -import cgeo.geocaching.settings.Settings; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.maps.interfaces.GeneralOverlay; import cgeo.geocaching.maps.interfaces.GeoPointImpl; @@ -9,6 +8,7 @@ import cgeo.geocaching.maps.interfaces.MapItemFactory; import cgeo.geocaching.maps.interfaces.MapProjectionImpl; import cgeo.geocaching.maps.interfaces.MapViewImpl; import cgeo.geocaching.maps.interfaces.OverlayImpl; +import cgeo.geocaching.settings.Settings; import android.app.Activity; import android.graphics.Bitmap; @@ -37,10 +37,7 @@ public class PositionOverlay implements GeneralOverlay { private int heightArrowHalf = 0; private PaintFlagsDrawFilter setfil = null; private PaintFlagsDrawFilter remfil = null; - private Location historyRecent = null; - private ArrayList<Location> history = new ArrayList<Location>(); - private Point historyPointN = new Point(); - private Point historyPointP = new Point(); + private PositionHistory positionHistory = new PositionHistory(); private Activity activity; private MapItemFactory mapItemFactory = null; private OverlayImpl ovlImpl = null; @@ -139,67 +136,40 @@ public class PositionOverlay implements GeneralOverlay { accuracyCircle.setStyle(Style.FILL); canvas.drawCircle(center.x, center.y, radius, accuracyCircle); - if (coordinates.getAccuracy() < 50f && ((historyRecent != null && historyRecent.distanceTo(coordinates) > 5.0) || historyRecent == null)) { - if (historyRecent != null) { - history.add(historyRecent); - } - historyRecent = coordinates; - - int toRemove = history.size() - 700; - - if (toRemove > 0) { - for (int cnt = 0; cnt < toRemove; cnt++) { - history.remove(cnt); - } - } - } + positionHistory.rememberTrailPosition(coordinates); if (Settings.isMapTrail()) { - int size = history.size(); + int size = positionHistory.getHistory().size(); if (size > 1) { int alphaCnt = size - 201; if (alphaCnt < 1) { alphaCnt = 1; } + Point pointNow = new Point(); + Point pointPrevious = new Point(); + Location prev = positionHistory.getHistory().get(0); + projection.toPixels(mapItemFactory.getGeoPointBase(new Geopoint(prev)), pointPrevious); + for (int cnt = 1; cnt < size; cnt++) { - Location prev = history.get(cnt - 1); - Location now = history.get(cnt); - - if (prev != null && now != null) { - projection.toPixels(mapItemFactory.getGeoPointBase(new Geopoint(prev)), historyPointP); - projection.toPixels(mapItemFactory.getGeoPointBase(new Geopoint(now)), historyPointN); - - int alpha; - if ((alphaCnt - cnt) > 0) { - alpha = 255 / (alphaCnt - cnt); - } - else { - alpha = 255; - } - - historyLineShadow.setAlpha(alpha); - historyLine.setAlpha(alpha); - - canvas.drawLine(historyPointP.x, historyPointP.y, historyPointN.x, historyPointN.y, historyLineShadow); - canvas.drawLine(historyPointP.x, historyPointP.y, historyPointN.x, historyPointN.y, historyLine); - } - } - } + Location now = positionHistory.getHistory().get(cnt); + projection.toPixels(mapItemFactory.getGeoPointBase(new Geopoint(now)), pointNow); - if (size > 0) { - Location prev = history.get(size - 1); - Location now = coordinates; + int alpha; + if ((alphaCnt - cnt) > 0) { + alpha = 255 / (alphaCnt - cnt); + } + else { + alpha = 255; + } - if (prev != null && now != null) { - projection.toPixels(mapItemFactory.getGeoPointBase(new Geopoint(prev)), historyPointP); - projection.toPixels(mapItemFactory.getGeoPointBase(new Geopoint(now)), historyPointN); + historyLineShadow.setAlpha(alpha); + historyLine.setAlpha(alpha); - historyLineShadow.setAlpha(255); - historyLine.setAlpha(255); + canvas.drawLine(pointPrevious.x, pointPrevious.y, pointNow.x, pointNow.y, historyLineShadow); + canvas.drawLine(pointPrevious.x, pointPrevious.y, pointNow.x, pointNow.y, historyLine); - canvas.drawLine(historyPointP.x, historyPointP.y, historyPointN.x, historyPointN.y, historyLineShadow); - canvas.drawLine(historyPointP.x, historyPointP.y, historyPointN.x, historyPointN.y, historyLine); + pointPrevious.set(pointNow.x, pointNow.y); } } } @@ -230,10 +200,10 @@ public class PositionOverlay implements GeneralOverlay { } public ArrayList<Location> getHistory() { - return history; + return positionHistory.getHistory(); } - public void setHistory(ArrayList<Location> inHistory) { - history = inHistory; + public void setHistory(ArrayList<Location> history) { + positionHistory.setHistory(history); } } |
