-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMapPolylineBuilder.kt.kt
More file actions
96 lines (83 loc) · 2.83 KB
/
MapPolylineBuilder.kt.kt
File metadata and controls
96 lines (83 loc) · 2.83 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
package com.rngooglemapsplus
import android.graphics.Color
import com.facebook.react.uimanager.PixelUtil.dpToPx
import com.google.android.gms.maps.model.ButtCap
import com.google.android.gms.maps.model.Cap
import com.google.android.gms.maps.model.JointType
import com.google.android.gms.maps.model.Polyline
import com.google.android.gms.maps.model.PolylineOptions
import com.google.android.gms.maps.model.RoundCap
import com.google.android.gms.maps.model.SquareCap
import com.rngooglemapsplus.extensions.toColor
import com.rngooglemapsplus.extensions.toLatLng
class MapPolylineBuilder {
fun build(pl: RNPolyline): PolylineOptions =
PolylineOptions().apply {
pl.coordinates.forEach { pt ->
add(pt.toLatLng())
}
pl.width?.let { width(it.dpToPx()) }
pl.lineCap?.let {
startCap(mapLineCap(it))
endCap(mapLineCap(it))
}
pl.lineJoin?.let { jointType(mapLineJoin(it)) }
pl.color?.let { color(it.toColor()) }
pl.geodesic?.let { geodesic(it) }
pl.pressable?.let { clickable(it) }
pl.zIndex?.let { zIndex(it.toFloat()) }
}
fun update(
prev: RNPolyline,
next: RNPolyline,
polyline: Polyline,
) {
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) {
polyline.points = next.coordinates.map { it.toLatLng() }
}
if (prev.width != next.width) {
polyline.width = next.width?.dpToPx() ?: 1f
}
val newCap = mapLineCap(next.lineCap ?: RNLineCapType.BUTT)
val prevCap = mapLineCap(prev.lineCap ?: RNLineCapType.BUTT)
if (newCap != prevCap) {
polyline.startCap = newCap
polyline.endCap = newCap
}
val newJoin = mapLineJoin(next.lineJoin ?: RNLineJoinType.MITER)
val prevJoin = mapLineJoin(prev.lineJoin ?: RNLineJoinType.MITER)
if (newJoin != prevJoin) {
polyline.jointType = newJoin
}
if (prev.color != next.color) {
polyline.color = next.color?.toColor() ?: Color.BLACK
}
if (prev.pressable != next.pressable) {
polyline.isClickable = next.pressable ?: false
}
if (prev.geodesic != next.geodesic) {
polyline.isGeodesic = next.geodesic ?: false
}
if (prev.zIndex != next.zIndex) {
polyline.zIndex = next.zIndex?.toFloat() ?: 0f
}
}
private fun mapLineCap(type: RNLineCapType?): Cap =
when (type) {
RNLineCapType.ROUND -> RoundCap()
RNLineCapType.SQUARE -> SquareCap()
else -> ButtCap()
}
private fun mapLineJoin(type: RNLineJoinType?): Int =
when (type) {
RNLineJoinType.ROUND -> JointType.ROUND
RNLineJoinType.BEVEL -> JointType.BEVEL
RNLineJoinType.MITER -> JointType.DEFAULT
null -> JointType.DEFAULT
}
}