aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt
blob: bd1701344a1b2b4a03c98b0110a05241b6e88f6e (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package mx.trackermap.TrackerMap.android.map

import android.graphics.Bitmap
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.ComponentObject
import com.mousebird.maply.GlobeMapFragment
import com.mousebird.maply.MarkerInfo
import com.mousebird.maply.Point2d
import com.mousebird.maply.QuadImageLoader
import com.mousebird.maply.RemoteTileInfoNew
import com.mousebird.maply.RenderController
import com.mousebird.maply.RenderControllerInterface
import com.mousebird.maply.SamplingParams
import com.mousebird.maply.ScreenMarker
import com.mousebird.maply.SphericalMercatorCoordSystem
import java.io.File
import kotlinx.coroutines.DelicateCoroutinesApi
import mx.trackermap.TrackerMap.android.R

@DelicateCoroutinesApi
class MapFragment : GlobeMapFragment() {

    enum class MarkerType {
        ANIMAL, BICYCLE, BOAT, BUS, CAR, CRANE, DEFAULT, HELICOPTER, MOTORCYCLE, OFFROAD, PERSON,
        PICKUP, PLANE, SCOOTER, SHIP, TRACTOR, TRAIN, TRAM, TROLLEYBUS, TRUCK, VAN
    }

    data class Marker(
        val id: Int,
        val latitude: Double,
        val longitude: Double,
        val type: MarkerType = MarkerType.DEFAULT
    )

    private val markers = mutableListOf<Pair<ScreenMarker, ComponentObject>>()

    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.setPositionGeo(longitude, latitude, zoom)
    }

    fun clear() {
        mapControl.removeObjects(
            markers.map { it.second },
            RenderControllerInterface.ThreadMode.ThreadAny
        )
    }

    fun display(markers: Array<Marker>) {
        Log.d("MapFragment", "Displaying markers")

        clear()

        val screenMarkers = markers.map { marker ->
            val screenMarker = ScreenMarker()
            val markerSize = Point2d(144.0, 144.0)

            screenMarker.loc = Point2d.FromDegrees(marker.longitude, marker.latitude)
            screenMarker.image = getIcon(marker.type)
            screenMarker.size = markerSize
            screenMarker.userObject = marker.id

            screenMarker
        }
        mapControl.addScreenMarkers(
            screenMarkers,
            MarkerInfo(),
            RenderControllerInterface.ThreadMode.ThreadAny
        )
    }

    fun focusOn(latitude: Double, longitude: Double) {
        val lat = latitude * Math.PI / 180
        val lon = longitude * Math.PI / 180
        val zoom = 0.000008
        mapControl.setPositionGeo(lon, lat, zoom)
    }

    private fun getIcon(markerType: MarkerType): Bitmap {
        return BitmapFactory.decodeResource(
            activity!!.resources,
            when (markerType) {
                MarkerType.ANIMAL -> R.drawable.map_animal
                MarkerType.BICYCLE -> R.drawable.map_bicycle
                MarkerType.BOAT -> R.drawable.map_boat
                MarkerType.BUS -> R.drawable.map_bus
                MarkerType.CAR -> R.drawable.map_car
                MarkerType.CRANE -> R.drawable.map_crane
                MarkerType.DEFAULT -> R.drawable.map_default
                MarkerType.HELICOPTER -> R.drawable.map_helicopter
                MarkerType.MOTORCYCLE -> R.drawable.map_motorcycle
                MarkerType.OFFROAD -> R.drawable.map_offroad
                MarkerType.PERSON -> R.drawable.map_person
                MarkerType.PICKUP -> R.drawable.map_pickup
                MarkerType.PLANE -> R.drawable.map_plane
                MarkerType.SCOOTER -> R.drawable.map_scooter
                MarkerType.SHIP -> R.drawable.map_ship
                MarkerType.TRACTOR -> R.drawable.map_tractor
                MarkerType.TRAIN -> R.drawable.map_train
                MarkerType.TRAM -> R.drawable.map_tram
                MarkerType.TROLLEYBUS -> R.drawable.map_trolleybus
                MarkerType.TRUCK -> R.drawable.map_truck
                MarkerType.VAN -> R.drawable.map_van
            }
        )
    }
}