package mx.trackermap.TrackerMap.android.map import android.graphics.BitmapFactory 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 kotlinx.coroutines.DelicateCoroutinesApi import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.android.databinding.MapFragmentBinding import mx.trackermap.TrackerMap.android.units.UnitsViewModel import org.koin.androidx.viewmodel.ext.android.viewModel import java.io.File @DelicateCoroutinesApi class MapFragment: GlobeMapFragment() { private var _binding: MapFragmentBinding? = null private val binding get() = _binding!! private val unitsViewModel: UnitsViewModel by viewModel() override fun chooseDisplayType(): MapDisplayType { return MapDisplayType.Map } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { super.onCreateView(inflater, container, savedInstanceState) return baseControl.contentView!! } override fun controlHasStarted() { val cacheDirName = "stamen_watercolor6" val cacheDir = File(activity!!.cacheDir, cacheDirName) cacheDir.mkdir() val tileInfo = RemoteTileInfoNew("https://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", 0, 21) tileInfo.cacheDir = cacheDir val params = SamplingParams() params.coordSystem = SphericalMercatorCoordSystem() params.coverPoles = true params.edgeMatching = true params.minZoom = tileInfo.minZoom params.maxZoom = tileInfo.maxZoom params.singleLevel = true val loader = QuadImageLoader(params, tileInfo, baseControl) loader.setImageFormat(RenderController.ImageFormat.MaplyImageUShort565) 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) insertMarkers() } private fun insertMarkers() { val markerInfo = MarkerInfo() val icon = BitmapFactory.decodeResource(activity!!.resources, R.drawable.map_default) val markerSize = Point2d(80.0, 80.0) val moscow = ScreenMarker() moscow.loc = Point2d.FromDegrees(37.616667, 55.75) moscow.image = icon moscow.size = markerSize mapControl.addScreenMarker(moscow, markerInfo, ThreadMode.ThreadAny) } override fun onDestroyView() { super.onDestroyView() _binding = null } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) setupObservers() } @DelicateCoroutinesApi private fun setupObservers() { unitsViewModel.units.observe(this) { units -> Log.d("MapFragment", "Success $units") } } }