From 780466ca82462823295986ef8a51139ce11e89dd Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Mon, 7 Sep 2020 14:43:01 +0300 Subject: [PATCH] Fix track position on map --- .../FollowTrackFragment.java | 60 ++++++++++++++++++- .../MapRouteInfoMenu.java | 28 +++++++++ .../MapRouteInfoMenuFragment.java | 40 +------------ .../osmand/plus/routing/RoutingHelper.java | 39 +++++++++--- 4 files changed, 121 insertions(+), 46 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/FollowTrackFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/FollowTrackFragment.java index a23ecd6dd3..b620e0a9a5 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/FollowTrackFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/FollowTrackFragment.java @@ -27,7 +27,9 @@ import net.osmand.GPXUtilities.GPXFile; import net.osmand.GPXUtilities.TrkSegment; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; +import net.osmand.ValueHolder; import net.osmand.data.QuadRect; +import net.osmand.data.RotatedTileBox; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -55,6 +57,7 @@ import net.osmand.plus.routepreparationmenu.cards.ReverseTrackCard; import net.osmand.plus.routepreparationmenu.cards.SelectTrackCard; import net.osmand.plus.routepreparationmenu.cards.TrackEditCard; import net.osmand.plus.routepreparationmenu.cards.TracksToFollowCard; +import net.osmand.plus.routing.IRouteInformationListener; import net.osmand.plus.routing.RouteProvider; import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RoutingHelper; @@ -66,7 +69,7 @@ import java.io.File; import java.util.List; -public class FollowTrackFragment extends ContextMenuScrollFragment implements CardListener { +public class FollowTrackFragment extends ContextMenuScrollFragment implements CardListener, IRouteInformationListener { public static final String TAG = FollowTrackFragment.class.getName(); @@ -264,12 +267,14 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca @Override public void onResume() { super.onResume(); + app.getRoutingHelper().addListener(this); MapRouteInfoMenu.followTrackVisible = true; } @Override public void onPause() { super.onPause(); + app.getRoutingHelper().removeListener(this); MapRouteInfoMenu.followTrackVisible = false; } @@ -293,6 +298,41 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca updateStatusBarColor(); } + @Override + protected int applyPosY(int currentY, boolean needCloseMenu, boolean needMapAdjust, int previousMenuState, int newMenuState, int dZoom, boolean animated) { + int y = super.applyPosY(currentY, needCloseMenu, needMapAdjust, previousMenuState, newMenuState, dZoom, animated); + if (needMapAdjust) { + adjustMapPosition(y); + } + return y; + } + + private void adjustMapPosition(int y) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity == null) { + return; + } + + RoutingHelper rh = app.getRoutingHelper(); + if (rh.isRoutePlanningMode() && mapActivity.getMapView() != null) { + QuadRect r = mapActivity.getMapRouteInfoMenu().getRouteRect(mapActivity); + + RotatedTileBox tb = mapActivity.getMapView().getCurrentRotatedTileBox().copy(); + int tileBoxWidthPx = 0; + int tileBoxHeightPx = 0; + + if (!isPortrait()) { + tileBoxWidthPx = tb.getPixWidth() - getWidth(); + } else { + int fHeight = getViewHeight() - y - AndroidUtils.getStatusBarHeight(app); + tileBoxHeightPx = tb.getPixHeight() - fHeight; + } + if (r.left != 0 && r.right != 0) { + mapActivity.getMapView().fitRectToMap(r.left, r.right, r.top, r.bottom, tileBoxWidthPx, tileBoxHeightPx, 0); + } + } + } + @Override public void onDestroyView() { super.onDestroyView(); @@ -569,4 +609,22 @@ public class FollowTrackFragment extends ContextMenuScrollFragment implements Ca log.error(e); } } + + @Override + public void newRouteIsCalculated(boolean newRoute, ValueHolder showToast) { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null && newRoute && app.getRoutingHelper().isRoutePlanningMode()) { + adjustMapPosition(getHeight()); + } + } + + @Override + public void routeWasCancelled() { + + } + + @Override + public void routeWasFinished() { + + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java index 138f0a20ee..3aec1a312d 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenu.java @@ -43,6 +43,7 @@ import net.osmand.ValueHolder; import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; +import net.osmand.data.QuadRect; import net.osmand.data.RotatedTileBox; import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.FavouritesDbHelper.FavoritesListener; @@ -95,6 +96,7 @@ import net.osmand.plus.routepreparationmenu.cards.PublicTransportNotFoundWarning import net.osmand.plus.routepreparationmenu.cards.SimpleRouteCard; import net.osmand.plus.routepreparationmenu.cards.TracksCard; import net.osmand.plus.routing.IRouteInformationListener; +import net.osmand.plus.routing.RouteCalculationResult; import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RoutingHelper; import net.osmand.plus.routing.TransportRoutingHelper; @@ -2377,6 +2379,32 @@ public class MapRouteInfoMenu implements IRouteInformationListener, CardListener updateMenu(); } + @NonNull + public QuadRect getRouteRect(@NonNull MapActivity mapActivity) { + OsmandApplication app = mapActivity.getMyApplication(); + RoutingHelper routingHelper = app.getRoutingHelper(); + MapRouteInfoMenu menu = mapActivity.getMapRouteInfoMenu(); + + QuadRect rect = new QuadRect(0, 0, 0, 0); + if (menu.isTransportRouteCalculated()) { + TransportRoutingHelper transportRoutingHelper = app.getTransportRoutingHelper(); + TransportRouteResult result = transportRoutingHelper.getCurrentRouteResult(); + if (result != null) { + QuadRect transportRouteRect = transportRoutingHelper.getTransportRouteRect(result); + if (transportRouteRect != null) { + rect = transportRouteRect; + } + } + } else if (routingHelper.isRouteCalculated()) { + RouteCalculationResult result = routingHelper.getRoute(); + QuadRect routeRect = routingHelper.getRouteRect(result); + if (routeRect != null) { + rect = routeRect; + } + } + return rect; + } + public enum MapRouteMenuType { ROUTE_INFO, ROUTE_DETAILS diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenuFragment.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenuFragment.java index 537901f6f2..bd1d382677 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/MapRouteInfoMenuFragment.java @@ -16,23 +16,16 @@ import androidx.annotation.Nullable; import androidx.core.content.ContextCompat; import net.osmand.AndroidUtils; -import net.osmand.Location; import net.osmand.data.QuadRect; import net.osmand.data.RotatedTileBox; -import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.TargetPointsHelper.TargetPoint; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.base.ContextMenuFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.routing.RoutingHelper; -import net.osmand.plus.routing.TransportRoutingHelper; +import net.osmand.plus.settings.backend.ApplicationMode; import net.osmand.plus.widgets.TextViewExProgress; -import net.osmand.router.TransportRouteResult; -import net.osmand.util.MapUtils; - -import java.util.List; public class MapRouteInfoMenuFragment extends ContextMenuFragment { public static final String TAG = MapRouteInfoMenuFragment.class.getName(); @@ -289,36 +282,7 @@ public class MapRouteInfoMenuFragment extends ContextMenuFragment { RoutingHelper rh = app.getRoutingHelper(); if (rh.isRoutePlanningMode() && mapActivity.getMapView() != null) { - QuadRect r = new QuadRect(0, 0, 0, 0); - if (menu.isTransportRouteCalculated()) { - TransportRoutingHelper transportRoutingHelper = app.getTransportRoutingHelper(); - TransportRouteResult result = transportRoutingHelper.getCurrentRouteResult(); - if (result != null) { - QuadRect transportRouteRect = transportRoutingHelper.getTransportRouteRect(result); - if (transportRouteRect != null) { - r = transportRouteRect; - } - } - } else if (rh.isRouteCalculated()) { - Location lt = rh.getLastProjection(); - if (lt == null) { - lt = app.getTargetPointsHelper().getPointToStartLocation(); - } - if (lt == null) { - lt = app.getLocationProvider().getLastKnownLocation(); - } - if (lt != null) { - MapUtils.insetLatLonRect(r, lt.getLatitude(), lt.getLongitude()); - } - List list = rh.getCurrentCalculatedRoute(); - for (Location l : list) { - MapUtils.insetLatLonRect(r, l.getLatitude(), l.getLongitude()); - } - List targetPoints = app.getTargetPointsHelper().getIntermediatePointsWithTarget(); - for (TargetPoint l : targetPoints) { - MapUtils.insetLatLonRect(r, l.getLatitude(), l.getLongitude()); - } - } + QuadRect r = menu.getRouteRect(mapActivity); RotatedTileBox tb = mapActivity.getMapView().getCurrentRotatedTileBox().copy(); int tileBoxWidthPx = 0; int tileBoxHeightPx = 0; diff --git a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java index 57becfe9a6..4fd5b1dd65 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java @@ -1,9 +1,10 @@ package net.osmand.plus.routing; -import net.osmand.GPXUtilities; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import net.osmand.GPXUtilities.GPXFile; -import net.osmand.GPXUtilities.WptPt; import net.osmand.Location; import net.osmand.LocationsHolder; import net.osmand.PlatformUtil; @@ -12,14 +13,11 @@ import net.osmand.ValueHolder; import net.osmand.binary.RouteDataObject; import net.osmand.data.LatLon; import net.osmand.data.QuadPoint; -import net.osmand.plus.routing.RouteProvider.RoutingEnvironment; -import net.osmand.plus.settings.backend.ApplicationMode; +import net.osmand.data.QuadRect; import net.osmand.plus.NavigationService; -import net.osmand.plus.settings.backend.OsmAndAppCustomization.OsmAndAppCustomizationListener; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.TargetPointsHelper.TargetPoint; @@ -27,9 +25,12 @@ import net.osmand.plus.notifications.OsmandNotification.NotificationType; import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo; import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RouteProvider.RouteService; +import net.osmand.plus.routing.RouteProvider.RoutingEnvironment; +import net.osmand.plus.settings.backend.ApplicationMode; +import net.osmand.plus.settings.backend.OsmAndAppCustomization.OsmAndAppCustomizationListener; +import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.router.RouteCalculationProgress; import net.osmand.router.RouteExporter; -import net.osmand.router.RoutePlannerFrontEnd; import net.osmand.router.RoutePlannerFrontEnd.GpxPoint; import net.osmand.router.RoutePlannerFrontEnd.GpxRouteApproximation; import net.osmand.router.RouteSegmentResult; @@ -1041,7 +1042,30 @@ public class RoutingHelper { return route.getRouteDirections(); } + @Nullable + public QuadRect getRouteRect(@NonNull RouteCalculationResult result) { + QuadRect rect = new QuadRect(0, 0, 0, 0); + Location lt = getLastProjection(); + if (lt == null) { + lt = app.getTargetPointsHelper().getPointToStartLocation(); + } + if (lt == null) { + lt = app.getLocationProvider().getLastKnownLocation(); + } + if (lt != null) { + MapUtils.insetLatLonRect(rect, lt.getLatitude(), lt.getLongitude()); + } + List list = result.getImmutableAllLocations(); + for (Location l : list) { + MapUtils.insetLatLonRect(rect, l.getLatitude(), l.getLongitude()); + } + List targetPoints = app.getTargetPointsHelper().getIntermediatePointsWithTarget(); + for (TargetPoint l : targetPoints) { + MapUtils.insetLatLonRect(rect, l.getLatitude(), l.getLongitude()); + } + return rect.left == 0 && rect.right == 0 ? null : rect; + } private class RouteRecalculationThread extends Thread { @@ -1344,6 +1368,7 @@ public class RoutingHelper { // NEVER returns null + @NonNull public RouteCalculationResult getRoute() { return route; }