-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMapPolygonBuilder.kt
More file actions
86 lines (74 loc) · 2.49 KB
/
MapPolygonBuilder.kt
File metadata and controls
86 lines (74 loc) · 2.49 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
package com.rngooglemapsplus
import android.graphics.Color
import com.facebook.react.uimanager.PixelUtil.dpToPx
import com.google.android.gms.maps.model.Polygon
import com.google.android.gms.maps.model.PolygonOptions
import com.rngooglemapsplus.extensions.toColor
import com.rngooglemapsplus.extensions.toLatLng
class MapPolygonBuilder {
fun build(poly: RNPolygon): PolygonOptions =
PolygonOptions().apply {
poly.coordinates.forEach { pt ->
add(
pt.toLatLng(),
)
}
poly.fillColor?.let { fillColor(it.toColor()) }
poly.strokeColor?.let { strokeColor(it.toColor()) }
poly.strokeWidth?.let { strokeWidth(it.dpToPx()) }
poly.pressable?.let { clickable(it) }
poly.geodesic?.let { geodesic(it) }
poly.holes?.forEach { hole ->
addHole(hole.coordinates.map { it.toLatLng() })
}
poly.zIndex?.let { zIndex(it.toFloat()) }
}
fun update(
prev: RNPolygon,
next: RNPolygon,
poly: Polygon,
) {
val coordsChanged =
prev.coordinates.size != next.coordinates.size ||
!prev.coordinates.zip(next.coordinates).all { (a, b) ->
a.latitude == b.latitude && a.longitude == b.longitude
}
if (coordsChanged) {
poly.points = next.coordinates.map { it.toLatLng() }
}
val prevHoles = prev.holes?.toList() ?: emptyList()
val nextHoles = next.holes?.toList() ?: emptyList()
val holesChanged =
prevHoles.size != nextHoles.size ||
!prevHoles.zip(nextHoles).all { (ha, hb) ->
ha.coordinates.size == hb.coordinates.size &&
ha.coordinates.zip(hb.coordinates).all { (a, b) ->
a.latitude == b.latitude && a.longitude == b.longitude
}
}
if (holesChanged) {
poly.holes =
nextHoles.map { hole ->
hole.coordinates.map { it.toLatLng() }
}
}
if (prev.fillColor != next.fillColor) {
poly.fillColor = next.fillColor?.toColor() ?: Color.TRANSPARENT
}
if (prev.strokeColor != next.strokeColor) {
poly.strokeColor = next.strokeColor?.toColor() ?: Color.BLACK
}
if (prev.strokeWidth != next.strokeWidth) {
poly.strokeWidth = next.strokeWidth?.dpToPx() ?: 1f
}
if (prev.pressable != next.pressable) {
poly.isClickable = next.pressable ?: false
}
if (prev.geodesic != next.geodesic) {
poly.isGeodesic = next.geodesic ?: false
}
if (prev.zIndex != next.zIndex) {
poly.zIndex = next.zIndex?.toFloat() ?: 0f
}
}
}