-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMapMarker.kt
More file actions
145 lines (127 loc) · 3.63 KB
/
MapMarker.kt
File metadata and controls
145 lines (127 loc) · 3.63 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
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.LatLng
import com.google.android.gms.maps.model.MarkerOptions
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 MarkerOptions(
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()
.position(LatLng(m.coordinate.latitude, m.coordinate.longitude))
.zIndex(m.zIndex.toFloat())
.icon(icon)
.anchor((m.anchor?.x ?: 0.5).toFloat(), (m.anchor?.y ?: 0.5).toFloat())
fun buildIconAsync(
id: String,
m: RNMarker,
onReady: (BitmapDescriptor) -> Unit,
) {
jobsById[id]?.cancel()
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
try {
coroutineContext.ensureActive()
val svg = SVG.getFromString(m.iconSvg)
coroutineContext.ensureActive()
svg.setDocumentWidth(m.width.dpToPx())
svg.setDocumentHeight(m.height.dpToPx())
coroutineContext.ensureActive()
bmp =
createBitmap(m.width.dpToPx().toInt(), m.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
}
}
}
fun RNMarker.markerEquals(b: RNMarker): Boolean =
id == b.id &&
zIndex == b.zIndex &&
coordinate == b.coordinate &&
anchor == b.anchor &&
markerStyleEquals(b)
fun RNMarker.markerStyleEquals(b: RNMarker): Boolean =
width == b.width &&
height == b.height &&
iconSvg == b.iconSvg
fun RNMarker.styleHash(): Int =
arrayOf<Any?>(
width,
height,
iconSvg,
).contentHashCode()