From b4f303b2bb2825d1c2fdc7e5dd6b67747d9cdaca Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Thu, 22 Feb 2018 15:26:35 +0200 Subject: [PATCH] Fix sorting of map markers by distance --- .../src/net/osmand/plus/MapMarkersHelper.java | 37 +++++++++++-------- .../mapmarkers/MapMarkersDialogFragment.java | 11 +++++- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java index 4c4dfbb49c..f0d32692e3 100644 --- a/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java +++ b/OsmAnd/src/net/osmand/plus/MapMarkersHelper.java @@ -15,6 +15,7 @@ import net.osmand.plus.FavouritesDbHelper.FavoriteGroup; import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile; +import net.osmand.plus.OsmandSettings.MapMarkersOrderByMode; import net.osmand.plus.mapmarkers.MapMarkersDbHelper; import net.osmand.plus.mapmarkers.MarkersPlanRouteContext; import net.osmand.util.Algorithms; @@ -282,7 +283,7 @@ public class MapMarkersHelper { reorderActiveMarkersIfNeeded(); List markersHistory = markersDbHelper.getMarkersHistory(); - sortMarkers(markersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(markersHistory, true, MapMarkersOrderByMode.DATE_ADDED_DESC); addToMapMarkersHistoryList(markersHistory); if (!ctx.isApplicationInitializing()) { @@ -384,8 +385,12 @@ public class MapMarkersHelper { } } - public void sortMarkers(List markers, final boolean visited, final OsmandSettings.MapMarkersOrderByMode orderByMode) { - final LatLon location = ctx.getSettings().getLastKnownMapLocation(); + public void sortMarkers(List markers, final boolean visited, final MapMarkersOrderByMode orderByMode) { + sortMarkers(markers, visited, orderByMode, null); + } + + public void sortMarkers(List markers, final boolean visited, + final MapMarkersOrderByMode orderByMode, @Nullable final LatLon location) { Collections.sort(markers, new Comparator() { @Override public int compare(MapMarker mapMarker1, MapMarker mapMarker2) { @@ -399,7 +404,7 @@ public class MapMarkersHelper { } else { return orderByMode.isDateAddedDescending() ? 1 : -1; } - } else if (orderByMode.isDistanceDescending() || orderByMode.isDistanceAscending()) { + } else if (location != null && (orderByMode.isDistanceDescending() || orderByMode.isDistanceAscending())) { int d1 = (int) MapUtils.getDistance(location, mapMarker1.getLatitude(), mapMarker1.getLongitude()); int d2 = (int) MapUtils.getDistance(location, mapMarker2.getLatitude(), mapMarker2.getLongitude()); if (d1 > d2) { @@ -418,8 +423,8 @@ public class MapMarkersHelper { }); } - public void orderMarkers(OsmandSettings.MapMarkersOrderByMode orderByMode) { - sortMarkers(getMapMarkers(), false, orderByMode); + public void orderMarkers(MapMarkersOrderByMode orderByMode, LatLon location) { + sortMarkers(getMapMarkers(), false, orderByMode, location); reorderActiveMarkersIfNeeded(); } @@ -682,7 +687,7 @@ public class MapMarkersHelper { marker.nextKey = MapMarkersDbHelper.HISTORY_NEXT_VALUE; addToMapMarkersHistoryList(marker); reorderActiveMarkersIfNeeded(); - sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(mapMarkersHistory, true, MapMarkersOrderByMode.DATE_ADDED_DESC); refresh(); } } @@ -702,7 +707,7 @@ public class MapMarkersHelper { markersDbHelper.addMarker(marker); if (marker.history) { addToMapMarkersHistoryList(marker); - sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(mapMarkersHistory, true, MapMarkersOrderByMode.DATE_ADDED_DESC); } else { addToMapMarkersList(marker); reorderActiveMarkersIfNeeded(); @@ -719,7 +724,7 @@ public class MapMarkersHelper { marker.history = false; addToMapMarkersList(position, marker); reorderActiveMarkersIfNeeded(); - sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(mapMarkersHistory, true, MapMarkersOrderByMode.DATE_ADDED_DESC); refresh(); } } @@ -733,7 +738,7 @@ public class MapMarkersHelper { addToMapMarkersList(marker); } reorderActiveMarkersIfNeeded(); - sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(mapMarkersHistory, true, MapMarkersOrderByMode.DATE_ADDED_DESC); updateGroups(); refresh(); } @@ -824,7 +829,7 @@ public class MapMarkersHelper { removeFromMapMarkersList(markersToRemove); addToMapMarkersList(0, markers); reorderActiveMarkersIfNeeded(); - ctx.getSettings().MAP_MARKERS_ORDER_BY_MODE.set(OsmandSettings.MapMarkersOrderByMode.CUSTOM); + ctx.getSettings().MAP_MARKERS_ORDER_BY_MODE.set(MapMarkersOrderByMode.CUSTOM); } public List getActiveMarkersLatLon() { @@ -857,7 +862,7 @@ public class MapMarkersHelper { cancelAddressRequests(); Collections.reverse(mapMarkers); reorderActiveMarkersIfNeeded(); - ctx.getSettings().MAP_MARKERS_ORDER_BY_MODE.set(OsmandSettings.MapMarkersOrderByMode.CUSTOM); + ctx.getSettings().MAP_MARKERS_ORDER_BY_MODE.set(MapMarkersOrderByMode.CUSTOM); } public void moveAllActiveMarkersToHistory() { @@ -871,7 +876,7 @@ public class MapMarkersHelper { } addToMapMarkersHistoryList(mapMarkers); mapMarkers = new LinkedList<>(); - sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(mapMarkersHistory, true, MapMarkersOrderByMode.DATE_ADDED_DESC); updateGroups(); refresh(); } @@ -935,7 +940,7 @@ public class MapMarkersHelper { } } reorderActiveMarkersIfNeeded(); - sortMarkers(mapMarkersHistory, true, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(mapMarkersHistory, true, MapMarkersOrderByMode.DATE_ADDED_DESC); refresh(); } @@ -1214,7 +1219,7 @@ public class MapMarkersHelper { mapMarkersGroup.getMarkers().add(marker); updateGroup(mapMarkersGroup); if (mapMarkersGroup.getName() == null) { - sortMarkers(mapMarkersGroup.getMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(mapMarkersGroup.getMarkers(), false, MapMarkersOrderByMode.DATE_ADDED_DESC); } } else { mapMarkersGroup = createMapMarkerGroup(marker); @@ -1328,7 +1333,7 @@ public class MapMarkersHelper { for (int i = 0; i < mapMarkersGroups.size(); i++) { MapMarkersGroup group = mapMarkersGroups.get(i); if (group.getName() == null) { - sortMarkers(group.getMarkers(), false, OsmandSettings.MapMarkersOrderByMode.DATE_ADDED_DESC); + sortMarkers(group.getMarkers(), false, MapMarkersOrderByMode.DATE_ADDED_DESC); removeFromGroupsList(group); noGroup = group; } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java index a0f612cb7d..7969e80bfd 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersDialogFragment.java @@ -21,6 +21,8 @@ import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; +import net.osmand.Location; +import net.osmand.data.LatLon; import net.osmand.plus.LockableViewPager; import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.OsmandApplication; @@ -475,7 +477,14 @@ public class MapMarkersDialogFragment extends android.support.v4.app.DialogFragm private void setOrderByMode(MapMarkersOrderByMode orderByMode) { if (orderByMode != MapMarkersOrderByMode.CUSTOM) { - getMyApplication().getMapMarkersHelper().orderMarkers(orderByMode); + OsmandApplication app = getMyApplication(); + MapActivity mapActivity = getMapActivity(); + + Location location = app.getLocationProvider().getLastKnownLocation(); + boolean useCenter = !(mapActivity.getMapViewTrackingUtilities().isMapLinkedToLocation() && location != null); + LatLon loc = useCenter ? mapActivity.getMapLocation() : new LatLon(location.getLatitude(), location.getLongitude()); + + app.getMapMarkersHelper().orderMarkers(orderByMode, loc); activeFragment.updateAdapter(); } }