commit 3f6e41440295b9e662a88accdc5c1905d037a175 Author: Fabio Mazza Date: Mon Oct 5 14:25:43 2026 +0200 Add button to center the map on open stop or vehicle diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt index 915b4f3..1b760ec 100644 --- a/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt @@ -76,6 +76,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.maplibre.android.MapLibre import org.maplibre.android.camera.CameraPosition +import org.maplibre.android.camera.CameraUpdateFactory import org.maplibre.android.geometry.LatLng import org.maplibre.android.location.LocationComponent import org.maplibre.android.location.LocationComponentActivationOptions @@ -100,6 +101,7 @@ import org.maplibre.android.style.sources.GeoJsonSource import org.maplibre.geojson.Feature import org.maplibre.geojson.FeatureCollection import org.maplibre.geojson.Point +import kotlin.math.absoluteValue import kotlin.time.Duration.Companion.milliseconds abstract class GeneralMapLibreFragment: ScreenBaseFragment(), OnMapReadyCallback { @@ -181,6 +183,7 @@ abstract class GeneralMapLibreFragment: ScreenBaseFragment(), OnMapReadyCallback protected lateinit var vehicleIcon: ImageView protected lateinit var warningTripIcon: ImageView private lateinit var loadingTripIcon: ImageView + protected lateinit var centerBottomImage: ImageView protected var lastLocation : Location? = null @@ -282,7 +285,8 @@ abstract class GeneralMapLibreFragment: ScreenBaseFragment(), OnMapReadyCallback linesBottomTextView.text = getString(R.string.lines_fill, "") bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN - + centerBottomImage = view.findViewById(R.id.centerBottomLayoutImage) + centerBottomImage.setOnClickListener(){ centerOnOpenItemInBottomSheet() } //set onclick listener for warning trip icon warningTripIcon.setOnClickListener { showToastMessage(R.string.no_trip_info_warning, true) @@ -1218,6 +1222,44 @@ abstract class GeneralMapLibreFragment: ScreenBaseFragment(), OnMapReadyCallback } } + protected fun centerOnOpenItemInBottomSheet(){ + var latlng: LatLng? = null + var found = 0 + if(shownStopInBottomSheet!=null){ + val stop = shownStopInBottomSheet!! + latlng = LatLng(stop.latitude!!, stop.longitude!!) + found = 1 + }else if(vehShowing!=null){ + val update = updatesByVehDict.getOrDefault(vehShowing!!, null) + update?.let{ + latlng = LatLng(it.posUpdate.latitude, it.posUpdate.longitude) + found = 2 + } + + } + if (latlng!=null) { + map?.apply{ + //complicated part: make toast if the position is the same + //val newBbox = projection.visibleRegion.latLngBounds + val ll = cameraPosition.target + if(ll!=null && (ll.latitude - latlng.latitude).absoluteValue < 1e-4 && (ll.longitude-latlng.longitude).absoluteValue < 1e-4){ + Toast.makeText(requireContext(), + if(found==1) R.string.already_center_bottom_sheet_stop else R.string.already_center_bottom_sheet_vehicle, + Toast.LENGTH_SHORT + ).show() + } else { + animateCamera( + CameraUpdateFactory.newLatLng(latlng), + 700 + ) + } + } + }else { + Log.d(DEBUG_TAG, "Asked to center on open thing in bottom sheet but no position found") + } + + } + diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt index 74e0d9f..ba55d31 100644 --- a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt @@ -236,7 +236,7 @@ class LinesDetailFragment() : GeneralMapLibreFragment() { mapView = rootView.findViewById(R.id.lineMap) mapView!!.getMapAsync(this) - + //TODO: Unify in GeneralMapLibreFragment // Setup close button rootView.findViewById(R.id.btnClose).setOnClickListener { hideStopOrBusBottomSheet() diff --git a/app/src/main/res/drawable/bottom_sheet_background.xml b/app/src/main/res/drawable/bottom_sheet_background.xml index 4319073..fede6dd 100644 --- a/app/src/main/res/drawable/bottom_sheet_background.xml +++ b/app/src/main/res/drawable/bottom_sheet_background.xml @@ -2,6 +2,6 @@ + android:topLeftRadius="16sp" + android:topRightRadius="16sp" /> \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_map_pin.xml b/app/src/main/res/drawable/ic_map_pin.xml new file mode 100644 index 0000000..cfac477 --- /dev/null +++ b/app/src/main/res/drawable/ic_map_pin.xml @@ -0,0 +1,12 @@ + + + + diff --git a/app/src/main/res/layout/include_map_bottom_sheet.xml b/app/src/main/res/layout/include_map_bottom_sheet.xml index 63ffb40..40145a9 100644 --- a/app/src/main/res/layout/include_map_bottom_sheet.xml +++ b/app/src/main/res/layout/include_map_bottom_sheet.xml @@ -205,14 +205,53 @@ + + Location enabled Location disabled Location is disabled on device + Map already centered on stop + Map already centered on vehicle