-
Notifications
You must be signed in to change notification settings - Fork 43
/
map.kt
235 lines (197 loc) · 7.72 KB
/
map.kt
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import declarations.alert
import declarations.append
import declarations.j
import jquery.jq
import openlayers.ol
import openlayers.olx
import org.w3c.dom.EventSource
import org.w3c.dom.HTMLOptionElement
import org.w3c.dom.MessageEvent
import kotlin.Pair
import kotlin.browser.document
private fun fillUsersSelect() {
j.getJSON("/user") { data: Array<dynamic> ->
for (d in data) {
val user = (document.createElement("option") as HTMLOptionElement).apply {
value = d.userName
text = d.firstName + " " + d.lastName
}
jq("#select-user").append(user)
}
}
}
private fun createMap(): Pair<ol.View, ol.Map> {
val view = ol.View(object : olx.ViewOptions {
override var zoom: Number? = 4
})
val map = ol.Map(object : olx.MapOptions {
override var layers: Array<Any>? =
arrayOf(
ol.layer.Tile(object/* : olx.layer.TileOptions*/ {
/*override */var source: ol.source.Source? = ol.source.MapQuest(object {
val layer = "osm"
})
} as olx.layer.TileOptions)
)
override var target: Any? = "map"
override var controls: Any? = ol.control.defaults(object : olx.control.DefaultsOptions {
override var attributionOptions: Any? = object {
val collapsible = false
}
})
override var view: ol.View? = view
})
return Pair(view, map)
}
private fun setupGeolocation(map: ol.Map, view: ol.View) {
val geolocation = ol.Geolocation(object : olx.GeolocationOptions {
override var projection = view.getProjection()
})
geolocation.on("error", { error ->
// todo test: looks like error doesn't have message
alert("Geolocation error: " + error.asDynamic().message);
})
val positionFeature = ol.Feature()
positionFeature.setStyle(ol.style.Style(object : olx.style.StyleOptions {
override var image: ol.style.Image? = ol.style.Icon(object : olx.style.IconOptions {
override var src: String? = "u.png";
override var scale: Number? = 0.5
})
}))
var centerDefined = false
geolocation.on("change:position", {
// should be nullable?
val coordinates = geolocation.getPosition()
j.ajax(object {
val url = "/user/" + jq("#select-user").`val`() + "/location/" + coordinates[0] + "," + coordinates[1]
val type = "PUT"
})
if (!centerDefined) {
view.setCenter(coordinates)
centerDefined = true
}
positionFeature.setGeometry(if (coordinates == null) ol.geom.Point(coordinates) else null)
})
ol.layer.Vector(object : olx.layer.VectorOptions {
/// ???
override var map = map
override var source: ol.source.Source? = ol.source.Vector(object : olx.source.VectorOptions {
override var features = arrayOf(positionFeature)
})
})
geolocation.setTracking(true)
}
private fun setupPopup(map: ol.Map) {
val element = document.getElementById("popup")
fun ol.Overlay.show(coordinate: ol.Coordinate, content: Any, additionalSetup: dynamic.() -> Unit = {}){
j(element).popover("destroy")
setPosition(coordinate)
j(element).popover(object {
val placement = "top"
val html = true
val content = content
val animation = false
}).additionalSetup()
j(element).popover("show")
}
val popup = ol.Overlay(object : olx.OverlayOptions {
override var element = element
override var positioning = "bottom-center" as ol.OverlayPositioning?
})
map.addOverlay(popup)
// display popup on click
map.on("click", { e ->
val coordinate = e.coordinate
val feature = map.forEachFeatureAtPixel(e.pixel, { feature, layer -> feature })
if (feature != null) {
popup.show(coordinate, feature.get("content"))
} else {
popup.show(coordinate, "New message") {
data("bs.popover").tip()
.width(300).height(300)
.append("<div id='message' style='height:70%'/>")
}
j("#message").editable({ value, settings ->
j.ajax(object {
val method = "POST"
val url = "/message"
val data = JSON.stringify(object {
val content = value
val author = jq("#select-user").`val`()
val location = object {
val type = "Point";
val coordinates = arrayOf(coordinate[0], coordinate[1])
}
})
val contentType = "application/json; charset=utf-8"
val dataType = "json"
})
popup.setPosition()
value
}, object {
val type = "textarea"
val submit = "OK"
})
}
})
}
private fun setupMessageLayer(map: ol.Map): ol.source.Vector {
var vectorSourceRef: ol.source.Vector? = null
val vectorSource = ol.source.Vector(object/* : olx.source.VectorOptions*/ {
/*override */var loader: ol.FeatureLoader? = { extent: ol.Extent, number: Number, projection: ol.proj.Projection ->
var url = "/message/bbox/" + extent[0] + "," + extent[1] + "," + extent[2] + "," + extent[3]
j.ajax(object {
val url = url
val dataType = "json"
val success = { response: dynamic ->
if (response.error) {
alert(response.error.message + '\n' + response.error.details.join('\n'))
} else {
j.each(response) { index: Int, value: dynamic ->
var feature = ol.Feature(object {
val geometry = ol.geom.Point(value.location.coordinates)
val content = value.content
val author = value.autho
})
vectorSourceRef!!.addFeature(feature)
}
}
}
})
} as ol.FeatureLoader
} as olx.source.VectorOptions)
vectorSourceRef = vectorSource
val vector = ol.layer.Vector(object/*: olx.layer.VectorOptions*/ {
/*override */var source: ol.source.Source? = vectorSource
/*override */var style = ol.style.Style(object : olx.style.StyleOptions {
override var image: ol.style.Image? = ol.style.Icon(object : olx.style.IconOptions {
override var src: String? = "m.png";
override var scale: Number? = 0.5
})
})
} as olx.layer.VectorOptions)
map.addLayer(vector)
return vectorSource
}
private fun setupMessagePush(vectorSource: ol.source.Vector) {
val source = EventSource("/message/subscribe")
//todo: report issue
source.addEventListener("message", {
val e = it as MessageEvent
val message = j.parseJSON(e.data)
val feature = ol.Feature(object {
val geometry = ol.geom.Point(message.location.coordinates)
val content = message.content
val author = message.author
})
vectorSource.addFeature(feature)
})
}
fun main(args: Array<String>) {
fillUsersSelect()
val (view, map) = createMap()
setupGeolocation(map, view)
setupPopup(map)
val vectorSource = setupMessageLayer(map)
setupMessagePush(vectorSource)
}