aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt
blob: 681a21f34b98e00b736700ad3de0dec4fbc16f75 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package mx.trackermap.TrackerMap.android.map

import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import com.mousebird.maply.*
import java.io.File
import mx.trackermap.TrackerMap.android.R
import mx.trackermap.TrackerMap.utils.MarkerType

typealias MarkerCallback = (Int?) -> Unit

class MapFragment : GlobeMapFragment() {

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

    var markerCallback: MarkerCallback? = null

    private val objects = mutableListOf<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(
            getString(R.string.maps_tile_url),
            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
        val longitude = -100.36
        focusOn(latitude, longitude, zoom = 0.4, animated = false)
    }

    override fun userDidSelect(
        mapControl: MapController?,
        selObjs: Array<out SelectedObject>?,
        loc: Point2d?,
        screenLoc: Point2d?
    ) {
        super.userDidSelect(mapControl, selObjs, loc, screenLoc)

        selObjs?.forEach { selectedObject ->
            if (selectedObject.selObj is ScreenMarker) {
                val screenMarker = selectedObject.selObj as ScreenMarker
                val markerId = screenMarker.userObject as Int
                Log.d("MapFragment", "Selected marker with id: $markerId")

                markerCallback?.let { it(markerId) }
            }
        }
    }

    override fun userDidTap(mapControl: MapController?, loc: Point2d?, screenLoc: Point2d?) {
        super.userDidTap(mapControl, loc, screenLoc)

        markerCallback?.let { it(null) }
    }

    private fun clear() {
        mapControl.removeObjects(
            objects,
            ThreadMode.ThreadAny
        )
        objects.clear()
    }

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

        clear()

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

            screenMarker.loc = Point2d.FromDegrees(marker.longitude, marker.latitude)
            screenMarker.image = if (isReport) {
                when (i) {
                    0 -> getIcon(MarkerType.REPORT_START)
                    markers.size - 1 -> getIcon(MarkerType.REPORT_END)
                    else -> getIcon(MarkerType.REPORT_POSITION)
                }
            } else getIcon(marker.type)
            screenMarker.size = markerSize
            screenMarker.userObject = marker.id
            screenMarker.selectable = true

            screenMarker
        }

        objects.add(mapControl.addScreenMarkers(
            screenMarkers,
            MarkerInfo(),
            ThreadMode.ThreadAny
        ))

        if (isReport && markers.isNotEmpty()) {
            Log.d("MapFragment", "Displaying report")

            val points = markers.map { marker ->
                Point2d.FromDegrees(marker.longitude, marker.latitude)
            }.toTypedArray()

            // Draw polyline
            val vector = VectorObject()
            vector.addAreal(points)

            val vectorInfo = VectorInfo()
            vectorInfo.setColor(Color.GREEN)
            vectorInfo.setLineWidth(25.0f)

            objects.add(mapControl.addVector(
                vector,
                vectorInfo,
                ThreadMode.ThreadAny
            ))

            // Center map to polyline bounds
            val mbr = Mbr()
            points.forEach {
                mbr.addPoint(it)
            }
            mbr.expandByFraction(0.1)

            mapControl.addPostSurfaceRunnable {
                val zoom = mapControl.findHeightToViewBounds(mbr, mbr.middle())
                mapControl.setPositionGeo(mbr.middle(), zoom)
            }
        }
    }

    fun focusOn(latitude: Double, longitude: Double, zoom: Double = 0.0000144, animated: Boolean = true) {
        val lat = latitude * Math.PI / 180
        val lon = longitude * Math.PI / 180
        if (animated) {
            mapControl.animatePositionGeo(lon, lat, zoom, 0.5)
        } else {
            mapControl.setPositionGeo(lon, lat, zoom)
        }
    }

    private fun getIcon(markerType: MarkerType): Bitmap {
        return ResourcesCompat.getDrawable(
            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

                MarkerType.REPORT_POSITION -> R.drawable.map_report_position
                MarkerType.REPORT_START -> R.drawable.map_report_start
                MarkerType.REPORT_END -> R.drawable.map_report_end
            }, activity!!.theme)!!.toBitmap(144, 144)
    }
}