From 9902060d7b366e5d08c9918662d46edb09155a53 Mon Sep 17 00:00:00 2001 From: Marco Bresciani Date: Mon, 12 Aug 2019 10:00:02 +0200 Subject: [PATCH] Add BoundingBox calculation The BoundingBox is computed using all existing Waypoints of a Track and returning the box through a collection of GeoPoints. The box might be used to autofit the map of a saved Track. See issue #60. --- .../java/org/y20k/trackbook/core/Track.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/y20k/trackbook/core/Track.java b/app/src/main/java/org/y20k/trackbook/core/Track.java index d5641df..62be542 100755 --- a/app/src/main/java/org/y20k/trackbook/core/Track.java +++ b/app/src/main/java/org/y20k/trackbook/core/Track.java @@ -20,6 +20,10 @@ import android.location.Location; import android.os.Parcel; import android.os.Parcelable; +import androidx.annotation.Nullable; + +import org.osmdroid.util.BoundingBox; +import org.osmdroid.util.GeoPoint; import org.y20k.trackbook.helpers.LocationHelper; import org.y20k.trackbook.helpers.TrackbookKeys; @@ -28,8 +32,6 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.List; -import androidx.annotation.Nullable; - /** * Track class @@ -53,6 +55,32 @@ public class Track implements TrackbookKeys, Parcelable { private double mPositiveElevation; private double mNegativeElevation; + private BoundingBox boundingBox; + + /** + * Create a {@code BoundingBox} for the collection of + * {@code WayPoint}s, so that it would be possible to fit the map in + * such box and see the whole {@code Track} in the map without + * manual zooming. + * + * It computes the {@code BoundingBox} only once since it's possibly + * useless to compute it everytime. + * + * @return {@code BoundingBox} containing all {@code Waypoint}s + */ + public BoundingBox getBoundingBox() { + if (null == boundingBox) { + final ArrayList geoPoints = new ArrayList<>(mWayPoints.size()); + + for (final WayPoint aWayPoint : mWayPoints) { + final GeoPoint aGeoPoint = new GeoPoint(aWayPoint.getLocation()); + geoPoints.add(aGeoPoint); + } + + boundingBox = BoundingBox.fromGeoPoints(geoPoints); + } + return boundingBox; + } /* Generic Constructor */ public Track(int trackFormatVersion, List wayPoints, float trackLength, long duration, float stepCount, Date recordingStart, Date recordingStop, double maxAltitude, double minAltitude, double positiveElevation, double negativeElevation) {