Zone.kt
1.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
package fdit.leafletmap
class Zone constructor(private var title: String) {
private lateinit var map: LeafletMapView
private var isAttached = false
private var isDisplayed = false
private var positions = listOf<LatLong>()
fun addToMap(map: LeafletMapView) {
this.map = map
if (map.execScript("typeof zone$title == 'undefined';") as Boolean) {
map.execScript("var zone$title")
}
if (!this.isAttached) {
map.execScript("var points$title = [];" +
"zone$title = L.polygon(points$title).addTo(myMap);")
this.isAttached = true
this.isDisplayed = true
} else if (!this.isDisplayed) {
map.execScript("zone$title.addTo(myMap);")
this.isDisplayed = true
}
}
private fun addPoint(latLong: LatLong) {
map.execScript("points$title.push([${latLong.latitude}, ${latLong.longitude}]);")
}
fun updatePoints(positions: List<LatLong>) {
this.positions = positions
if (map.execScript("typeof points$title == 'undefined'") as Boolean) {
map.execScript("var points$title = [];")
} else {
map.execScript("points$title = [];")
}
for (position in positions) {
addPoint(position)
}
}
fun updateMap() {
if (this.isAttached) {
map.execScript("myMap.removeLayer(zone$title);" +
"zone$title = L.polygon(points$title).addTo(myMap);")
this.isDisplayed = true
}
}
internal fun removeZone() {
if (this.isAttached && this.isDisplayed) {
map.execScript("myMap.removeLayer(zone$title);")
this.isDisplayed = false
}
}
}