/
Map.js
1819 lines (1666 loc) · 56.1 KB
/
Map.js
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module ol/Map
*/
import BaseObject from './Object.js';
import Collection from './Collection.js';
import CollectionEventType from './CollectionEventType.js';
import CompositeMapRenderer from './renderer/Composite.js';
import EventType from './events/EventType.js';
import Layer from './layer/Layer.js';
import LayerGroup, {GroupEvent} from './layer/Group.js';
import MapBrowserEvent from './MapBrowserEvent.js';
import MapBrowserEventHandler from './MapBrowserEventHandler.js';
import MapBrowserEventType from './MapBrowserEventType.js';
import MapEvent from './MapEvent.js';
import MapEventType from './MapEventType.js';
import MapProperty from './MapProperty.js';
import ObjectEventType from './ObjectEventType.js';
import PointerEventType from './pointer/EventType.js';
import RenderEventType from './render/EventType.js';
import TileQueue, {getTilePriority} from './TileQueue.js';
import View from './View.js';
import ViewHint from './ViewHint.js';
import {DEVICE_PIXEL_RATIO, PASSIVE_EVENT_LISTENERS} from './has.js';
import {TRUE} from './functions.js';
import {
apply as applyTransform,
create as createTransform,
} from './transform.js';
import {assert} from './asserts.js';
import {
clone,
createOrUpdateEmpty,
equals,
getForViewAndSize,
isEmpty,
} from './extent.js';
import {defaults as defaultControls} from './control/defaults.js';
import {defaults as defaultInteractions} from './interaction/defaults.js';
import {fromUserCoordinate, toUserCoordinate} from './proj.js';
import {getUid} from './util.js';
import {hasArea} from './size.js';
import {listen, unlistenByKey} from './events.js';
import {removeNode} from './dom.js';
/**
* State of the current frame. Only `pixelRatio`, `time` and `viewState` should
* be used in applications.
* @typedef {Object} FrameState
* @property {number} pixelRatio The pixel ratio of the frame.
* @property {number} time The time when rendering of the frame was requested.
* @property {import("./View.js").State} viewState The state of the current view.
* @property {boolean} animate Animate.
* @property {import("./transform.js").Transform} coordinateToPixelTransform CoordinateToPixelTransform.
* @property {import("rbush").default} declutterTree DeclutterTree.
* @property {null|import("./extent.js").Extent} extent Extent.
* @property {import("./extent.js").Extent} [nextExtent] Next extent during an animation series.
* @property {number} index Index.
* @property {Array<import("./layer/Layer.js").State>} layerStatesArray LayerStatesArray.
* @property {number} layerIndex LayerIndex.
* @property {import("./transform.js").Transform} pixelToCoordinateTransform PixelToCoordinateTransform.
* @property {Array<PostRenderFunction>} postRenderFunctions PostRenderFunctions.
* @property {import("./size.js").Size} size Size.
* @property {TileQueue} tileQueue TileQueue.
* @property {!Object<string, Object<string, boolean>>} usedTiles UsedTiles.
* @property {Array<number>} viewHints ViewHints.
* @property {!Object<string, Object<string, boolean>>} wantedTiles WantedTiles.
* @property {string} mapId The id of the map.
* @property {Object<string, boolean>} renderTargets Identifiers of previously rendered elements.
*/
/**
* @typedef {function(Map, ?FrameState): any} PostRenderFunction
*/
/**
* @typedef {Object} AtPixelOptions
* @property {undefined|function(import("./layer/Layer.js").default<import("./source/Source").default>): boolean} [layerFilter] Layer filter
* function. The filter function will receive one argument, the
* {@link module:ol/layer/Layer~Layer layer-candidate} and it should return a boolean value.
* Only layers which are visible and for which this function returns `true`
* will be tested for features. By default, all visible layers will be tested.
* @property {number} [hitTolerance=0] Hit-detection tolerance in css pixels. Pixels
* inside the radius around the given position will be checked for features.
* @property {boolean} [checkWrapped=true] Check-Wrapped Will check for for wrapped geometries inside the range of
* +/- 1 world width. Works only if a projection is used that can be wrapped.
*/
/**
* @typedef {Object} MapOptionsInternal
* @property {Collection<import("./control/Control.js").default>} [controls] Controls.
* @property {Collection<import("./interaction/Interaction.js").default>} [interactions] Interactions.
* @property {HTMLElement|Document} keyboardEventTarget KeyboardEventTarget.
* @property {Collection<import("./Overlay.js").default>} overlays Overlays.
* @property {Object<string, *>} values Values.
*/
/**
* @typedef {import("./ObjectEventType").Types|'change:layergroup'|'change:size'|'change:target'|'change:view'} MapObjectEventTypes
*/
/***
* @template Return
* @typedef {import("./Observable").OnSignature<import("./Observable").EventTypes, import("./events/Event.js").default, Return> &
* import("./Observable").OnSignature<MapObjectEventTypes, import("./Object").ObjectEvent, Return> &
* import("./Observable").OnSignature<import("./MapBrowserEventType").Types, import("./MapBrowserEvent").default, Return> &
* import("./Observable").OnSignature<import("./MapEventType").Types, import("./MapEvent").default, Return> &
* import("./Observable").OnSignature<import("./render/EventType").MapRenderEventTypes, import("./render/Event").default, Return> &
* import("./Observable").CombinedOnSignature<import("./Observable").EventTypes|MapObjectEventTypes|
* import("./MapBrowserEventType").Types|import("./MapEventType").Types|
* import("./render/EventType").MapRenderEventTypes, Return>} MapEventHandler
*/
/**
* Object literal with config options for the map.
* @typedef {Object} MapOptions
* @property {Collection<import("./control/Control.js").default>|Array<import("./control/Control.js").default>} [controls]
* Controls initially added to the map. If not specified,
* {@link module:ol/control/defaults.defaults} is used.
* @property {number} [pixelRatio=window.devicePixelRatio] The ratio between
* physical pixels and device-independent pixels (dips) on the device.
* @property {Collection<import("./interaction/Interaction.js").default>|Array<import("./interaction/Interaction.js").default>} [interactions]
* Interactions that are initially added to the map. If not specified,
* {@link module:ol/interaction/defaults.defaults} is used.
* @property {HTMLElement|Document|string} [keyboardEventTarget] The element to
* listen to keyboard events on. This determines when the `KeyboardPan` and
* `KeyboardZoom` interactions trigger. 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
* map target (i.e. the user-provided div for the map). If this is not
* `document`, the target element needs to be focused for key events to be
* emitted, requiring that the target element has a `tabindex` attribute.
* @property {Array<import("./layer/Base.js").default>|Collection<import("./layer/Base.js").default>|LayerGroup} [layers]
* Layers. If this is not defined, a map with no layers will be rendered. Note
* that layers are rendered in the order supplied, so if you want, for example,
* a vector layer to appear on top of a tile layer, it must come after the tile
* layer.
* @property {number} [maxTilesLoading=16] Maximum number tiles to load
* simultaneously.
* @property {number} [moveTolerance=1] 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.
* @property {Collection<import("./Overlay.js").default>|Array<import("./Overlay.js").default>} [overlays]
* Overlays initially added to the map. By default, no overlays are added.
* @property {HTMLElement|string} [target] The container for the map, either the
* element itself or the `id` of the element. If not specified at construction
* time, {@link module:ol/Map~Map#setTarget} must be called for the map to be
* rendered. If passed by element, the container can be in a secondary document.
* @property {View|Promise<import("./View.js").ViewOptions>} [view] The map's view. No layer sources will be
* fetched unless this is specified at construction time or through
* {@link module:ol/Map~Map#setView}.
*/
/**
* @param {import("./layer/Base.js").default} layer Layer.
*/
function removeLayerMapProperty(layer) {
if (layer instanceof Layer) {
layer.setMapInternal(null);
return;
}
if (layer instanceof LayerGroup) {
layer.getLayers().forEach(removeLayerMapProperty);
}
}
/**
* @param {import("./layer/Base.js").default} layer Layer.
* @param {Map} map Map.
*/
function setLayerMapProperty(layer, map) {
if (layer instanceof Layer) {
layer.setMapInternal(map);
return;
}
if (layer instanceof LayerGroup) {
const layers = layer.getLayers().getArray();
for (let i = 0, ii = layers.length; i < ii; ++i) {
setLayerMapProperty(layers[i], map);
}
}
}
/**
* @classdesc
* The map is the core component of OpenLayers. For a map to render, a view,
* one or more layers, and a target container are needed:
*
* import Map from 'ol/Map';
* import View from 'ol/View';
* import TileLayer from 'ol/layer/Tile';
* import OSM from 'ol/source/OSM';
*
* const map = new Map({
* view: new View({
* center: [0, 0],
* zoom: 1,
* }),
* layers: [
* new TileLayer({
* source: new OSM(),
* }),
* ],
* target: 'map',
* });
*
* The above snippet creates a map using a {@link module:ol/layer/Tile~TileLayer} to
* display {@link module:ol/source/OSM~OSM} OSM data and render it to a DOM
* element with the id `map`.
*
* The constructor places a viewport container (with CSS class name
* `ol-viewport`) in the target element (see `getViewport()`), and then two
* further elements within the viewport: one with CSS class name
* `ol-overlaycontainer-stopevent` for controls and some overlays, and one with
* CSS class name `ol-overlaycontainer` for other overlays (see the `stopEvent`
* option of {@link module:ol/Overlay~Overlay} for the difference). The map
* itself is placed in a further element within the viewport.
*
* Layers are stored as a {@link module:ol/Collection~Collection} in
* layerGroups. A top-level group is provided by the library. This is what is
* accessed by `getLayerGroup` and `setLayerGroup`. Layers entered in the
* options are added to this group, and `addLayer` and `removeLayer` change the
* layer collection in the group. `getLayers` is a convenience function for
* `getLayerGroup().getLayers()`. Note that {@link module:ol/layer/Group~LayerGroup}
* is a subclass of {@link module:ol/layer/Base~BaseLayer}, so layers entered in the
* options or added with `addLayer` can be groups, which can contain further
* groups, and so on.
*
* @fires import("./MapBrowserEvent.js").MapBrowserEvent
* @fires import("./MapEvent.js").MapEvent
* @fires import("./render/Event.js").default#precompose
* @fires import("./render/Event.js").default#postcompose
* @fires import("./render/Event.js").default#rendercomplete
* @api
*/
class Map extends BaseObject {
/**
* @param {MapOptions} [options] Map options.
*/
constructor(options) {
super();
options = options || {};
/***
* @type {MapEventHandler<import("./events").EventsKey>}
*/
this.on;
/***
* @type {MapEventHandler<import("./events").EventsKey>}
*/
this.once;
/***
* @type {MapEventHandler<void>}
*/
this.un;
const optionsInternal = createOptionsInternal(options);
/**
* @private
* @type {boolean|undefined}
*/
this.renderComplete_;
/**
* @private
* @type {boolean}
*/
this.loaded_ = true;
/** @private */
this.boundHandleBrowserEvent_ = this.handleBrowserEvent.bind(this);
/**
* @type {number}
* @private
*/
this.maxTilesLoading_ =
options.maxTilesLoading !== undefined ? options.maxTilesLoading : 16;
/**
* @private
* @type {number}
*/
this.pixelRatio_ =
options.pixelRatio !== undefined
? options.pixelRatio
: DEVICE_PIXEL_RATIO;
/**
* @private
* @type {*}
*/
this.postRenderTimeoutHandle_;
/**
* @private
* @type {number|undefined}
*/
this.animationDelayKey_;
/**
* @private
*/
this.animationDelay_ = this.animationDelay_.bind(this);
/**
* @private
* @type {import("./transform.js").Transform}
*/
this.coordinateToPixelTransform_ = createTransform();
/**
* @private
* @type {import("./transform.js").Transform}
*/
this.pixelToCoordinateTransform_ = createTransform();
/**
* @private
* @type {number}
*/
this.frameIndex_ = 0;
/**
* @private
* @type {?FrameState}
*/
this.frameState_ = null;
/**
* The extent at the previous 'moveend' event.
* @private
* @type {import("./extent.js").Extent}
*/
this.previousExtent_ = null;
/**
* @private
* @type {?import("./events.js").EventsKey}
*/
this.viewPropertyListenerKey_ = null;
/**
* @private
* @type {?import("./events.js").EventsKey}
*/
this.viewChangeListenerKey_ = null;
/**
* @private
* @type {?Array<import("./events.js").EventsKey>}
*/
this.layerGroupPropertyListenerKeys_ = null;
/**
* @private
* @type {!HTMLElement}
*/
this.viewport_ = document.createElement('div');
this.viewport_.className =
'ol-viewport' + ('ontouchstart' in window ? ' ol-touch' : '');
this.viewport_.style.position = 'relative';
this.viewport_.style.overflow = 'hidden';
this.viewport_.style.width = '100%';
this.viewport_.style.height = '100%';
/**
* @private
* @type {!HTMLElement}
*/
this.overlayContainer_ = document.createElement('div');
this.overlayContainer_.style.position = 'absolute';
this.overlayContainer_.style.zIndex = '0';
this.overlayContainer_.style.width = '100%';
this.overlayContainer_.style.height = '100%';
this.overlayContainer_.style.pointerEvents = 'none';
this.overlayContainer_.className = 'ol-overlaycontainer';
this.viewport_.appendChild(this.overlayContainer_);
/**
* @private
* @type {!HTMLElement}
*/
this.overlayContainerStopEvent_ = document.createElement('div');
this.overlayContainerStopEvent_.style.position = 'absolute';
this.overlayContainerStopEvent_.style.zIndex = '0';
this.overlayContainerStopEvent_.style.width = '100%';
this.overlayContainerStopEvent_.style.height = '100%';
this.overlayContainerStopEvent_.style.pointerEvents = 'none';
this.overlayContainerStopEvent_.className = 'ol-overlaycontainer-stopevent';
this.viewport_.appendChild(this.overlayContainerStopEvent_);
/**
* @private
* @type {MapBrowserEventHandler}
*/
this.mapBrowserEventHandler_ = null;
/**
* @private
* @type {number}
*/
this.moveTolerance_ = options.moveTolerance;
/**
* @private
* @type {HTMLElement|Document}
*/
this.keyboardEventTarget_ = optionsInternal.keyboardEventTarget;
/**
* @private
* @type {?Array<import("./events.js").EventsKey>}
*/
this.targetChangeHandlerKeys_ = null;
/**
* @type {Collection<import("./control/Control.js").default>}
* @protected
*/
this.controls = optionsInternal.controls || defaultControls();
/**
* @type {Collection<import("./interaction/Interaction.js").default>}
* @protected
*/
this.interactions =
optionsInternal.interactions ||
defaultInteractions({
onFocusOnly: true,
});
/**
* @type {Collection<import("./Overlay.js").default>}
* @private
*/
this.overlays_ = optionsInternal.overlays;
/**
* A lookup of overlays by id.
* @private
* @type {Object<string, import("./Overlay.js").default>}
*/
this.overlayIdIndex_ = {};
/**
* @type {import("./renderer/Map.js").default|null}
* @private
*/
this.renderer_ = null;
/**
* @private
* @type {!Array<PostRenderFunction>}
*/
this.postRenderFunctions_ = [];
/**
* @private
* @type {TileQueue}
*/
this.tileQueue_ = new TileQueue(
this.getTilePriority.bind(this),
this.handleTileChange_.bind(this)
);
this.addChangeListener(
MapProperty.LAYERGROUP,
this.handleLayerGroupChanged_
);
this.addChangeListener(MapProperty.VIEW, this.handleViewChanged_);
this.addChangeListener(MapProperty.SIZE, this.handleSizeChanged_);
this.addChangeListener(MapProperty.TARGET, this.handleTargetChanged_);
// setProperties will trigger the rendering of the map if the map
// is "defined" already.
this.setProperties(optionsInternal.values);
const map = this;
if (options.view && !(options.view instanceof View)) {
options.view.then(function (viewOptions) {
map.setView(new View(viewOptions));
});
}
this.controls.addEventListener(
CollectionEventType.ADD,
/**
* @param {import("./Collection.js").CollectionEvent<import("./control/Control.js").default>} event CollectionEvent
*/
function (event) {
event.element.setMap(this);
}.bind(this)
);
this.controls.addEventListener(
CollectionEventType.REMOVE,
/**
* @param {import("./Collection.js").CollectionEvent<import("./control/Control.js").default>} event CollectionEvent.
*/
function (event) {
event.element.setMap(null);
}.bind(this)
);
this.interactions.addEventListener(
CollectionEventType.ADD,
/**
* @param {import("./Collection.js").CollectionEvent<import("./interaction/Interaction.js").default>} event CollectionEvent.
*/
function (event) {
event.element.setMap(this);
}.bind(this)
);
this.interactions.addEventListener(
CollectionEventType.REMOVE,
/**
* @param {import("./Collection.js").CollectionEvent<import("./interaction/Interaction.js").default>} event CollectionEvent.
*/
function (event) {
event.element.setMap(null);
}.bind(this)
);
this.overlays_.addEventListener(
CollectionEventType.ADD,
/**
* @param {import("./Collection.js").CollectionEvent<import("./Overlay.js").default>} event CollectionEvent.
*/
function (event) {
this.addOverlayInternal_(event.element);
}.bind(this)
);
this.overlays_.addEventListener(
CollectionEventType.REMOVE,
/**
* @param {import("./Collection.js").CollectionEvent<import("./Overlay.js").default>} event CollectionEvent.
*/
function (event) {
const id = event.element.getId();
if (id !== undefined) {
delete this.overlayIdIndex_[id.toString()];
}
event.element.setMap(null);
}.bind(this)
);
this.controls.forEach(
/**
* @param {import("./control/Control.js").default} control Control.
* @this {Map}
*/
function (control) {
control.setMap(this);
}.bind(this)
);
this.interactions.forEach(
/**
* @param {import("./interaction/Interaction.js").default} interaction Interaction.
* @this {Map}
*/
function (interaction) {
interaction.setMap(this);
}.bind(this)
);
this.overlays_.forEach(this.addOverlayInternal_.bind(this));
}
/**
* Add the given control to the map.
* @param {import("./control/Control.js").default} control Control.
* @api
*/
addControl(control) {
this.getControls().push(control);
}
/**
* Add the given interaction to the map. If you want to add an interaction
* at another point of the collection use `getInteractions()` and the methods
* available on {@link module:ol/Collection~Collection}. This can be used to
* stop the event propagation from the handleEvent function. The interactions
* get to handle the events in the reverse order of this collection.
* @param {import("./interaction/Interaction.js").default} interaction Interaction to add.
* @api
*/
addInteraction(interaction) {
this.getInteractions().push(interaction);
}
/**
* Adds the given layer to the top of this map. If you want to add a layer
* elsewhere in the stack, use `getLayers()` and the methods available on
* {@link module:ol/Collection~Collection}.
* @param {import("./layer/Base.js").default} layer Layer.
* @api
*/
addLayer(layer) {
const layers = this.getLayerGroup().getLayers();
layers.push(layer);
}
/**
* @param {import("./layer/Group.js").GroupEvent} event The layer add event.
* @private
*/
handleLayerAdd_(event) {
setLayerMapProperty(event.layer, this);
}
/**
* Add the given overlay to the map.
* @param {import("./Overlay.js").default} overlay Overlay.
* @api
*/
addOverlay(overlay) {
this.getOverlays().push(overlay);
}
/**
* This deals with map's overlay collection changes.
* @param {import("./Overlay.js").default} overlay Overlay.
* @private
*/
addOverlayInternal_(overlay) {
const id = overlay.getId();
if (id !== undefined) {
this.overlayIdIndex_[id.toString()] = overlay;
}
overlay.setMap(this);
}
/**
*
* Clean up.
*/
disposeInternal() {
this.controls.clear();
this.interactions.clear();
this.overlays_.clear();
this.setTarget(null);
super.disposeInternal();
}
/**
* Detect features that intersect a pixel on the viewport, and execute a
* callback with each intersecting feature. Layers included in the detection can
* be configured through the `layerFilter` option in `options`.
* @param {import("./pixel.js").Pixel} pixel Pixel.
* @param {function(import("./Feature.js").FeatureLike, import("./layer/Layer.js").default<import("./source/Source").default>, import("./geom/SimpleGeometry.js").default): T} callback Feature callback. The callback will be
* called with two arguments. The first argument is one
* {@link module:ol/Feature~Feature feature} or
* {@link module:ol/render/Feature~RenderFeature render feature} at the pixel, the second is
* the {@link module:ol/layer/Layer~Layer layer} of the feature and will be null for
* unmanaged layers. To stop detection, callback functions can return a
* truthy value.
* @param {AtPixelOptions} [options] Optional options.
* @return {T|undefined} Callback result, i.e. the return value of last
* callback execution, or the first truthy callback return value.
* @template T
* @api
*/
forEachFeatureAtPixel(pixel, callback, options) {
if (!this.frameState_ || !this.renderer_) {
return;
}
const coordinate = this.getCoordinateFromPixelInternal(pixel);
options = options !== undefined ? options : {};
const hitTolerance =
options.hitTolerance !== undefined ? options.hitTolerance : 0;
const layerFilter =
options.layerFilter !== undefined ? options.layerFilter : TRUE;
const checkWrapped = options.checkWrapped !== false;
return this.renderer_.forEachFeatureAtCoordinate(
coordinate,
this.frameState_,
hitTolerance,
checkWrapped,
callback,
null,
layerFilter,
null
);
}
/**
* Get all features that intersect a pixel on the viewport.
* @param {import("./pixel.js").Pixel} pixel Pixel.
* @param {AtPixelOptions} [options] Optional options.
* @return {Array<import("./Feature.js").FeatureLike>} The detected features or
* an empty array if none were found.
* @api
*/
getFeaturesAtPixel(pixel, options) {
const features = [];
this.forEachFeatureAtPixel(
pixel,
function (feature) {
features.push(feature);
},
options
);
return features;
}
/**
* Get all layers from all layer groups.
* @return {Array<import("./layer/Layer.js").default>} Layers.
* @api
*/
getAllLayers() {
const layers = [];
function addLayersFrom(layerGroup) {
layerGroup.forEach(function (layer) {
if (layer instanceof LayerGroup) {
addLayersFrom(layer.getLayers());
} else {
layers.push(layer);
}
});
}
addLayersFrom(this.getLayers());
return layers;
}
/**
* Detect if features intersect a pixel on the viewport. Layers included in the
* detection can be configured through the `layerFilter` option.
* @param {import("./pixel.js").Pixel} pixel Pixel.
* @param {AtPixelOptions} [options] Optional options.
* @return {boolean} Is there a feature at the given pixel?
* @api
*/
hasFeatureAtPixel(pixel, options) {
if (!this.frameState_ || !this.renderer_) {
return false;
}
const coordinate = this.getCoordinateFromPixelInternal(pixel);
options = options !== undefined ? options : {};
const layerFilter =
options.layerFilter !== undefined ? options.layerFilter : TRUE;
const hitTolerance =
options.hitTolerance !== undefined ? options.hitTolerance : 0;
const checkWrapped = options.checkWrapped !== false;
return this.renderer_.hasFeatureAtCoordinate(
coordinate,
this.frameState_,
hitTolerance,
checkWrapped,
layerFilter,
null
);
}
/**
* Returns the coordinate in user projection for a browser event.
* @param {MouseEvent} event Event.
* @return {import("./coordinate.js").Coordinate} Coordinate.
* @api
*/
getEventCoordinate(event) {
return this.getCoordinateFromPixel(this.getEventPixel(event));
}
/**
* Returns the coordinate in view projection for a browser event.
* @param {MouseEvent} event Event.
* @return {import("./coordinate.js").Coordinate} Coordinate.
*/
getEventCoordinateInternal(event) {
return this.getCoordinateFromPixelInternal(this.getEventPixel(event));
}
/**
* Returns the map pixel position for a browser event relative to the viewport.
* @param {UIEvent} event Event.
* @return {import("./pixel.js").Pixel} Pixel.
* @api
*/
getEventPixel(event) {
const viewportPosition = this.viewport_.getBoundingClientRect();
const eventPosition =
//FIXME Are we really calling this with a TouchEvent anywhere?
'changedTouches' in event
? /** @type {TouchEvent} */ (event).changedTouches[0]
: /** @type {MouseEvent} */ (event);
return [
eventPosition.clientX - viewportPosition.left,
eventPosition.clientY - viewportPosition.top,
];
}
/**
* Get the target in which this map is rendered.
* Note that this returns what is entered as an option or in setTarget:
* if that was an element, it returns an element; if a string, it returns that.
* @return {HTMLElement|string|undefined} The Element or id of the Element that the
* map is rendered in.
* @observable
* @api
*/
getTarget() {
return /** @type {HTMLElement|string|undefined} */ (
this.get(MapProperty.TARGET)
);
}
/**
* Get the DOM element into which this map is rendered. In contrast to
* `getTarget` this method always return an `Element`, or `null` if the
* map has no target.
* @return {HTMLElement} The element that the map is rendered in.
* @api
*/
getTargetElement() {
const target = this.getTarget();
if (target !== undefined) {
return typeof target === 'string'
? document.getElementById(target)
: target;
} else {
return null;
}
}
/**
* Get the coordinate for a given pixel. This returns a coordinate in the
* user projection.
* @param {import("./pixel.js").Pixel} pixel Pixel position in the map viewport.
* @return {import("./coordinate.js").Coordinate} The coordinate for the pixel position.
* @api
*/
getCoordinateFromPixel(pixel) {
return toUserCoordinate(
this.getCoordinateFromPixelInternal(pixel),
this.getView().getProjection()
);
}
/**
* Get the coordinate for a given pixel. This returns a coordinate in the
* map view projection.
* @param {import("./pixel.js").Pixel} pixel Pixel position in the map viewport.
* @return {import("./coordinate.js").Coordinate} The coordinate for the pixel position.
*/
getCoordinateFromPixelInternal(pixel) {
const frameState = this.frameState_;
if (!frameState) {
return null;
} else {
return applyTransform(
frameState.pixelToCoordinateTransform,
pixel.slice()
);
}
}
/**
* Get the map controls. Modifying this collection changes the controls
* associated with the map.
* @return {Collection<import("./control/Control.js").default>} Controls.
* @api
*/
getControls() {
return this.controls;
}
/**
* Get the map overlays. Modifying this collection changes the overlays
* associated with the map.
* @return {Collection<import("./Overlay.js").default>} Overlays.
* @api
*/
getOverlays() {
return this.overlays_;
}
/**
* Get an overlay by its identifier (the value returned by overlay.getId()).
* Note that the index treats string and numeric identifiers as the same. So
* `map.getOverlayById(2)` will return an overlay with id `'2'` or `2`.
* @param {string|number} id Overlay identifier.
* @return {import("./Overlay.js").default} Overlay.
* @api
*/
getOverlayById(id) {
const overlay = this.overlayIdIndex_[id.toString()];
return overlay !== undefined ? overlay : null;
}
/**
* Get the map interactions. Modifying this collection changes the interactions
* associated with the map.
*
* Interactions are used for e.g. pan, zoom and rotate.
* @return {Collection<import("./interaction/Interaction.js").default>} Interactions.
* @api
*/
getInteractions() {
return this.interactions;
}
/**
* Get the layergroup associated with this map.
* @return {LayerGroup} A layer group containing the layers in this map.
* @observable
* @api
*/
getLayerGroup() {
return /** @type {LayerGroup} */ (this.get(MapProperty.LAYERGROUP));
}
/**
* Clear any existing layers and add layers to the map.
* @param {Array<import("./layer/Base.js").default>|Collection<import("./layer/Base.js").default>} layers The layers to be added to the map.
* @api
*/
setLayers(layers) {
const group = this.getLayerGroup();
if (layers instanceof Collection) {
group.setLayers(layers);
return;
}
const collection = group.getLayers();
collection.clear();
collection.extend(layers);
}
/**
* Get the collection of layers associated with this map.
* @return {!Collection<import("./layer/Base.js").default>} Layers.
* @api
*/
getLayers() {
const layers = this.getLayerGroup().getLayers();
return layers;
}
/**
* @return {boolean} Layers have sources that are still loading.
*/
getLoadingOrNotReady() {
const layerStatesArray = this.getLayerGroup().getLayerStatesArray();
for (let i = 0, ii = layerStatesArray.length; i < ii; ++i) {
const state = layerStatesArray[i];
if (!state.visible) {
continue;
}
const renderer = state.layer.getRenderer();
if (renderer && !renderer.ready) {
return true;
}
const source = state.layer.getSource();
if (source && source.loading) {
return true;
}
}
return false;
}
/**
* Get the pixel for a coordinate. This takes a coordinate in the user
* projection and returns the corresponding pixel.
* @param {import("./coordinate.js").Coordinate} coordinate A map coordinate.
* @return {import("./pixel.js").Pixel} A pixel position in the map viewport.
* @api
*/
getPixelFromCoordinate(coordinate) {
const viewCoordinate = fromUserCoordinate(
coordinate,
this.getView().getProjection()
);
return this.getPixelFromCoordinateInternal(viewCoordinate);
}
/**
* Get the pixel for a coordinate. This takes a coordinate in the map view
* projection and returns the corresponding pixel.
* @param {import("./coordinate.js").Coordinate} coordinate A map coordinate.
* @return {import("./pixel.js").Pixel} A pixel position in the map viewport.
*/
getPixelFromCoordinateInternal(coordinate) {
const frameState = this.frameState_;
if (!frameState) {
return null;
} else {
return applyTransform(
frameState.coordinateToPixelTransform,
coordinate.slice(0, 2)
);
}