From f03bde72226bd6d6bcdf3f5d9ba7831d35e89ef6 Mon Sep 17 00:00:00 2001 From: max-klaus Date: Sun, 9 Feb 2020 11:36:54 +0300 Subject: [PATCH] Added avoid route search radius --- .../osmand/plus/OsmAndLocationProvider.java | 2 +- .../plus/helpers/AvoidSpecificRoads.java | 21 ++++++++++++------- .../osmand/plus/routing/RoutingHelper.java | 4 ++-- .../osmand/plus/views/ContextMenuLayer.java | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index 379c98f1bf..743b79f472 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -151,7 +151,7 @@ public class OsmAndLocationProvider implements SensorEventListener { startLocation.setTime(ms); } RouteSegmentSearchResult searchResult = - RoutingHelper.searchRouteSegment(currentLocation.getLatitude(), currentLocation.getLongitude(), roads); + RoutingHelper.searchRouteSegment(currentLocation.getLatitude(), currentLocation.getLongitude(), -1, roads); if (searchResult != null) { currentRoad = searchResult.getRoadIndex(); currentSegment = searchResult.getSegmentIndex(); diff --git a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java index fdf17e63a6..d8cc1f4def 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java +++ b/OsmAnd/src/net/osmand/plus/helpers/AvoidSpecificRoads.java @@ -24,6 +24,7 @@ import net.osmand.binary.RouteDataObject; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.data.QuadPoint; +import net.osmand.data.RotatedTileBox; import net.osmand.plus.ApplicationMode; import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; @@ -44,6 +45,8 @@ import java.util.Map; public class AvoidSpecificRoads { + private static final float MAX_AVOID_ROUTE_SEARCH_RADIUS_DP = 32f; + private OsmandApplication app; private Map impassableRoads = new LinkedHashMap<>(); @@ -210,7 +213,7 @@ public class AvoidSpecificRoads { }); } - public void addImpassableRoad(@Nullable final MapActivity activity, + public void addImpassableRoad(@Nullable final MapActivity mapActivity, @NonNull final LatLon loc, final boolean showDialog, final boolean skipWritingSettings) { @@ -220,15 +223,17 @@ public class AvoidSpecificRoads { ApplicationMode appMode = app.getRoutingHelper().getAppMode(); List roads = app.getRoutingHelper().getRoute().getOriginalRoute(); - if (roads != null) { + if (mapActivity != null && roads != null) { + RotatedTileBox tb = mapActivity.getMapView().getCurrentRotatedTileBox().copy(); + float maxDistPx = MAX_AVOID_ROUTE_SEARCH_RADIUS_DP * tb.getDensity(); RouteSegmentSearchResult searchResult = - RoutingHelper.searchRouteSegment(loc.getLatitude(), loc.getLongitude(), roads); + RoutingHelper.searchRouteSegment(loc.getLatitude(), loc.getLongitude(), maxDistPx / tb.getPixDensity(), roads); if (searchResult != null) { QuadPoint point = searchResult.getPoint(); LatLon newLoc = new LatLon(MapUtils.get31LatitudeY((int) point.y), MapUtils.get31LongitudeX((int) point.x)); ll.setLatitude(newLoc.getLatitude()); ll.setLongitude(newLoc.getLongitude()); - addImpassableRoadInternal(roads.get(searchResult.getRoadIndex()).getObject(), ll, showDialog, activity, newLoc); + addImpassableRoadInternal(roads.get(searchResult.getRoadIndex()).getObject(), ll, showDialog, mapActivity, newLoc); if (!skipWritingSettings) { app.getSettings().addImpassableRoad(newLoc.getLatitude(), newLoc.getLongitude()); } @@ -240,11 +245,11 @@ public class AvoidSpecificRoads { @Override public boolean publish(RouteDataObject object) { if (object == null) { - if (activity != null) { - Toast.makeText(activity, R.string.error_avoid_specific_road, Toast.LENGTH_LONG).show(); + if (mapActivity != null) { + Toast.makeText(mapActivity, R.string.error_avoid_specific_road, Toast.LENGTH_LONG).show(); } } else { - addImpassableRoadInternal(object, ll, showDialog, activity, loc); + addImpassableRoadInternal(object, ll, showDialog, mapActivity, loc); } return true; } @@ -319,7 +324,7 @@ public class AvoidSpecificRoads { showDialog(activity); } MapContextMenu menu = activity.getContextMenu(); - if (menu.isActive() && menu.getLatLon().equals(loc)) { + if (menu.isActive()) { menu.close(); } activity.refreshMap(); diff --git a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java index 16673c7d25..d4154c2f55 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java @@ -1251,13 +1251,13 @@ public class RoutingHelper { } } - public static RouteSegmentSearchResult searchRouteSegment(double latitude, double longitude, List roads) { + public static RouteSegmentSearchResult searchRouteSegment(double latitude, double longitude, double maxDist, List roads) { int roadIndex = -1; int segmentIndex = -1; QuadPoint point = null; int px = MapUtils.get31TileNumberX(longitude); int py = MapUtils.get31TileNumberY(latitude); - double dist = 1000; + double dist = maxDist < 0 ? 1000 : maxDist; for (int i = 0; i < roads.size(); i++) { RouteSegmentResult road = roads.get(i); int startPointIndex = road.getStartPointIndex() < road.getEndPointIndex() ? road.getStartPointIndex() : road.getEndPointIndex(); diff --git a/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java b/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java index 49a0824c89..7b56728c37 100644 --- a/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/ContextMenuLayer.java @@ -947,9 +947,9 @@ public class ContextMenuLayer extends OsmandMapLayer { if (selectOnMap != null) { LatLon latlon = tileBox.getLatLonFromPixel(point.x, point.y); + menu.init(latlon, null, null); CallbackWithObject cb = selectOnMap; cb.processResult(latlon); - menu.init(latlon, null, null); selectOnMap = null; return true; }