From 5b39665ccdf318fe3b43fa76519484bc98764884 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Tue, 18 Jan 2022 00:25:39 -0600 Subject: Improvements to zoom on UnitMapFragment.kt --- .../TrackerMap/android/map/UnitMapFragment.kt | 36 +++++++++++++++++----- .../TrackerMap/android/units/UnitsViewModel.kt | 13 ++++++++ 2 files changed, 42 insertions(+), 7 deletions(-) (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt index 768c0f0..ca59266 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt @@ -9,6 +9,7 @@ import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.fragment.app.commit import androidx.fragment.app.viewModels +import com.mousebird.maply.Point2d import kotlin.time.ExperimentalTime import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R @@ -30,7 +31,7 @@ class UnitMapFragment : Fragment() { private val binding get() = _binding!! private lateinit var mapFragment: MapWrapperFragment - private var shouldCenter = true + private var shouldCenterMap = true override fun onCreateView( inflater: LayoutInflater, @@ -60,7 +61,7 @@ class UnitMapFragment : Fragment() { } private fun initializeMap() { - shouldCenter = true + shouldCenterMap = true mapFragment = MapWrapperFragment.newInstance( overlayMarginTop = resources.getDimensionPixelSize(R.dimen.nav_height) ) @@ -78,10 +79,12 @@ class UnitMapFragment : Fragment() { mapFragment.display( units.toTypedArray(), isReport = false, - center = shouldCenter && unitsViewModel.selectedUnit.value == null + center = shouldCenterMap + && unitsViewModel.selectedUnit.value == null + && unitsViewModel.camera.value == null ) if (units.isNotEmpty()) { - shouldCenter = false + shouldCenterMap = false } } @@ -91,7 +94,8 @@ class UnitMapFragment : Fragment() { binding.mapUnitCard.visibility = if (selectedUnit == null) View.GONE else View.VISIBLE selectedUnit?.let { unit -> - if (unit.position == null || unit.position!!.latitude == null || unit.position!!.longitude == null) { + if (unit.position == null || unit.position?.latitude == null || unit.position?.longitude == null) { + binding.mapUnitCard.visibility = View.GONE return@let } @@ -107,7 +111,14 @@ class UnitMapFragment : Fragment() { ), binding.mapUnitCard.context, unit, this::itemAction ) - mapFragment.focusOn(unit.position!!.latitude!!, unit.position!!.longitude!!) + unitsViewModel.moveCamera( + UnitsViewModel.Camera( + point = Point2d( + unit.position!!.longitude!!, unit.position!!.latitude!! + ), + animated = true + ) + ) } } @@ -115,13 +126,24 @@ class UnitMapFragment : Fragment() { Log.d("UnitMapFragment", "Loading layer!") mapFragment.updateLayer(type) unitsViewModel.selectedUnit.value?.let { - mapFragment.focusOn(it.position!!.latitude!!, it.position!!.longitude!!) + unitsViewModel.moveCamera( + UnitsViewModel.Camera( + point = Point2d( + it.position!!.longitude!!, it.position!!.latitude!! + ), + animated = false + ) + ) } } unitsViewModel.geofences.observe(viewLifecycleOwner) { geofences -> mapFragment.display(geofences.values.toTypedArray()) } + + unitsViewModel.camera.observe(viewLifecycleOwner) { camera -> + mapFragment.focusOn(camera.point.y, camera.point.x) + } } private fun removeObservers() { diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt index efa5781..ee6fa98 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt @@ -2,6 +2,7 @@ package mx.trackermap.TrackerMap.android.units import android.util.Log import androidx.lifecycle.* +import com.mousebird.maply.Point2d import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch @@ -14,6 +15,7 @@ import org.koin.core.component.KoinComponent import org.koin.core.component.inject import kotlin.time.ExperimentalTime + @DelicateCoroutinesApi @ExperimentalTime class UnitsViewModel( @@ -24,6 +26,11 @@ class UnitsViewModel( MAP, LIST } + data class Camera ( + val point: Point2d, + val animated: Boolean, + ) + private val unitsController: UnitsController by inject() private val geofenceController: GeofencesController by inject() @@ -33,6 +40,7 @@ class UnitsViewModel( private var _selectedUnit = MutableLiveData() private var _mapLayerType = MutableLiveData() private var _geofences = MutableLiveData>() + private val _camera = MutableLiveData() val searchQuery: LiveData get() = _searchQuery val unitsDisplayMode: LiveData get() = _unitsDisplayMode @@ -40,6 +48,7 @@ class UnitsViewModel( val selectedUnit: LiveData get() = _selectedUnit val mapLayerType: LiveData get() = _mapLayerType val geofences: LiveData> get() = _geofences + val camera: LiveData get() = _camera init { Log.d("UnitsViewModel", "Initializing Units View Model") @@ -106,4 +115,8 @@ class UnitsViewModel( fun search(query: String) { unitsController.search(query) } + + fun moveCamera(point: Camera) { + _camera.postValue(point) + } } \ No newline at end of file -- cgit v1.2.3 From 7dd62c690e4b9db4a98c43591caa297b97c89b37 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Tue, 18 Jan 2022 00:29:31 -0600 Subject: More improvements to zoom --- .../java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt | 8 +++++++- .../java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt | 8 ++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt index ca59266..e64afd3 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt @@ -93,6 +93,10 @@ class UnitMapFragment : Fragment() { binding.mapUnitCard.visibility = if (selectedUnit == null) View.GONE else View.VISIBLE + if (selectedUnit == null) { + unitsViewModel.clearCamera() + } + selectedUnit?.let { unit -> if (unit.position == null || unit.position?.latitude == null || unit.position?.longitude == null) { binding.mapUnitCard.visibility = View.GONE @@ -142,7 +146,9 @@ class UnitMapFragment : Fragment() { } unitsViewModel.camera.observe(viewLifecycleOwner) { camera -> - mapFragment.focusOn(camera.point.y, camera.point.x) + camera?.let { + mapFragment.focusOn(camera.point.y, camera.point.x) + } } } diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt index ee6fa98..cb30725 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt @@ -40,7 +40,7 @@ class UnitsViewModel( private var _selectedUnit = MutableLiveData() private var _mapLayerType = MutableLiveData() private var _geofences = MutableLiveData>() - private val _camera = MutableLiveData() + private val _camera = MutableLiveData() val searchQuery: LiveData get() = _searchQuery val unitsDisplayMode: LiveData get() = _unitsDisplayMode @@ -48,7 +48,7 @@ class UnitsViewModel( val selectedUnit: LiveData get() = _selectedUnit val mapLayerType: LiveData get() = _mapLayerType val geofences: LiveData> get() = _geofences - val camera: LiveData get() = _camera + val camera: LiveData get() = _camera init { Log.d("UnitsViewModel", "Initializing Units View Model") @@ -119,4 +119,8 @@ class UnitsViewModel( fun moveCamera(point: Camera) { _camera.postValue(point) } + + fun clearCamera() { + _camera.postValue(null) + } } \ No newline at end of file -- cgit v1.2.3 From e679a7d019391fce98a8a30458d09759733d60b1 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Tue, 18 Jan 2022 00:33:05 -0600 Subject: Changed default zoom for focusOn --- .../src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt | 2 +- .../java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap') diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt index 87fb44a..37956c1 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt @@ -344,7 +344,7 @@ open class MapFragment : GlobeMapFragment() { fun focusOn( latitude: Double, longitude: Double, - height: Double = 0.00001, + height: Double = 0.0000264, animated: Boolean = true ) { val lat = latitude * Math.PI / 180 diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt index 01587ae..c8ef688 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapWrapperFragment.kt @@ -130,7 +130,7 @@ class MapWrapperFragment: Fragment() { fun focusOn( latitude: Double, longitude: Double, - height: Double = 0.00001, + height: Double = 0.0000264, animated: Boolean = true ) { Log.d("MapWrapperFragment", "Focusing on $latitude, $longitude") -- cgit v1.2.3