From 4b8ea588455645708ed2e5504f20feaf8337f7a5 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sat, 13 Feb 2016 14:32:25 +0300 Subject: [PATCH] Added top map markers widget --- OsmAnd/res/layout/map_hud_top.xml | 177 +++++++++++++++- OsmAnd/res/values/colors.xml | 4 + .../plus/base/MapViewTrackingUtilities.java | 14 ++ .../plus/helpers/MapMarkerDialogHelper.java | 19 +- .../osmand/plus/views/MapMarkersLayer.java | 8 + .../views/mapwidgets/MapMarkersWidget.java | 197 ++++++++++++++++++ 6 files changed, 408 insertions(+), 11 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidget.java diff --git a/OsmAnd/res/layout/map_hud_top.xml b/OsmAnd/res/layout/map_hud_top.xml index cc44e2d13a..1139a1e1d9 100644 --- a/OsmAnd/res/layout/map_hud_top.xml +++ b/OsmAnd/res/layout/map_hud_top.xml @@ -139,10 +139,185 @@ android:id="@+id/waypoint_close" android:layout_width="@dimen/map_address_height" android:layout_height="@dimen/map_address_height" - android:background="@drawable/btn_circle_transparent" + android:background="@drawable/bottom_shadow" android:src="@drawable/ic_action_test_light"/> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + #145256 + #0e3b3e + #588689 + #2196f3 #73b825 #ff9800 diff --git a/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java b/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java index df9a745159..ca4206e4a7 100644 --- a/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java +++ b/OsmAnd/src/net/osmand/plus/base/MapViewTrackingUtilities.java @@ -3,6 +3,7 @@ package net.osmand.plus.base; import android.content.Context; import android.view.WindowManager; +import net.osmand.FloatMath; import net.osmand.Location; import net.osmand.StateChangedListener; import net.osmand.ValueHolder; @@ -47,10 +48,13 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc private boolean isUserZoomed = false; private String locationProvider; private boolean showRouteFinishDialog = false; + private Location myLocation; + private Float heading; public MapViewTrackingUtilities(OsmandApplication app){ this.app = app; settings = app.getSettings(); + myLocation = app.getLocationProvider().getLastKnownLocation(); app.getLocationProvider().addLocationListener(this); app.getLocationProvider().addCompassListener(this); addTargetPointListener(app); @@ -95,12 +99,21 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc } } + public Location getMyLocation() { + return myLocation; + } + + public Float getHeading() { + return heading; + } + public String getLocationProvider() { return locationProvider; } @Override public void updateCompassValue(float val) { + heading = val; if (mapView != null) { if (settings.ROTATE_MAP.get() == OsmandSettings.ROTATE_MAP_COMPASS && !routePlanningMode) { if (Math.abs(MapUtils.degreesDiff(mapView.getRotate(), -val)) > 1) { @@ -128,6 +141,7 @@ public class MapViewTrackingUtilities implements OsmAndLocationListener, IMapLoc @Override public void updateLocation(Location location) { + myLocation = location; showViewAngle = false; if (location != null) { locationProvider = location.getProvider(); diff --git a/OsmAnd/src/net/osmand/plus/helpers/MapMarkerDialogHelper.java b/OsmAnd/src/net/osmand/plus/helpers/MapMarkerDialogHelper.java index 0a45c67dbc..b6f05f3755 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/MapMarkerDialogHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/MapMarkerDialogHelper.java @@ -84,7 +84,7 @@ public class MapMarkerDialogHelper { if (obj instanceof MapMarker) { MapMarker marker = (MapMarker) obj; if (!marker.history) { - showOnMap(marker); + showMarkerOnMap(mapActivity, marker); } else { showHistoryOnMap(marker); } @@ -316,12 +316,7 @@ public class MapMarkerDialogHelper { } int dist = (int) mes[0]; - - //if (dist > 0) { textDist.setText(OsmAndFormatter.getFormattedDistance(dist, app)); - //} else { - // textDist.setText(""); - //} waypointDeviation.setVisibility(View.GONE); @@ -396,8 +391,8 @@ public class MapMarkerDialogHelper { textDist.setText(OsmAndFormatter.getFormattedDistance(dist, app)); } - public void showOnMap(MapMarker marker) { - app.getSettings().setMapLocationToShow(marker.getLatitude(), marker.getLongitude(), + public static void showMarkerOnMap(MapActivity mapActivity, MapMarker marker) { + mapActivity.getMyApplication().getSettings().setMapLocationToShow(marker.getLatitude(), marker.getLongitude(), 15, marker.getPointDescription(mapActivity), true, marker); MapActivity.launchMapActivityMoveToTop(mapActivity); } @@ -492,6 +487,10 @@ public class MapMarkerDialogHelper { } public static Drawable getMapMarkerIcon(OsmandApplication app, int colorIndex) { + return app.getIconsCache().getIcon(R.drawable.ic_action_flag_dark, getMapMarkerColorId(colorIndex)); + } + + public static int getMapMarkerColorId(int colorIndex) { int colorId; switch (colorIndex) { case 0: @@ -505,7 +504,7 @@ public class MapMarkerDialogHelper { break; case 3: colorId = R.color.marker_red; - break; + break; case 4: colorId = R.color.marker_yellow; break; @@ -518,7 +517,7 @@ public class MapMarkerDialogHelper { default: colorId = R.color.marker_blue; } - return app.getIconsCache().getIcon(R.drawable.ic_action_flag_dark, colorId); + return colorId; } public void updateLocation(ListView listView, boolean compassChanged) { diff --git a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java index 54d477b485..8b562b45ed 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapMarkersLayer.java @@ -16,6 +16,7 @@ import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.views.mapwidgets.MapMarkersWidget; import java.util.ArrayList; import java.util.List; @@ -26,6 +27,8 @@ public class MapMarkersLayer extends OsmandMapLayer implements ContextMenuLayer. private final MapActivity map; private OsmandMapTileView view; + private MapMarkersWidget widget; + private Paint bitmapPaint; private Bitmap markerBitmapBlue; private Bitmap markerBitmapGreen; @@ -70,6 +73,8 @@ public class MapMarkersLayer extends OsmandMapLayer implements ContextMenuLayer. bitmapPaintDestYellow = createPaintDest(R.color.marker_yellow); bitmapPaintDestTeal = createPaintDest(R.color.marker_teal); bitmapPaintDestPurple = createPaintDest(R.color.marker_purple); + + widget = new MapMarkersWidget(map); } private Paint createPaintDest(int colorId) { @@ -132,6 +137,9 @@ public class MapMarkersLayer extends OsmandMapLayer implements ContextMenuLayer. @Override public void onDraw(Canvas canvas, RotatedTileBox tb, DrawSettings nightMode) { + + widget.updateInfo(tb.getZoom()); + if (tb.getZoom() < 3) { return; } diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidget.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidget.java new file mode 100644 index 0000000000..ea4b1614df --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidget.java @@ -0,0 +1,197 @@ +package net.osmand.plus.views.mapwidgets; + +import android.view.View; +import android.widget.ImageButton; +import android.widget.ImageView; +import android.widget.TextView; + +import net.osmand.Location; +import net.osmand.data.PointDescription; +import net.osmand.plus.IconsCache; +import net.osmand.plus.MapMarkersHelper; +import net.osmand.plus.MapMarkersHelper.MapMarker; +import net.osmand.plus.OsmAndFormatter; +import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.dashboard.DashLocationFragment; +import net.osmand.plus.helpers.MapMarkerDialogHelper; +import net.osmand.plus.views.DirectionDrawable; +import net.osmand.util.Algorithms; + +import java.util.List; + +public class MapMarkersWidget { + + public static final int MIN_DIST_OK_VISIBLE = 50; + + private final MapActivity map; + private MapMarkersHelper helper; + private int screenOrientation; + + private View topBar; + private View topBar2nd; + private View rowView; + private View rowView2nd; + private ImageView arrowImg; + private ImageView arrowImg2nd; + private TextView distText; + private TextView distText2nd; + private TextView addressText; + private TextView addressText2nd; + private ImageButton okButton; + private ImageButton okButton2nd; + private ImageButton moreButton; + private ImageButton moreButton2nd; + + public MapMarkersWidget(MapActivity map) { + this.map = map; + helper = map.getMyApplication().getMapMarkersHelper(); + screenOrientation = DashLocationFragment.getScreenOrientation(map); + + topBar = map.findViewById(R.id.map_markers_top_bar); + topBar2nd = map.findViewById(R.id.map_markers_top_bar_2nd); + rowView = map.findViewById(R.id.map_marker_row); + rowView2nd = map.findViewById(R.id.map_marker_row_2nd); + arrowImg = (ImageView) map.findViewById(R.id.map_marker_arrow); + arrowImg2nd = (ImageView) map.findViewById(R.id.map_marker_arrow_2nd); + distText = (TextView) map.findViewById(R.id.map_marker_dist); + distText2nd = (TextView) map.findViewById(R.id.map_marker_dist_2nd); + addressText = (TextView) map.findViewById(R.id.map_marker_address); + addressText2nd = (TextView) map.findViewById(R.id.map_marker_address_2nd); + okButton = (ImageButton) map.findViewById(R.id.marker_btn_ok); + okButton2nd = (ImageButton) map.findViewById(R.id.marker_btn_ok_2dn); + moreButton = (ImageButton) map.findViewById(R.id.marker_btn_more); + moreButton2nd = (ImageButton) map.findViewById(R.id.marker_btn_more_2nd); + + rowView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + showMarkerOnMap(0); + } + }); + rowView2nd.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + showMarkerOnMap(1); + } + }); + + IconsCache iconsCache = map.getMyApplication().getIconsCache(); + moreButton.setImageDrawable(iconsCache.getIcon(R.drawable.ic_overflow_menu_white, R.color.marker_top_2nd_line_color)); + moreButton2nd.setImageDrawable(iconsCache.getIcon(R.drawable.ic_overflow_menu_white, R.color.marker_top_2nd_line_color)); + okButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + removeMarker(0); + } + }); + okButton2nd.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + removeMarker(1); + } + }); + + updateVisibility(false); + } + + private void removeMarker(int index) { + if (helper.getActiveMapMarkers().size() > index) { + MapMarker marker = helper.getActiveMapMarkers().get(index); + helper.removeMapMarker(marker.index); + helper.addMapMarkerHistory(marker); + } + } + + private void showMarkerOnMap(int index) { + if (helper.getActiveMapMarkers().size() > index) { + MapMarker marker = helper.getActiveMapMarkers().get(index); + MapMarkerDialogHelper.showMarkerOnMap(map, marker); + } + } + + public boolean updateVisibility(boolean visible) { + return updateVisibility(topBar, visible); + } + + public boolean updateVisibility(View v, boolean visible) { + if (visible != (v.getVisibility() == View.VISIBLE)) { + if (visible) { + v.setVisibility(View.VISIBLE); + } else { + v.setVisibility(View.GONE); + } + v.invalidate(); + return true; + } + return false; + } + + public void updateInfo(int zoom) { + List markers = helper.getActiveMapMarkers(); + if (zoom < 3 || markers.size() == 0 || map.getMyApplication().getRoutingHelper().isFollowingMode() + || map.getMapLayers().getMapControlsLayer().getMapRouteInfoMenu().isVisible()) { + updateVisibility(false); + return; + } + + Location loc = map.getMapViewTrackingUtilities().getMyLocation(); + Float heading = map.getMapViewTrackingUtilities().getHeading(); + + MapMarker marker = markers.get(0); + updateUI(loc, heading, marker, arrowImg, distText, okButton, addressText, true); + + if (markers.size() > 1) { + marker = markers.get(1); + updateUI(loc, heading, marker, arrowImg2nd, distText2nd, okButton2nd, addressText2nd, false); + updateVisibility(topBar2nd, true); + } else { + updateVisibility(topBar2nd, false); + } + + updateVisibility(true); + } + + private void updateUI(Location loc, Float heading, MapMarker marker, ImageView arrowImg, + TextView distText, ImageButton okButton, TextView addressText, boolean firstLine) { + float[] mes = new float[2]; + if (loc != null && marker.point != null) { + Location.distanceBetween(marker.getLatitude(), marker.getLongitude(), loc.getLatitude(), loc.getLongitude(), mes); + } + + boolean newImage = false; + DirectionDrawable dd; + if (!(arrowImg.getDrawable() instanceof DirectionDrawable)) { + newImage = true; + dd = new DirectionDrawable(map, arrowImg.getWidth(), arrowImg.getHeight()); + } else { + dd = (DirectionDrawable) arrowImg.getDrawable(); + } + dd.setImage(R.drawable.map_arrow_to_destination, MapMarkerDialogHelper.getMapMarkerColorId(marker.colorIndex)); + if (loc == null || heading == null || marker.point == null) { + dd.setAngle(0); + } else { + dd.setAngle(mes[1] - heading + 90 + screenOrientation); + } + if (newImage) { + arrowImg.setImageDrawable(dd); + } + arrowImg.invalidate(); + + int dist = (int) mes[0]; + distText.setText(OsmAndFormatter.getFormattedDistance(dist, map.getMyApplication())); + updateVisibility(okButton, dist < MIN_DIST_OK_VISIBLE); + + String descr; + PointDescription pd = marker.getPointDescription(map); + if (Algorithms.isEmpty(pd.getName())) { + descr = pd.getTypeName(); + } else { + descr = pd.getName(); + } + if (!firstLine) { + descr = " — " + descr; + } + addressText.setText(descr); + } +}