-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMapMarkerBuilder.kt
More file actions
181 lines (165 loc) · 4.74 KB
/
MapMarkerBuilder.kt
File metadata and controls
181 lines (165 loc) · 4.74 KB
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
package com.rngooglemapsplus
import android.graphics.Bitmap
import android.graphics.Canvas
import android.util.LruCache
import androidx.core.graphics.createBitmap
import com.caverock.androidsvg.SVG
import com.facebook.react.uimanager.PixelUtil.dpToPx
import com.google.android.gms.maps.model.BitmapDescriptor
import com.google.android.gms.maps.model.BitmapDescriptorFactory
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import com.rngooglemapsplus.extensions.markerStyleEquals
import com.rngooglemapsplus.extensions.styleHash
import com.rngooglemapsplus.extensions.toLatLng
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.coroutines.coroutineContext
class MapMarkerBuilder(
private val scope: CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default),
) {
private val iconCache =
object : LruCache<Int, BitmapDescriptor>(512) {
override fun sizeOf(
key: Int,
value: BitmapDescriptor,
): Int = 1
}
private val jobsById = mutableMapOf<String, Job>()
fun build(
m: RNMarker,
icon: BitmapDescriptor?,
): MarkerOptions =
MarkerOptions().apply {
position(m.coordinate.toLatLng())
icon(icon)
m.title?.let { title(it) }
m.snippet?.let { snippet(it) }
m.opacity?.let { alpha(it.toFloat()) }
m.flat?.let { flat(it) }
m.draggable?.let { draggable(it) }
m.rotation?.let { rotation(it.toFloat()) }
m.infoWindowAnchor?.let { infoWindowAnchor(it.x.toFloat(), it.y.toFloat()) }
m.anchor?.let { anchor((m.anchor.x).toFloat(), (m.anchor.y).toFloat()) }
m.zIndex?.let { zIndex(it.toFloat()) }
}
fun update(
marker: Marker,
prev: RNMarker,
next: RNMarker,
) {
marker.position =
next.coordinate.toLatLng()
if (!prev.markerStyleEquals(next)) {
buildIconAsync(marker.id, next) { icon ->
marker.setIcon(icon)
}
}
marker.title = next.title
marker.snippet = next.snippet
marker.alpha = next.opacity?.toFloat() ?: 0f
marker.isFlat = next.flat ?: false
marker.isDraggable = next.draggable ?: false
marker.rotation = next.rotation?.toFloat() ?: 0f
marker.setInfoWindowAnchor(
(next.infoWindowAnchor?.x ?: 0.5).toFloat(),
(next.infoWindowAnchor?.y ?: 0).toFloat(),
)
marker.setAnchor(
(next.anchor?.x ?: 0.5).toFloat(),
(next.anchor?.y ?: 1.0).toFloat(),
)
marker.zIndex = next.zIndex?.toFloat() ?: 0f
}
fun buildIconAsync(
id: String,
m: RNMarker,
onReady: (BitmapDescriptor?) -> Unit,
) {
jobsById[id]?.cancel()
if (m.iconSvg == null) {
onReady(null)
return
}
val key = m.styleHash()
iconCache.get(key)?.let { cached ->
onReady(cached)
return
}
val job =
scope.launch {
try {
ensureActive()
val bmp = renderBitmap(m)
if (bmp != null) {
ensureActive()
val desc = BitmapDescriptorFactory.fromBitmap(bmp)
iconCache.put(key, desc)
bmp.recycle()
withContext(Dispatchers.Main) {
ensureActive()
onReady(desc)
}
}
} catch (_: OutOfMemoryError) {
iconCache.evictAll()
} catch (_: Throwable) {
} finally {
jobsById.remove(id)
}
}
jobsById[id] = job
}
fun cancelIconJob(id: String) {
jobsById[id]?.cancel()
jobsById.remove(id)
}
fun cancelAllJobs() {
val ids = jobsById.keys.toList()
ids.forEach { id ->
jobsById[id]?.cancel()
}
jobsById.clear()
iconCache.evictAll()
}
private suspend fun renderBitmap(m: RNMarker): Bitmap? {
var bmp: Bitmap? = null
if (m.iconSvg == null) {
return null
}
try {
coroutineContext.ensureActive()
val svg = SVG.getFromString(m.iconSvg.svgString)
coroutineContext.ensureActive()
svg.setDocumentWidth(m.iconSvg.width.dpToPx())
svg.setDocumentHeight(m.iconSvg.height.dpToPx())
coroutineContext.ensureActive()
bmp =
createBitmap(
m.iconSvg.width
.dpToPx()
.toInt(),
m.iconSvg.height
.dpToPx()
.toInt(),
Bitmap.Config.ARGB_8888,
)
coroutineContext.ensureActive()
val canvas = Canvas(bmp)
svg.renderToCanvas(canvas)
coroutineContext.ensureActive()
return bmp
} catch (t: Throwable) {
try {
bmp?.recycle()
} catch (_: Throwable) {
}
throw t
}
}
}