-
Notifications
You must be signed in to change notification settings - Fork 229
/
map.vue
491 lines (463 loc) · 13 KB
/
map.vue
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<template>
<div :class="[$options.name]" :tabindex="tabindex">
<slot/>
</div>
</template>
<script>
import { defaults as defaultsControl } from 'ol/control'
import VectorLayer from 'ol/layer/Vector'
import Map from 'ol/Map'
import VectorSource from 'ol/source/Vector'
import View from 'ol/View'
import { merge as mergeObs } from 'rxjs/observable'
import { distinctUntilChanged, map as mapObs, throttleTime } from 'rxjs/operators'
import Vue from 'vue'
import featuresContainer from '../../mixin/features-container'
import interactionsContainer from '../../mixin/interactions-container'
import layersContainer from '../../mixin/layers-container'
import olCmp from '../../mixin/ol-cmp'
import overlaysContainer from '../../mixin/overlays-container'
import projTransforms from '../../mixin/proj-transforms'
import { IndexedCollectionAdapter, SourceCollectionAdapter } from '../../ol-ext/collection'
import { RENDERER_TYPE } from '../../ol-ext/consts'
import observableFromOlEvent from '../../rx-ext/from-ol-event'
import { hasMap, hasView } from '../../util/assert'
import { isEqual } from '../../util/minilo'
import mergeDescriptors from '../../util/multi-merge-descriptors'
/**
* @vueProps
*/
const props = /** @lends module:map/map# */{
/**
* Options for default controls added to the map by default. Set to `false` to disable all map controls. Object
* value is used to configure controls.
* @type {Object|boolean}
* @todo remove when vl-control-* components will be ready
*/
controls: {
type: [Object, Boolean],
default: true,
},
/**
* The element to listen to keyboard events on. For example, if this option is set to `document` the keyboard
* interactions will always trigger. If this option is not specified, the element the library listens to keyboard
* events on is the component root element.
* @type {string|Element|Document}
*/
keyboardEventTarget: [String, Element, Document],
/**
* When set to `true`, tiles will be loaded during animations.
* @type {boolean}
*/
loadTilesWhileAnimating: {
type: Boolean,
default: false,
},
/**
* When set to `true`, tiles will be loaded while interacting with the map.
* @type {boolean}
*/
loadTilesWhileInteracting: {
type: Boolean,
default: false,
},
/**
* The map logo. If a **string** is provided, it will be set as the image source of the logo. If an **object** is provided,
* the `src` property should be the **URL** for an image and the `href` property should be a **URL** for creating a link.
* If an **element** is provided, the **element** will be used. To disable the map logo, set the option to `false`.
* By default, the **OpenLayers** logo is shown.
* @type {boolean|string|Object|Element}
*/
logo: [String, Object, Element, Boolean],
/**
* The minimum distance in pixels the cursor must move to be detected as a map move event instead of a click.
* Increasing this value can make it easier to click on the map.
* @type {Number}
*/
moveTolerance: {
type: Number,
default: 1,
},
/**
* The ratio between physical pixels and device-independent pixels (dips) on the device.
* @type {number}
*/
pixelRatio: {
type: Number,
default: () => window.devicePixelRatio || 1,
},
/**
* Renderer. By default, **Canvas** and **WebGL** renderers are tested for support in that order,
* and the first supported used. **Note** that the **Canvas** renderer fully supports vector data,
* but **WebGL** can only render **Point** geometries.
* @type {string|string[]}
* @default ['canvas', 'webgl']
*/
renderer: {
type: [String, Array],
default: () => [RENDERER_TYPE.CANVAS, RENDERER_TYPE.WEBGL],
},
/**
* Root element `tabindex` attribute value. Value should be provided to allow keyboard events on map.
* @type {number|string}
*/
tabindex: [String, Number],
/**
* Projection for input/output coordinates in plain data.
* @type {string}
*/
dataProjection: String,
wrapX: {
type: Boolean,
default: true,
},
}
/**
* @vueComputed
*/
const computed = {}
/**
* @vueMethods
*/
const methods = /** @lends module:map/map# */{
/**
* @return {Map}
* @protected
*/
createOlObject () {
const map = new Map({
controls: [],
loadTilesWhileAnimating: this.loadTilesWhileAnimating,
loadTilesWhileInteracting: this.loadTilesWhileInteracting,
pixelRatio: this.pixelRatio,
renderer: this.renderer,
logo: this.logo,
keyboardEventTarget: this.keyboardEventTarget,
view: this._view,
})
// ol.Map constructor can create default view if no provided with options
this._view = map.getView()
// add default overlay to map
this._defaultLayer.setMap(map)
if (this.controls) {
let opts = typeof this.controls === 'object' ? this.controls : undefined
map.getControls().extend(defaultsControl(opts).getArray())
}
map.set('dataProjection', this.dataProjection)
return map
},
/**
* @return {IndexedCollectionAdapter}
* @protected
*/
getLayersTarget () {
hasMap(this)
if (this._layersTarget == null) {
this._layersTarget = new IndexedCollectionAdapter(
this.$map.getLayers(),
layer => layer.get('id'),
)
}
return this._layersTarget
},
/**
* @return {IndexedCollectionAdapter}
* @protected
*/
getInteractionsTarget () {
hasMap(this)
if (this._interactionsTarget == null) {
this._interactionsTarget = new IndexedCollectionAdapter(
this.$map.getInteractions(),
interaction => interaction.get('id'),
)
}
return this._interactionsTarget
},
/**
* @return {function}
* @protected
*/
getDefaultInteractionsSorter () {
// sort interactions by priority in asc order
// the higher the priority, the earlier the interaction handles the event
return (a, b) => {
let ap = a.get('priority') || 0
let bp = b.get('priority') || 0
return ap === bp ? 0 : ap - bp
}
},
/**
* @return {SourceCollectionAdapter}
* @protected
*/
getFeaturesTarget () {
if (this._featuresTarget == null) {
this._featuresTarget = new SourceCollectionAdapter(this._defaultLayer.getSource())
}
return this._featuresTarget
},
/**
* @return {IndexedCollectionAdapter}
* @protected
*/
getOverlaysTarget () {
hasMap(this)
if (this._overlaysTarget == null) {
this._overlaysTarget = new IndexedCollectionAdapter(
this.$map.getOverlays(),
overlay => overlay.getId(),
)
}
return this._overlaysTarget
},
/**
* @param {number[]} pixel
* @return {number[]} Coordinates in the map view projection.
*/
getCoordinateFromPixel (pixel) {
hasMap(this)
let coordinate = this.$map.getCoordinateFromPixel(pixel)
return this.pointToDataProj(coordinate)
},
/**
* @returns {Object}
* @protected
*/
getServices () {
const vm = this
return mergeDescriptors(
this::olCmp.methods.getServices(),
this::layersContainer.methods.getServices(),
this::interactionsContainer.methods.getServices(),
this::overlaysContainer.methods.getServices(),
this::featuresContainer.methods.getServices(),
{
get map () { return vm.$map },
get view () { return vm.$view },
get viewContainer () { return vm },
},
)
},
/**
* Triggers focus on map container.
* @return {void}
*/
focus () {
this.$el.focus()
},
/**
* @param {number[]} pixel
* @param {function((Feature), ?Layer): *} callback
* @param {Object} [opts]
* @return {*|undefined}
*/
forEachFeatureAtPixel (pixel, callback, opts = {}) {
hasMap(this)
return this.$map.forEachFeatureAtPixel(pixel, callback, opts)
},
/**
* @param {number[]} pixel
* @param {function(Layer, ?(number[]|Uint8Array)): *} callback
* @param {function(Layer): boolean} [layerFilter]
* @return {*|undefined}
*/
forEachLayerAtPixel (pixel, callback, layerFilter) {
hasMap(this)
return this.$map.forEachLayerAtPixel(pixel, callback, undefined, layerFilter)
},
/**
* @param {View|Vue|undefined} view
* @return {void}
* @protected
*/
setView (view) {
view = view instanceof Vue ? view.$view : view
view || (view = new View())
if (view !== this._view) {
this._view = view
}
if (this.$map && view !== this.$map.getView()) {
this.$map.setView(view)
}
},
/**
* @return {void}
* @protected
*/
mount () {
hasMap(this)
this.$map.setTarget(this.$el)
this.subscribeAll()
this.updateSize()
},
/**
* @return {void}
* @protected
*/
unmount () {
hasMap(this)
this.clearLayers()
this.clearInteractions()
this.clearOverlays()
this.unsubscribeAll()
this.$map.setTarget(undefined)
},
/**
* Updates map size and re-renders map.
* @return {Promise}
*/
refresh () {
this.updateSize()
return this.render()
.then(() => this::olCmp.methods.refresh())
},
/**
* @return {Promise}
*/
render () {
return new Promise(resolve => {
hasMap(this)
this.$map.once('postrender', () => resolve())
this.$map.render()
})
},
/**
* @return {void}
* @protected
*/
subscribeAll () {
this::subscribeToMapEvents()
},
/**
* Updates map size.
* @return {void}
*/
updateSize () {
hasMap(this)
this.$map.updateSize()
},
}
const watch = {
dataProjection (value) {
if (this.$map) {
this.$map.set('dataProjection', value)
this.scheduleRefresh()
}
},
}
/**
* Container for **layers**, **interactions**, **controls** and **overlays**. It responsible for viewport
* rendering and low level interaction events.
*
* @title vl-map
* @alias module:map/map
* @vueProto
*
* @fires module:map/map#click
* @fires module:map/map#dblclick
* @fires module:map/map#singleclick
* @fires module:map/map#pointerdrag
* @fires module:map/map#pointermove
* @fires module:map/map#movestart
* @fires module:map/map#moveend
* @fires module:map/map#postrender
* @fires module:map/map#precompose
* @fires module:map/map#postcompose
*
* @vueSlot default Default slot for all child components.
*/
export default {
name: 'vl-map',
mixins: [olCmp, layersContainer, interactionsContainer, overlaysContainer, featuresContainer, projTransforms],
props,
computed,
methods,
watch,
created () {
/**
* @type {View|undefined}
* @private
*/
this._view = undefined
/**
* @type {Vector}
* @private
*/
this._defaultLayer = new VectorLayer({
source: new VectorSource({
wrapX: this.wrapX,
}),
})
Object.defineProperties(this, /** @lends module:map/map# */{
/**
* OpenLayers map instance.
* @type {Map|undefined}
*/
$map: {
enumerable: true,
get: () => this.$olObject,
},
/**
* OpenLayers view instance.
* @type {View|undefined}
*/
$view: {
enumerable: true,
get: () => this._view,
},
})
},
}
/**
* Subscribe to OL map events.
*
* @return {void}
* @private
*/
function subscribeToMapEvents () {
hasMap(this)
hasView(this)
const ft = 100
// pointer
const pointerEvents = mergeObs(
observableFromOlEvent(this.$map, [
'click',
'dblclick',
'singleclick',
]),
observableFromOlEvent(this.$map, [
'pointerdrag',
'pointermove',
]).pipe(
throttleTime(ft),
distinctUntilChanged((a, b) => isEqual(a.coordinate, b.coordinate)),
),
).pipe(
mapObs(evt => ({
...evt,
coordinate: this.pointToDataProj(evt.coordinate),
})),
)
// other
const otherEvents = observableFromOlEvent(this.$map, [
'movestart',
'moveend',
'postrender',
'precompose',
'postcompose',
])
const events = mergeObs(pointerEvents, otherEvents)
this.subscribeTo(events, evt => this.$emit(evt.type, evt))
}
/**
* A click with no dragging. A double click will fire two of this.
* @event module:map/map#click
* @type {MapBrowserEvent}
*/
/**
* A true double click, with no dragging.
* @event module:map/map#dblclick
* @type {MapBrowserEvent}
*/
</script>