forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVideoMap.js
More file actions
500 lines (480 loc) · 13.3 KB
/
VideoMap.js
File metadata and controls
500 lines (480 loc) · 13.3 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
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
import mapboxgl from 'mapbox-gl';
import '../core/Base';
import Coordtransfer from './coordTransfer';
import GeojsonLayer from './layers/GeojsonLayer';
import VideoMarker from './VideoMarker';
import GeojsonSource from './GeojsonSource';
import VideoMapPopup from './VideoMapPopup';
import VideoLayer from './layers/VideoLayer';
import { coordEach } from '@turf/meta';
import { transformCoordReverse } from './util';
import { SuperMap } from '@supermap/iclient-common';
const LAYER_MAP = {
geojson: GeojsonLayer,
vidoe: VideoLayer
};
const MAP_EVENTS = [
'resize',
'webglcontextlost',
'webglcontextrestored',
'remove',
'movestart',
'load',
'contextmenu',
'dblclick',
'click',
'touchcancel',
'touchmove',
'touchend',
'touchstart',
'dataloading',
'mousemove',
'mouseup',
'mousedown',
'sourcedataloading',
'error',
'data',
'styledata',
'sourcedata',
'mouseout',
'styledataloading',
'moveend',
'move',
'render',
'zoom',
'zoomstart',
'zoomend',
'boxzoomstart',
'boxzoomcancel',
'boxzoomend',
'rotate',
'rotatestart',
'rotateend',
'dragend',
'drag',
'dragstart',
'pitch',
'idle'
];
const MAP_DRAW_EVENTS = [
'draw.create',
'draw.delete',
'draw.combine',
'draw.uncombine',
'draw.update',
'draw.selectionchange',
'draw.modechange',
'draw.actionable'
];
/**
* @class SuperMap.VideoMap
* @classdesc 视频地图
* @param {Object} options - 参数
* @param {string} [options.container='map'] - 地图容器id
* @param {string} [options.src] - 视频地址
* @param {string} [options.videoParameters] - 视频配准参数
* @param {function} [options.videoWidth] - 视频宽度
* @param {function} [options.videoHeight] - 视频高度
* @param {string} [options.autoplay] - 视频是否自动播放
* @param {string} [options.loop] - 视频是否循环播放
* @extends {mapboxgl.Evented}
*/
export class VideoMap extends mapboxgl.Evented {
constructor(options) {
super();
const { container, src, videoParameters, autoplay, loop, videoWidth, videoHeight } = options;
this.container = container || 'map';
this.cacheVideoParameters = {};
this.layerCache = {};
this.sourceCache = {};
this.videoWidth = videoWidth;
this.videoHeight = videoHeight;
this.autoplay = autoplay;
this.loop = loop;
let promiseList = [];
promiseList.push(this._createMap());
if (videoParameters) {
promiseList.push(this._initVideoParameters(videoParameters));
}
Promise.all(promiseList).then((res) => {
if (res.length === 2) {
this.coordTransfer = res[1];
}
this.map = res[0];
if (src) {
this.addVideoLayer(src);
}
this._bindMapEventFn = this._bindMapEvent.bind(this);
MAP_EVENTS.forEach((eventName) => {
this.map.on(eventName, this._bindMapEventFn);
});
this.timerId = null;
});
}
addVideoLayer(src) {
this.videoLayer = new VideoLayer(this);
this.videoLayer.add(src);
this._bindEvents();
}
_initVideoParameters(parameters) {
if (parameters && !Object.keys(parameters).length) {
return;
}
this.cacheVideoParameters = parameters;
return new Coordtransfer(parameters).init();
}
/**
* @function SuperMap.VideoMap.prototype.setVideoParameters
* @description 设置视频配准参数。
* @param {Object} parameters - 视频配准参数。
*/
setVideoParameters(parameters) {
if (parameters && !Object.keys(parameters).length) {
return;
}
if (this.coordTransfer) {
this.coordTransfer.setCameraLocation(parameters);
this._updateDatas();
this.cacheVideoParameters = parameters;
} else {
this._initVideoParameters(parameters).then((coordTransfer) => {
this.coordTransfer = coordTransfer;
});
}
}
/**
* @function SuperMap.VideoMap.prototype.setVideoXYZ
* @description 设置视频配准 x y z 参数。
* @param {Object} pos - 视频配准 x y z 参数。
*/
setVideoXYZ(pos) {
const newVideoParameters = { ...this.cacheVideoParameters, ...pos };
this.setVideoParameters(newVideoParameters);
this.cacheVideoParameters = newVideoParameters;
}
/**
* @function SuperMap.VideoMap.prototype.setVideoPRY
* @description 设置视频配准 pitch roll yaw 参数。
* @param {Object} pos - 视频配准 pitch roll yaw 参数。
*/
setVideoPRY(parameters) {
const newVideoParameters = { ...this.cacheVideoParameters, ...parameters };
this.setVideoParameters(newVideoParameters);
this.cacheVideoParameters = newVideoParameters;
}
/**
* @function SuperMap.VideoMap.prototype.setVideoFov
* @description 设置视频配准 fx fy 参数。
* @param {Object} parameter - 视频配准 fx fy 参数。
*/
setVideoFov(fov) {
const newVideoParameters = { ...this.cacheVideoParameters, ...fov };
this.setVideoParameters(newVideoParameters);
this.cacheVideoParameters = newVideoParameters;
}
/**
* @function SuperMap.VideoMap.prototype.addControl
* @description 视频地图添加控件。
* @param {Object} control - 控件实例。
* @param {string} [position='top-right'] - 控制被添加到地图的位置。
*/
addControl(control, position) {
if (!this._mapExisted()) {
return;
}
this.map.addControl(control.instance, position);
if (control.controlName === 'draw') {
MAP_DRAW_EVENTS.forEach((eventName) => {
this.map.on(eventName, (e) => {
if (e && e.type === 'draw.create') {
coordEach(e.features[0], (coord) => {
let spatialPoint = this.coordTransfer.toSpatialCoordinate(
transformCoordReverse({
coord,
originCoordsRightBottom: this.originCoordsRightBottom,
originCoordsLeftTop: this.originCoordsLeftTop,
videoHeight: this.videoHeight,
videoWidth: this.videoWidth
})
);
coord[0] = spatialPoint[0];
coord[1] = spatialPoint[1];
});
}
this.fire(eventName, { e });
});
});
}
}
/**
* @function SuperMap.VideoMap.prototype.removeControl
* @description 视频地图移除控件。
* @param {Object} control - 控件实例。
*/
removeControl(control) {
if (!this._mapExisted()) {
return;
}
this.map.removeControl(control.instance);
}
_createMap() {
return new Promise((resolve) => {
const container =
typeof this.container === 'string' ? window.document.getElementById(this.container) : this.container;
if (!container) {
throw new Error(`Container '${container}' not found.`);
}
this.width = container.clientWidth || 400;
this.height = container.clientHeight || 300;
let map = new mapboxgl.Map({
container: this.container,
style: {
version: 8,
sources: {},
layers: []
},
renderWorldCopies: false,
center: [0, 0],
zoom: 8
});
map.on('load', () => {
resolve(map);
});
});
}
/**
* @function SuperMap.VideoMap.prototype.addMarker
* @description 添加 Marker。
* @param {Object} options
*/
addMarker(options) {
if (!this._mapExisted()) {
return;
}
return new VideoMarker(this, options);
}
/**
* @function SuperMap.VideoMap.prototype.addPopup
* @description 添加 Popup。
* @param {Object} options
*/
addPopup(options) {
if (!this._mapExisted()) {
return;
}
return new VideoMapPopup(this, options);
}
/**
* @function SuperMap.VideoMap.prototype.addLayer
* @description 添加图层。
* @param {Object} layer - 图层配置。
* @param {string} [type='geojson'] - 图层配置。
* @param {Object} beforeId - 已经存在的图层 ID。
*/
addLayer(layer, type = 'geojson', beforeId) {
if (!this._mapExisted()) {
return;
}
if (this.layerCache[layer.id]) {
return;
}
const currentLayer = new LAYER_MAP[type](this);
currentLayer.add(layer, beforeId);
this.layerCache[layer.id] = currentLayer;
}
/**
* @function SuperMap.VideoMap.prototype.addSource
* @description 添加数据源。
* @param {string} id - source id。
* @param {Object} source - 图层配置。
*/
addSource(id, source) {
if (!this._mapExisted()) {
return;
}
if (this.sourceCache[id]) {
return;
}
const geojsonSource = new GeojsonSource(this);
geojsonSource.add(id, source);
this.layerCache[id] = geojsonSource;
}
/**
* @function SuperMap.VideoMap.prototype.addSource
* @description 获取数据源。
* @param {string} id - source id。
*/
getSource(id) {
if (!this._mapExisted()) {
return;
}
return this.map.getSource(id);
}
/**
* @function SuperMap.VideoMap.prototype.getStyle
* @description 获取视频地图样式配置。
* @returns {Object} source - 图层配置。
*/
getStyle() {
if (!this._mapExisted()) {
return;
}
return this.map.getStyle();
}
/**
* @function SuperMap.VideoMap.prototype.removeLayer
* @description 移除图层。
* @param {string} id - 图层 id。
*/
removeLayer(id) {
if (!this._mapExisted()) {
return;
}
if (this.layerCache[id]) {
this.layerCache[id].remove();
this.layerCache[id] = null;
delete this.layerCache[id];
}
}
/**
* @function SuperMap.VideoMap.prototype.removeSource
* @description 移除数据源。
* @param {string} id - source id。
*/
removeSource(id) {
if (!this._mapExisted()) {
return;
}
if (this.sourceCache[id]) {
this.sourceCache[id].remove();
this.sourceCache[id] = null;
delete this.sourceCache[id];
}
}
/**
* @function SuperMap.VideoMap.prototype.play
* @description 播放视频。
*/
play() {
this.videoLayer.play();
}
/**
* @function SuperMap.VideoMap.prototype.pause
* @description 暂停视频。
*/
pause() {
this.videoLayer.pause();
}
/**
* @function SuperMap.VideoMap.prototype.queryRenderedFeatures
* @description 查询渲染的要素。
* @param {Array} geometry - 图层 ID。
* @param {Object} options - 配置。
*/
queryRenderedFeatures(pos, options) {
if (!this._mapExisted()) {
return;
}
return this.map.queryRenderedFeatures(pos, options);
}
/**
* @function SuperMap.VideoMap.prototype.getPaintProperty
* @description 获取绘制属性。
* @param {string} layerId - 图层 ID。
* @param {string} propertyName - 属性名。
*/
getPaintProperty(layerId, propertyName) {
if (!this._mapExisted()) {
return;
}
return this.map.getPaintProperty(layerId, propertyName);
}
getLayoutProperty(layerId, propertyName) {
if (!this._mapExisted()) {
return;
}
return this.map.getLayoutProperty(layerId, propertyName);
}
setPaintProperty(layerId, propertyName, propertyValue) {
if (!this._mapExisted()) {
return;
}
this.map.setPaintProperty(layerId, propertyName, propertyValue);
}
setLayoutProperty(layerId, propertyName, propertyValue) {
if (!this._mapExisted()) {
return;
}
return this.map.setLayoutProperty(layerId, propertyName, propertyValue);
}
/**
* @function SuperMap.VideoMap.prototype.setFilter
* @description 设置过滤。
* @param {string} layerId - 图层 ID。
* @param {Array} filter - 过滤配置。
*/
setFilter(layerId, filter) {
if (!this._mapExisted()) {
return;
}
return this.map.setFilter(layerId, filter);
}
/**
* @function SuperMap.VideoMap.prototype.destroy
* @description 销毁视频地图。
*/
destroy() {
this.timerId = null;
this.layerCache = {};
this.sourceCache = {};
this.cacheVideoParameters = {};
if (this.videoLayer) {
this.videoLayer.remove();
this.videoLayer = null;
}
this.map.remove();
this.map = null;
}
_updateDatas() {
this.map.fire('videoparameterchange');
}
_mapExisted() {
return !!this.map;
}
_bindEvents() {
this.videoLayer.on('videolayeradded', ({ originCoordsLeftTop, originCoordsRightBottom }) => {
this.originCoordsLeftTop = originCoordsLeftTop;
this.originCoordsRightBottom = originCoordsRightBottom;
this.fire('load', { map: this.map });
});
this.videoLayer.on('timeupdate', ({ time }) => {
this.fire('timeupdate', { time });
});
}
_clearEvents() {
MAP_EVENTS.forEach((eventName) => {
this.map.off(eventName, this._bindMapEventFn);
});
}
_bindMapEvent(e) {
if (e.lngLat) {
if (this.originCoordsRightBottom && this.originCoordsLeftTop && this.videoWidth && this.videoHeight) {
let coord = [e.lngLat.lng, e.lngLat.lat];
if (this.coordTransfer) {
let spatialPoint = this.coordTransfer.toSpatialCoordinate(
transformCoordReverse({
coord,
originCoordsRightBottom: this.originCoordsRightBottom,
originCoordsLeftTop: this.originCoordsLeftTop,
videoHeight: this.videoHeight,
videoWidth: this.videoWidth
})
);
e.spatialPoint = [spatialPoint[0], spatialPoint[1]];
}
}
}
this.fire(e.type, { mapEvent: e });
}
}
SuperMap.VideoMap = VideoMap;