diff options
Diffstat (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android/map')
-rw-r--r-- | androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt | 33 |
1 files changed, 17 insertions, 16 deletions
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 289cc2d..70a0236 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 @@ -1,19 +1,14 @@ package mx.trackermap.TrackerMap.android.map import android.graphics.BitmapFactory -import android.graphics.Point import android.os.Bundle -import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.mousebird.maply.* -import io.ktor.util.* import kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R -import mx.trackermap.TrackerMap.android.databinding.MapFragmentBinding import mx.trackermap.TrackerMap.android.units.UnitsViewModel -import mx.trackermap.TrackerMap.client.models.UnitInformation import org.koin.androidx.viewmodel.ext.android.viewModel import java.io.File @@ -22,7 +17,7 @@ class MapFragment: GlobeMapFragment() { private val unitsViewModel: UnitsViewModel by viewModel() - private val markers = mutableListOf<ScreenMarker>() + private val markers = mutableListOf<Pair<ScreenMarker, ComponentObject>>() override fun chooseDisplayType(): MapDisplayType { return MapDisplayType.Map @@ -59,7 +54,7 @@ class MapFragment: GlobeMapFragment() { val latitude = 23.191 * Math.PI / 180 val longitude = -100.36 * Math.PI / 180 val zoom = 0.4 - mapControl.animatePositionGeo(longitude, latitude, zoom, 1.0) + mapControl.setPositionGeo(longitude, latitude, zoom) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -70,13 +65,17 @@ class MapFragment: GlobeMapFragment() { @DelicateCoroutinesApi private fun setupObservers() { - unitsViewModel.units.observe(this) { units -> + unitsViewModel.units.observe(viewLifecycleOwner) { units -> + /* Remove all markers */ + markers.forEach { + mapControl.removeObject(it.second, ThreadMode.ThreadAny) + } + markers.clear() + + /* Reinsert markers from units */ units.forEach { unit -> unit.position?.let { position -> if (position.longitude != null && position.longitude != null) { - - /* Add marker */ - val markerInfo = MarkerInfo() val icon = BitmapFactory.decodeResource( activity!!.resources, @@ -105,23 +104,25 @@ class MapFragment: GlobeMapFragment() { else -> R.drawable.map_default } ) - val markerSize = Point2d(144.0, 144.0) val marker = ScreenMarker() + val markerSize = Point2d(144.0, 144.0) + marker.loc = Point2d.FromDegrees(position.longitude!!, position.latitude!!) marker.image = icon marker.size = markerSize marker.userObject = unit - markers.add(marker) - mapControl.addScreenMarker(marker, markerInfo, ThreadMode.ThreadAny) + /* Add marker to map */ + val markerInfo = MarkerInfo() + val componentObject = mapControl.addScreenMarker(marker, markerInfo, ThreadMode.ThreadAny) + markers.add(marker to componentObject) } } } } - unitsViewModel.selectedUnit.observe(this) { + unitsViewModel.selectedUnit.observe(viewLifecycleOwner) { it?.let { unit -> - Log.d("MapFragment", "Centering map on ${it.position?.latitude}, ${it.position?.longitude}") unit.position?.let { position -> val latitude = position.latitude!! * Math.PI / 180 val longitude = position.longitude!! * Math.PI / 180 |