aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt
blob: 289cc2ddee471f8d18639e4b5a013f5c8b267199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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

@DelicateCoroutinesApi
class MapFragment: GlobeMapFragment() {

    private val unitsViewModel: UnitsViewModel by viewModel()

    private val markers = mutableListOf<ScreenMarker>()

    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)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        setupObservers()
    }

    @DelicateCoroutinesApi
    private fun setupObservers() {
        unitsViewModel.units.observe(this) { 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,
                                when (unit.device.category?.lowercase()) {
                                    "animal" -> R.drawable.map_animal
                                    "bicycle" -> R.drawable.map_bicycle
                                    "boat" -> R.drawable.map_boat
                                    "bus" -> R.drawable.map_bus
                                    "car" -> R.drawable.map_car
                                    "crane" -> R.drawable.map_crane
                                    "default" -> R.drawable.map_default
                                    "helicopter" -> R.drawable.map_helicopter
                                    "motorcycle" -> R.drawable.map_motorcycle
                                    "offroad" -> R.drawable.map_offroad
                                    "person" -> R.drawable.map_person
                                    "pickup" -> R.drawable.map_pickup
                                    "plane" -> R.drawable.map_plane
                                    "scooter" -> R.drawable.map_scooter
                                    "ship" -> R.drawable.map_ship
                                    "tractor" -> R.drawable.map_tractor
                                    "train" -> R.drawable.map_train
                                    "tram" -> R.drawable.map_tram
                                    "trolleybus" -> R.drawable.map_trolleybus
                                    "truck" -> R.drawable.map_truck
                                    "van" -> R.drawable.map_van
                                    else -> R.drawable.map_default
                                }
                            )
                        val markerSize = Point2d(144.0, 144.0)

                        val marker = ScreenMarker()
                        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)
                    }
                }
            }
        }
        unitsViewModel.selectedUnit.observe(this) {
            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
                    val zoom = 0.000008
                    mapControl.setPositionGeo(longitude, latitude, zoom)
                }
            }
        }
    }
}