From 2cf37c97f022f0501c3a01f57b44df3ca5d2d854 Mon Sep 17 00:00:00 2001 From: Sergey Kharchenko Date: Fri, 23 Jun 2023 12:58:38 +0300 Subject: [PATCH] #16632: Implemented map pisitioning to show all selected items --- .../net/osmand/plus/auto/FavoritesScreen.java | 24 +++++++++++++++++++ .../net/osmand/plus/auto/MapMarkersScreen.kt | 16 +++++++++++++ OsmAnd/src/net/osmand/plus/auto/POIScreen.kt | 18 +++++++++++++- .../src/net/osmand/plus/auto/TracksScreen.kt | 15 ++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/auto/FavoritesScreen.java b/OsmAnd/src/net/osmand/plus/auto/FavoritesScreen.java index e84c7d88630..cde5b731862 100644 --- a/OsmAnd/src/net/osmand/plus/auto/FavoritesScreen.java +++ b/OsmAnd/src/net/osmand/plus/auto/FavoritesScreen.java @@ -26,9 +26,12 @@ import net.osmand.data.FavouritePoint; import net.osmand.data.LatLon; +import net.osmand.data.QuadRect; +import net.osmand.data.RotatedTileBox; import net.osmand.plus.R; import net.osmand.plus.myplaces.favorites.FavoriteGroup; import net.osmand.plus.utils.AndroidUtils; +import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.PointImageDrawable; import net.osmand.search.core.ObjectType; import net.osmand.search.core.SearchResult; @@ -67,6 +70,7 @@ public FavoritesScreen( public void onDestroy(@NonNull LifecycleOwner owner) { DefaultLifecycleObserver.super.onDestroy(owner); getApp().getOsmandMap().getMapLayers().getFavouritesLayer().setCustomMapObjects(null); + getApp().getOsmandMap().getMapView().backToLocation(); } }); } @@ -95,7 +99,22 @@ private void setupFavorites(ItemList.Builder listBuilder) { int favoritesPointsSize = favoritesPoints.size(); List limitedFavoritesPoints = favoritesPoints.subList(0, Math.min(favoritesPointsSize, getContentLimit() - 1)); getApp().getOsmandMap().getMapLayers().getFavouritesLayer().setCustomMapObjects(limitedFavoritesPoints); + QuadRect mapRect = new QuadRect(); for (FavouritePoint point : limitedFavoritesPoints) { + double longitude = point.getLongitude(); + double latitude = point.getLatitude(); + if (mapRect.left == 0.0) { + mapRect.left = longitude; + } else { + mapRect.left = Math.min(mapRect.left, longitude); + } + mapRect.right = Math.max(mapRect.right, longitude); + if (mapRect.bottom == 0.0) { + mapRect.bottom = latitude; + } else { + mapRect.bottom = Math.min(mapRect.bottom, latitude); + } + mapRect.top = Math.max(mapRect.top, latitude); String title = point.getDisplayName(getApp()); int color = getApp().getFavoritesHelper().getColorWithCategory(point, ContextCompat.getColor(getApp(), R.color.color_favorite)); CarIcon icon = new CarIcon.Builder(IconCompat.createWithBitmap( @@ -115,6 +134,11 @@ private void setupFavorites(ItemList.Builder listBuilder) { CarLocation.create(point.getLatitude(), point.getLongitude())).build()).build()) .build()); } + if (mapRect.left != 0.0 && mapRect.right != 0.0 && mapRect.top != 0.0 && mapRect.bottom != 0.0) { + OsmandMapTileView mapView = getApp().getOsmandMap().getMapView(); + RotatedTileBox tileBox =mapView.getCurrentRotatedTileBox().copy(); + mapView.fitRectToMap(mapRect.left, mapRect.right, mapRect.top, mapRect.bottom, tileBox.getPixHeight(), tileBox.getPixHeight(), 0); + } } private void onClickFavorite(@NonNull FavouritePoint point) { diff --git a/OsmAnd/src/net/osmand/plus/auto/MapMarkersScreen.kt b/OsmAnd/src/net/osmand/plus/auto/MapMarkersScreen.kt index 92f44bc7e95..6884264f6f3 100644 --- a/OsmAnd/src/net/osmand/plus/auto/MapMarkersScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/MapMarkersScreen.kt @@ -19,11 +19,15 @@ import androidx.core.graphics.drawable.IconCompat import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner import net.osmand.data.LatLon +import net.osmand.data.QuadRect +import net.osmand.data.RotatedTileBox import net.osmand.plus.R import net.osmand.plus.mapmarkers.MapMarker import net.osmand.search.core.ObjectType import net.osmand.search.core.SearchResult import net.osmand.util.MapUtils +import kotlin.math.max +import kotlin.math.min class MapMarkersScreen( carContext: CarContext, @@ -35,6 +39,7 @@ class MapMarkersScreen( override fun onDestroy(owner: LifecycleOwner) { super.onDestroy(owner) app.osmandMap.mapLayers.mapMarkersLayer.setCustomMapObjects(null) + app.osmandMap.mapView.backToLocation() } }) } @@ -46,7 +51,14 @@ class MapMarkersScreen( app.mapMarkersHelper.mapMarkers.subList(0, markersSize.coerceAtMost(contentLimit - 1)) val location = app.settings.lastKnownMapLocation app.osmandMap.mapLayers.mapMarkersLayer.setCustomMapObjects(markers) + val mapRect = QuadRect() for (marker in markers) { + val longitude = marker.longitude + val latitude = marker.latitude + mapRect.left = if(mapRect.left == 0.0) longitude else min(mapRect.left, longitude) + mapRect.right = max(mapRect.right, longitude) + mapRect.bottom = if(mapRect.bottom == 0.0) latitude else min(mapRect.bottom, latitude) + mapRect.top = max(mapRect.top, latitude) val title = marker.getName(app) val markerColor = MapMarker.getColorId(marker.colorIndex) val icon = CarIcon.Builder( @@ -77,6 +89,10 @@ class MapMarkersScreen( } listBuilder.addItem(rowBuilder.build()) } + if (mapRect.left != 0.0 && mapRect.right != 0.0 && mapRect.top != 0.0 && mapRect.bottom != 0.0) { + val tb: RotatedTileBox = app.osmandMap.mapView.currentRotatedTileBox.copy() + app.osmandMap.mapView.fitRectToMap(mapRect.left, mapRect.right, mapRect.top, mapRect.bottom, tb.pixWidth, tb.pixHeight, 0) + } val actionStripBuilder = ActionStrip.Builder() actionStripBuilder.addAction( Action.Builder() diff --git a/OsmAnd/src/net/osmand/plus/auto/POIScreen.kt b/OsmAnd/src/net/osmand/plus/auto/POIScreen.kt index 8719bc0e147..d01b91f60f3 100644 --- a/OsmAnd/src/net/osmand/plus/auto/POIScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/POIScreen.kt @@ -20,6 +20,8 @@ import androidx.lifecycle.LifecycleObserver import androidx.lifecycle.LifecycleOwner import net.osmand.data.Amenity import net.osmand.data.LatLon +import net.osmand.data.QuadRect +import net.osmand.data.RotatedTileBox import net.osmand.plus.R import net.osmand.plus.poi.PoiUIFilter import net.osmand.plus.render.RenderingIcons @@ -30,6 +32,8 @@ import net.osmand.search.core.SearchPhrase import net.osmand.search.core.SearchResult import net.osmand.util.Algorithms import net.osmand.util.MapUtils +import kotlin.math.max +import kotlin.math.min class POIScreen( carContext: CarContext, @@ -45,6 +49,7 @@ class POIScreen( override fun onDestroy(owner: LifecycleOwner) { super.onDestroy(owner) app.osmandMap.mapLayers.poiMapLayer.setCustomMapObjects(null) + app.osmandMap.mapView.backToLocation() } }) } @@ -91,13 +96,20 @@ class POIScreen( private fun setupPOI(listBuilder: ItemList.Builder, searchResults: List?) { val location = app.settings.lastKnownMapLocation val mapPoint = ArrayList() + val mapRect = QuadRect() searchResults?.let { val searchResultsSize = searchResults.size val limitedSearchResults = searchResults.subList(0, searchResultsSize.coerceAtMost(contentLimit - 1)) for (point in limitedSearchResults) { if (point.`object` is Amenity) { - mapPoint.add(point.`object` as Amenity) + val amenity = point.`object` as Amenity + mapPoint.add(amenity) + val amenityLocation = amenity.location + mapRect.left = if(mapRect.left == 0.0) amenityLocation.longitude else min(mapRect.left, amenityLocation.longitude) + mapRect.right = max(mapRect.right, amenityLocation.longitude) + mapRect.bottom = if(mapRect.bottom == 0.0) amenityLocation.latitude else min(mapRect.bottom, amenityLocation.latitude) + mapRect.top = max(mapRect.top, amenityLocation.latitude) } val title = point.localeName var groupIcon = RenderingIcons.getBigIcon(app, group.iconId) @@ -129,6 +141,10 @@ class POIScreen( .build()) } } + if (mapRect.left != 0.0 && mapRect.right != 0.0 && mapRect.top != 0.0 && mapRect.bottom != 0.0) { + val tb: RotatedTileBox = app.osmandMap.mapView.currentRotatedTileBox.copy() + app.osmandMap.mapView.fitRectToMap(mapRect.left, mapRect.right, mapRect.top, mapRect.bottom, tb.pixWidth, tb.pixHeight, 0) + } app.osmandMap.mapLayers.poiMapLayer.setCustomMapObjects(mapPoint) } diff --git a/OsmAnd/src/net/osmand/plus/auto/TracksScreen.kt b/OsmAnd/src/net/osmand/plus/auto/TracksScreen.kt index a93cd6e7f7e..d2055954b20 100644 --- a/OsmAnd/src/net/osmand/plus/auto/TracksScreen.kt +++ b/OsmAnd/src/net/osmand/plus/auto/TracksScreen.kt @@ -9,6 +9,8 @@ import androidx.car.app.navigation.model.PlaceListNavigationTemplate import androidx.core.graphics.drawable.IconCompat import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner +import net.osmand.data.QuadRect +import net.osmand.data.RotatedTileBox import net.osmand.gpx.GPXUtilities import net.osmand.plus.R import net.osmand.plus.configmap.tracks.TrackItem @@ -22,6 +24,8 @@ import net.osmand.search.core.ObjectType import net.osmand.search.core.SearchResult import net.osmand.util.Algorithms import net.osmand.util.MapUtils +import kotlin.math.max +import kotlin.math.min class TracksScreen( carContext: CarContext, @@ -46,6 +50,7 @@ class TracksScreen( super.onDestroy(owner) loadGpxFilesThread?.interrupt() app.osmandMap.mapLayers.gpxLayer.setCustomMapObjects(null) + app.osmandMap.mapView.backToLocation() } }) } @@ -109,10 +114,16 @@ class TracksScreen( val selectedGpxFiles = ArrayList() val tracks = trackTab.trackItems.subList(0, tracksSize.coerceAtMost(contentLimit - 1)) + val mapRect = QuadRect() for (track in tracks) { val gpxFile = loadedGpxFiles[track] gpxFile?.let { selectedGpxFiles.add(it) + val gpxRect: QuadRect = it.gpxFile.rect + mapRect.left = if(mapRect.left == 0.0) gpxRect.left else min(mapRect.left, gpxRect.left) + mapRect.right = max(mapRect.right, gpxRect.right) + mapRect.top = max(mapRect.top, gpxRect.top) + mapRect.bottom = if(mapRect.bottom == 0.0) gpxRect.bottom else min(mapRect.bottom, gpxRect.bottom) } val title = track.name val icon = CarIcon.Builder( @@ -143,6 +154,10 @@ class TracksScreen( .setOnClickListener { onClickTrack(track) } .build()) } + if (mapRect.left != 0.0 && mapRect.right != 0.0 && mapRect.top != 0.0 && mapRect.bottom != 0.0) { + val tb: RotatedTileBox = app.osmandMap.mapView.currentRotatedTileBox.copy() + app.osmandMap.mapView.fitRectToMap(mapRect.left, mapRect.right, mapRect.top, mapRect.bottom, tb.pixWidth, tb.pixHeight, 0) + } app.osmandMap.mapLayers.gpxLayer.setCustomMapObjects(selectedGpxFiles) templateBuilder.setItemList(listBuilder.build()) }