From 60cf701dd604f83c379c5349baa5d43712283e27 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sat, 16 Apr 2016 21:18:14 +0300 Subject: [PATCH] Added relative bearing mode to widget. Fixed map marker widget --- .../src/net/osmand/plus/OsmandSettings.java | 3 + .../net/osmand/plus/views/MapInfoLayer.java | 2 +- .../mapwidgets/MapMarkersWidgetsFactory.java | 9 +- .../mapwidgets/RouteInfoWidgetsFactory.java | 131 +++++++++--------- 4 files changed, 79 insertions(+), 66 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index b42e91cefe..c07fcfb831 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -2453,6 +2453,9 @@ public class OsmandSettings { public final OsmandPreference SHOW_ARRIVAL_TIME_OTHERWISE_EXPECTED_TIME = new BooleanPreference("show_arrival_time", true).makeGlobal(); + public final OsmandPreference SHOW_RELATIVE_BEARING_OTHERWISE_REGULAR_BEARING = + new BooleanPreference("show_relative_bearing", true).makeGlobal(); + public final OsmandPreference AGPS_DATA_LAST_TIME_DOWNLOADED = new LongPreference("agps_data_downloaded", 0).makeGlobal(); diff --git a/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java b/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java index c1f6f31e03..722558111f 100644 --- a/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/MapInfoLayer.java @@ -120,7 +120,7 @@ public class MapInfoLayer extends OsmandMapLayer { TextInfoWidget dist = ric.createDistanceControl(map); registerSideWidget(dist, R.drawable.ic_action_target, R.string.map_widget_distance, "distance", false, 5); TextInfoWidget bearing = ric.createBearingControl(map); - registerSideWidget(bearing, R.drawable.ic_action_target, R.string.map_widget_bearing, "bearing", false, 7); + registerSideWidget(bearing, R.drawable.ic_action_bearing, R.string.map_widget_bearing, "bearing", false, 7); TextInfoWidget time = ric.createTimeControl(map); registerSideWidget(time, R.drawable.ic_action_time, R.string.map_widget_time, "time", false, 10); diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidgetsFactory.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidgetsFactory.java index d2a0c69489..e030e82b3e 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidgetsFactory.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/MapMarkersWidgetsFactory.java @@ -18,6 +18,7 @@ import net.osmand.plus.dashboard.DashLocationFragment; import net.osmand.plus.dashboard.DashboardOnMap; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.MapMarkerDialogHelper; +import net.osmand.plus.views.AnimateDraggingMapThread; import net.osmand.plus.views.DirectionDrawable; import net.osmand.plus.views.OsmandMapLayer.DrawSettings; import net.osmand.plus.views.OsmandMapTileView; @@ -142,7 +143,13 @@ public class MapMarkersWidgetsFactory { private void showMarkerOnMap(int index) { if (helper.getSortedMapMarkers().size() > index) { MapMarker marker = helper.getSortedMapMarkers().get(index); - MapMarkerDialogHelper.showMarkerOnMap(map, marker); + AnimateDraggingMapThread thread = map.getMapView().getAnimatedDraggingThread(); + LatLon pointToNavigate = marker.point; + if (pointToNavigate != null) { + int fZoom = map.getMapView().getZoom() < 15 ? 15 : map.getMapView().getZoom(); + thread.startMoving(pointToNavigate.getLatitude(), pointToNavigate.getLongitude(), fZoom, true); + } + //MapMarkerDialogHelper.showMarkerOnMap(map, marker); } } diff --git a/OsmAnd/src/net/osmand/plus/views/mapwidgets/RouteInfoWidgetsFactory.java b/OsmAnd/src/net/osmand/plus/views/mapwidgets/RouteInfoWidgetsFactory.java index d6a048a03e..f0f0d8a1c3 100644 --- a/OsmAnd/src/net/osmand/plus/views/mapwidgets/RouteInfoWidgetsFactory.java +++ b/OsmAnd/src/net/osmand/plus/views/mapwidgets/RouteInfoWidgetsFactory.java @@ -505,76 +505,79 @@ public class RouteInfoWidgetsFactory { return distanceControl; } - - public abstract static class BearingToPointInfoControl extends TextInfoWidget { - - private final OsmandMapTileView view; - private float[] calculations = new float[2]; - private int cachedDegrees; - - public BearingToPointInfoControl(MapActivity ma, int res, int resNight) { - super(ma); - this.view = ma.getMapView(); - if (res != 0 && resNight != 0) { - setIcons(res, resNight); - } - setText(null, null); - setOnClickListener(new View.OnClickListener() { - - @Override - public void onClick(View v) { - click(view); - } - }); - } - - protected void click(final OsmandMapTileView view) { - AnimateDraggingMapThread thread = view.getAnimatedDraggingThread(); - LatLon pointToNavigate = getPointToNavigate(); - if (pointToNavigate != null) { - int fZoom = view.getZoom() < 15 ? 15 : view.getZoom(); - thread.startMoving(pointToNavigate.getLatitude(), pointToNavigate.getLongitude(), fZoom, true); - } - } - - @Override - public boolean updateInfo(DrawSettings drawSettings) { - int b = getBearing(); - if (distChanged(cachedDegrees, b)) { - cachedDegrees = b; - if (b >= 0) { - setText(String.valueOf(b) + "°", null); - } else { - setText(null, null); - } - return true; - } - return false; - } - - public abstract LatLon getPointToNavigate(); - - public int getBearing() { - int d = -1; - Location myLocation = getOsmandApplication().getLocationProvider().getLastKnownLocation(); - LatLon l = getPointToNavigate(); - if (myLocation != null && l != null) { - Location.distanceBetween(myLocation.getLatitude(), myLocation.getLongitude(), l.getLatitude(), l.getLongitude(), calculations); - d = (int) calculations[1]; - } - return d; - } - } - public TextInfoWidget createBearingControl(final MapActivity map) { - BearingToPointInfoControl bearingControl = new BearingToPointInfoControl(map,R.drawable.widget_target_day, - R.drawable.widget_target_night) { - @Override + final int bearingResId = R.drawable.widget_bearing_day; + final int bearingNightResId = R.drawable.widget_bearing_night; + final int relativeBearingResId = R.drawable.widget_bearing_day; + final int relativeBearingNightResId = R.drawable.widget_bearing_night; + final OsmandApplication ctx = map.getMyApplication(); + final OsmandPreference showRelativeBearing = ctx.getSettings().SHOW_RELATIVE_BEARING_OTHERWISE_REGULAR_BEARING; + + final TextInfoWidget bearingControl = new TextInfoWidget(map) { + private int cachedDegrees; + public LatLon getPointToNavigate() { TargetPoint p = map.getPointToNavigate(); return p == null ? null : p.point; } + + @Override + public boolean updateInfo(DrawSettings drawSettings) { + int b = getBearing(showRelativeBearing.get()); + if (distChanged(cachedDegrees, b)) { + cachedDegrees = b; + if (b != -1000) { + setText(String.valueOf(b) + "°", null); + } else { + setText(null, null); + } + return true; + } + return false; + } + + public int getBearing(boolean relative) { + int d = -1000; + Location myLocation = getOsmandApplication().getLocationProvider().getLastKnownLocation(); + LatLon l = getPointToNavigate(); + if (myLocation != null && l != null) { + Location dest = new Location(""); + dest.setLatitude(l.getLatitude()); + dest.setLongitude(l.getLongitude()); + dest.setBearing(myLocation.bearingTo(dest)); + float bearingToDest = dest.getBearing(); + if (relative) { + if (myLocation.hasBearing()) { + bearingToDest -= myLocation.getBearing(); + if (bearingToDest > 180f) { + bearingToDest -= 360f; + } else if (bearingToDest < -180f) { + bearingToDest += 360f; + } + d = (int) bearingToDest; + } + } else { + d = (int) bearingToDest; + } + } + return d; + } }; + + bearingControl.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + showRelativeBearing.set(!showRelativeBearing.get()); + bearingControl.setIcons(showRelativeBearing.get() ? bearingResId : relativeBearingResId, + showRelativeBearing.get() ? bearingNightResId : relativeBearingNightResId); + map.getMapView().refreshMap(); + } + + }); + bearingControl.setText(null, null); + bearingControl.setIcons(showRelativeBearing.get() ? bearingResId : relativeBearingResId, + showRelativeBearing.get() ? bearingNightResId : relativeBearingNightResId); return bearingControl; }