From 729101faf2042af281b6049aa16597dd1c46cb46 Mon Sep 17 00:00:00 2001 From: PavelRatushny Date: Wed, 20 Dec 2017 18:52:30 +0200 Subject: [PATCH] Draw coloured transport route on map --- .../net/osmand/plus/TransportStopRoute.java | 81 +++++++++++++++++++ .../plus/mapcontextmenu/MapContextMenu.java | 2 +- .../MapContextMenuFragment.java | 4 +- .../plus/mapcontextmenu/MenuBuilder.java | 4 +- .../plus/mapcontextmenu/MenuController.java | 2 +- .../TransportStopRouteAdapter.java | 2 +- .../controllers/AmenityMenuController.java | 6 +- .../controllers/TransportRouteController.java | 4 +- .../controllers/TransportStopController.java | 78 +----------------- .../plus/views/TransportStopsLayer.java | 31 ++++--- 10 files changed, 111 insertions(+), 103 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/TransportStopRoute.java diff --git a/OsmAnd/src/net/osmand/plus/TransportStopRoute.java b/OsmAnd/src/net/osmand/plus/TransportStopRoute.java new file mode 100644 index 0000000000..e0f7d3fb87 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/TransportStopRoute.java @@ -0,0 +1,81 @@ +package net.osmand.plus; + +import net.osmand.data.RotatedTileBox; +import net.osmand.data.TransportRoute; +import net.osmand.data.TransportStop; +import net.osmand.plus.mapcontextmenu.controllers.TransportStopController; + +import java.util.List; + +public class TransportStopRoute { + public TransportStop refStop; + public TransportStopController.TransportStopType type; + public String desc; + public TransportRoute route; + public TransportStop stop; + public int distance; + public boolean showWholeRoute; + + public String getDescription(OsmandApplication ctx, boolean useDistance) { + if (useDistance && distance > 0) { + String nm = OsmAndFormatter.getFormattedDistance(distance, ctx); + if (refStop != null && !refStop.getName().equals(stop.getName())) { + nm = refStop.getName() + ", " + nm; + } + return desc + " (" + nm + ")"; + } + return desc; + } + + public int calculateZoom(int startPosition, RotatedTileBox currentRotatedTileBox) { + RotatedTileBox cp = currentRotatedTileBox.copy(); + boolean notContains = true; + while (cp.getZoom() > 12 && notContains) { + notContains = false; + List sts = route.getForwardStops(); + for (int i = startPosition; i < sts.size(); i++) { + TransportStop st = sts.get(startPosition); + if (!cp.containsLatLon(st.getLocation())) { + notContains = true; + break; + } + } + cp.setZoom(cp.getZoom() - 1); + } + return cp.getZoom(); + } + + public int getColor(boolean nightMode) { + int color; + switch (type) { + case BUS: + color = R.color.route_bus_color; + break; + case SHARE_TAXI: + color = R.color.route_share_taxi_color; + break; + case TROLLEYBUS: + color = R.color.route_trolleybus_color; + break; + case TRAM: + color = R.color.route_tram_color; + break; + case TRAIN: + color = nightMode ? R.color.route_train_color_dark : R.color.route_train_color_light; + break; + case LIGHT_RAIL: + color = R.color.route_lightrail_color; + break; + case FUNICULAR: + color = R.color.route_funicular_color; + break; + case FERRY: + color = nightMode ? R.color.route_ferry_color_dark : R.color.route_ferry_color_light; + break; + default: + color = R.color.nav_track; + break; + } + return color; + } +} diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java index f6f7788041..3e1815179c 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenu.java @@ -37,7 +37,7 @@ import net.osmand.plus.mapcontextmenu.MenuController.MenuType; import net.osmand.plus.mapcontextmenu.MenuController.TitleButtonController; import net.osmand.plus.mapcontextmenu.MenuController.TitleProgressController; import net.osmand.plus.mapcontextmenu.controllers.MapDataMenuController; -import net.osmand.plus.mapcontextmenu.controllers.TransportStopController.TransportStopRoute; +import net.osmand.plus.TransportStopRoute; import net.osmand.plus.mapcontextmenu.editors.FavoritePointEditor; import net.osmand.plus.mapcontextmenu.editors.PointEditor; import net.osmand.plus.mapcontextmenu.editors.RtePtEditor; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 567aa99acd..36775029b4 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -46,7 +46,7 @@ import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.mapcontextmenu.MenuController.MenuState; import net.osmand.plus.mapcontextmenu.MenuController.TitleButtonController; import net.osmand.plus.mapcontextmenu.MenuController.TitleProgressController; -import net.osmand.plus.mapcontextmenu.controllers.TransportStopController.TransportStopRoute; +import net.osmand.plus.TransportStopRoute; import net.osmand.plus.mapcontextmenu.other.MapRouteInfoMenu; import net.osmand.plus.views.AnimateDraggingMapThread; import net.osmand.plus.views.OsmandMapTileView; @@ -399,7 +399,7 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo route.getDescription(getMapActivity().getMyApplication(), false)); menu.show(menu.getLatLon(), pd, route); TransportStopsLayer stopsLayer = getMapActivity().getMapLayers().getTransportStopsLayer(); - stopsLayer.setRoute(route.route); + stopsLayer.setRoute(route); int cz = route.calculateZoom(0, getMapActivity().getMapView().getCurrentRotatedTileBox()); getMapActivity().changeZoom(cz - getMapActivity().getMapView().getZoom()); } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java index 7a34fc3c43..8847d4eac6 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuBuilder.java @@ -52,7 +52,7 @@ import net.osmand.plus.mapcontextmenu.builders.cards.CardsRowBuilder; import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard; import net.osmand.plus.mapcontextmenu.builders.cards.ImageCard.GetImageCardsTask; import net.osmand.plus.mapcontextmenu.builders.cards.NoImagesCard; -import net.osmand.plus.mapcontextmenu.controllers.TransportStopController.TransportStopRoute; +import net.osmand.plus.TransportStopRoute; import net.osmand.plus.myplaces.FavoritesActivity; import net.osmand.plus.render.RenderingIcons; import net.osmand.plus.views.TransportStopsLayer; @@ -790,7 +790,7 @@ public class MenuBuilder { r.getDescription(getMapActivity().getMyApplication(), false)); mm.show(latLon, pd, r); TransportStopsLayer stopsLayer = getMapActivity().getMapLayers().getTransportStopsLayer(); - stopsLayer.setRoute(r.route); + stopsLayer.setRoute(r); int cz = r.calculateZoom(0, getMapActivity().getMapView().getCurrentRotatedTileBox()); getMapActivity().changeZoom(cz - getMapActivity().getMapView().getZoom()); } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java index 42b4612988..598a668fce 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MenuController.java @@ -51,7 +51,7 @@ import net.osmand.plus.mapcontextmenu.controllers.RenderedObjectMenuController; import net.osmand.plus.mapcontextmenu.controllers.TargetPointMenuController; import net.osmand.plus.mapcontextmenu.controllers.TransportRouteController; import net.osmand.plus.mapcontextmenu.controllers.TransportStopController; -import net.osmand.plus.mapcontextmenu.controllers.TransportStopController.TransportStopRoute; +import net.osmand.plus.TransportStopRoute; import net.osmand.plus.mapcontextmenu.controllers.WptPtMenuController; import net.osmand.plus.mapcontextmenu.other.ShareMenu; import net.osmand.plus.mapillary.MapillaryImage; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/TransportStopRouteAdapter.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/TransportStopRouteAdapter.java index f5b66638b6..b0543ede2d 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/TransportStopRouteAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/TransportStopRouteAdapter.java @@ -12,7 +12,7 @@ import android.widget.ArrayAdapter; import android.widget.TextView; import net.osmand.plus.R; -import net.osmand.plus.mapcontextmenu.controllers.TransportStopController.TransportStopRoute; +import net.osmand.plus.TransportStopRoute; import java.util.List; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java index 862f0090ca..9f64c1dd8c 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java @@ -1,7 +1,5 @@ package net.osmand.plus.mapcontextmenu.controllers; -import android.view.View; - import net.osmand.data.Amenity; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; @@ -14,16 +12,14 @@ import net.osmand.osm.PoiType; import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuController; import net.osmand.plus.mapcontextmenu.OpeningHoursInfo; import net.osmand.plus.mapcontextmenu.builders.AmenityMenuBuilder; -import net.osmand.plus.mapcontextmenu.controllers.TransportStopController.TransportStopRoute; +import net.osmand.plus.TransportStopRoute; import net.osmand.plus.render.RenderingIcons; import net.osmand.plus.resources.TransportIndexRepository; import net.osmand.plus.views.POIMapLayer; -import net.osmand.plus.views.TransportStopsLayer; import net.osmand.util.Algorithms; import net.osmand.util.MapUtils; import net.osmand.util.OpeningHoursParser; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/TransportRouteController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/TransportRouteController.java index fe4e748fcf..a72f2e3544 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/TransportRouteController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/TransportRouteController.java @@ -12,7 +12,7 @@ import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.mapcontextmenu.MenuBuilder; import net.osmand.plus.mapcontextmenu.MenuController; -import net.osmand.plus.mapcontextmenu.controllers.TransportStopController.TransportStopRoute; +import net.osmand.plus.TransportStopRoute; import net.osmand.plus.views.TransportStopsLayer; import java.util.List; @@ -202,7 +202,7 @@ public class TransportRouteController extends MenuController { TransportStopsLayer stopsLayer = getMapActivity().getMapLayers().getTransportStopsLayer(); int cz = transportRoute.calculateZoom(0, getMapActivity().getMapView().getCurrentRotatedTileBox()); getMapActivity().changeZoom(cz - getMapActivity().getMapView().getZoom()); - stopsLayer.setRoute(transportRoute.route); + stopsLayer.setRoute(transportRoute); } private void resetRoute() { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/TransportStopController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/TransportStopController.java index 17ede9a1bf..0972367bc4 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/TransportStopController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/TransportStopController.java @@ -6,12 +6,10 @@ import android.view.View.OnClickListener; import net.osmand.data.LatLon; import net.osmand.data.PointDescription; import net.osmand.data.QuadRect; -import net.osmand.data.RotatedTileBox; import net.osmand.data.TransportRoute; import net.osmand.data.TransportStop; -import net.osmand.plus.OsmAndFormatter; -import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; +import net.osmand.plus.TransportStopRoute; import net.osmand.plus.activities.MapActivity; import net.osmand.plus.mapcontextmenu.MapContextMenu; import net.osmand.plus.mapcontextmenu.MenuBuilder; @@ -137,7 +135,7 @@ public class TransportStopController extends MenuController { r.getDescription(getMapActivity().getMyApplication(), false)); mm.show(latLon, pd, r); TransportStopsLayer stopsLayer = getMapActivity().getMapLayers().getTransportStopsLayer(); - stopsLayer.setRoute(r.route); + stopsLayer.setRoute(r); int cz = r.calculateZoom(0, getMapActivity().getMapView().getCurrentRotatedTileBox()); getMapActivity().changeZoom(cz - getMapActivity().getMapView().getZoom()); } @@ -227,76 +225,4 @@ public class TransportStopController extends MenuController { return false; } - public static class TransportStopRoute { - public TransportStop refStop; - public TransportStopType type; - public String desc; - public TransportRoute route; - public TransportStop stop; - public int distance; - public boolean showWholeRoute; - - public String getDescription(OsmandApplication ctx, boolean useDistance) { - if (useDistance && distance > 0) { - String nm = OsmAndFormatter.getFormattedDistance(distance, ctx); - if (refStop != null && !refStop.getName().equals(stop.getName())) { - nm = refStop.getName() + ", " + nm; - } - return desc + " (" + nm + ")"; - } - return desc; - } - - public int calculateZoom(int startPosition, RotatedTileBox currentRotatedTileBox) { - RotatedTileBox cp = currentRotatedTileBox.copy(); - boolean notContains = true; - while (cp.getZoom() > 12 && notContains) { - notContains = false; - List sts = route.getForwardStops(); - for(int i = startPosition; i < sts.size(); i++) { - TransportStop st = sts.get(startPosition); - if (!cp.containsLatLon(st.getLocation())) { - notContains = true; - break; - } - } - cp.setZoom(cp.getZoom() - 1); - } - return cp.getZoom(); - } - - public int getColor(boolean nightMode) { - int color; - switch (type) { - case BUS: - color = R.color.route_bus_color; - break; - case SHARE_TAXI: - color = R.color.route_share_taxi_color; - break; - case TROLLEYBUS: - color = R.color.route_trolleybus_color; - break; - case TRAM: - color = R.color.route_tram_color; - break; - case TRAIN: - color = nightMode ? R.color.route_train_color_dark : R.color.route_train_color_light; - break; - case LIGHT_RAIL: - color = R.color.route_lightrail_color; - break; - case FUNICULAR: - color = R.color.route_funicular_color; - break; - case FERRY: - color = nightMode ? R.color.route_ferry_color_dark : R.color.route_ferry_color_light; - break; - default: - color = R.color.nav_track; - break; - } - return color; - } - } } diff --git a/OsmAnd/src/net/osmand/plus/views/TransportStopsLayer.java b/OsmAnd/src/net/osmand/plus/views/TransportStopsLayer.java index be9959b371..79fbbb89c7 100644 --- a/OsmAnd/src/net/osmand/plus/views/TransportStopsLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/TransportStopsLayer.java @@ -7,6 +7,7 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PointF; +import android.support.v4.content.ContextCompat; import android.util.DisplayMetrics; import android.view.WindowManager; @@ -16,11 +17,11 @@ import net.osmand.data.PointDescription; import net.osmand.data.QuadRect; import net.osmand.data.QuadTree; import net.osmand.data.RotatedTileBox; -import net.osmand.data.TransportRoute; import net.osmand.data.TransportStop; import net.osmand.osm.edit.Node; import net.osmand.osm.edit.Way; import net.osmand.plus.R; +import net.osmand.plus.TransportStopRoute; import net.osmand.plus.activities.MapActivity; import java.util.ArrayList; @@ -38,13 +39,14 @@ public class TransportStopsLayer extends OsmandMapLayer implements ContextMenuLa private final MapActivity mapActivity; private OsmandMapTileView view; + private int cachedColor; private Paint paintIcon; private Bitmap stopBus; private Bitmap stopSmall; private RenderingLineAttributes attrs; private MapLayerData> data; - private TransportRoute route = null; + private TransportStopRoute stopRoute = null; private boolean showTransportStops; private Path path; @@ -67,7 +69,6 @@ public class TransportStopsLayer extends OsmandMapLayer implements ContextMenuLa stopSmall = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_transport_stop_small); attrs = new RenderingLineAttributes("transport_route"); attrs.defaultWidth = (int) (12 * view.getDensity()); - attrs.defaultColor = view.getResources().getColor(R.color.transport_route_line); data = new OsmandMapLayer.MapLayerData>() { { ZOOM_THRESHOLD = 0; @@ -143,12 +144,12 @@ public class TransportStopsLayer extends OsmandMapLayer implements ContextMenuLa } } - public TransportRoute getRoute() { - return route; + public TransportStopRoute getRoute() { + return stopRoute; } - public void setRoute(TransportRoute route) { - this.route = route; + public void setRoute(TransportStopRoute route) { + this.stopRoute = route; } public boolean isShowTransportStops() { @@ -175,18 +176,22 @@ public class TransportStopsLayer extends OsmandMapLayer implements ContextMenuLa } return (int) (r * tb.getDensity()); } - @Override public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tb, DrawSettings settings) { List objects = null; if (tb.getZoom() >= startZoomRoute) { - if (route != null) { - objects = route.getForwardStops(); + if (stopRoute != null) { + objects = stopRoute.route.getForwardStops(); + int color = stopRoute.getColor(settings.isNightMode()); + if (cachedColor != color) { + cachedColor = color; + attrs.paint.setColor(ContextCompat.getColor(mapActivity, cachedColor)); + } attrs.updatePaints(view, settings, tb); try { path.reset(); - List ws = route.getForwardWays(); + List ws = stopRoute.route.getForwardWays(); if (ws != null) { for (Way w : ws) { TIntArrayList tx = new TIntArrayList(); @@ -286,8 +291,8 @@ public class TransportStopsLayer extends OsmandMapLayer implements ContextMenuLa @Override public void collectObjectsFromPoint(PointF point, RotatedTileBox tileBox, List res, boolean unknownLocation) { - if(tileBox.getZoom() >= startZoomRoute && route != null) { - getFromPoint(tileBox, point, res, route.getForwardStops()); + if(tileBox.getZoom() >= startZoomRoute && stopRoute != null) { + getFromPoint(tileBox, point, res, stopRoute.route.getForwardStops()); } else if (tileBox.getZoom() >= startZoom && data.getResults() != null) { getFromPoint(tileBox, point, res, data.getResults()); }