Skip to content

Commit fb317da

Browse files
committed
【杨琨】【SDK】【合并行业标绘代码】
1 parent 4765ef6 commit fb317da

File tree

229 files changed

+32976
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

229 files changed

+32976
-6
lines changed

src/mapboxgl/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ export {
3636
import {
3737
EchartsLayer,
3838
MapvLayer,
39-
DeckglLayer
39+
DeckglLayer,
40+
FabricLayer
4041
/* StreamLayer */
4142
} from './overlay/index.js';
4243

4344
export {
4445
EchartsLayer,
4546
MapvLayer,
46-
DeckglLayer
47+
DeckglLayer,
48+
FabricLayer
4749
/* StreamLayer */
4850
};
Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,302 @@
1+
/*
2+
* @class: Module:FabricLayer
3+
* @Description: fabric-地图框架联动图层
4+
* @Author: zk
5+
* @Date: 2022-05-13 11:22:56
6+
* @LastEditors: zk
7+
* @LastEditTime: 2022-05-24 11:23:51
8+
*/
9+
import mapboxgl from '@mapgis/mapbox-gl';
10+
import { PlotMapCoordSys } from './fabric/PlotMapCoordSys';
11+
import { throttle } from '../util/Util';
12+
13+
export class FabricLayer {
14+
/**
15+
* @function: Module:FabricLayer
16+
* @description: 构造
17+
* @param {Object} map mapboxglmap
18+
* @param {Function} fabricClass fabric图层
19+
* @param {Object} fabricOptions fabric图层选项
20+
*/
21+
constructor(map, fabricClass, fabricOptions) {
22+
if(!FabricLayer.instance){
23+
const m_fabricOptions = fabricOptions || {};
24+
this.map = map;
25+
this.initDevicePixelRatio();
26+
this.canvas = this._createCanvas();
27+
this.mapContainer = map.getCanvasContainer();
28+
this.mapContainer.appendChild(this.canvas);
29+
30+
this.m_fabricCanvas = this._createFabricCanvas(this.canvas, fabricClass, m_fabricOptions);
31+
this.m_fabricCanvas.setMap(this.map)
32+
// this.mapContainer.style.perspective = this.map.transform.cameraToCenterDistance + 'px';
33+
34+
this.bindEvent();
35+
this._reset();
36+
37+
// set fabric instance
38+
FabricLayer.instance= this
39+
}else{
40+
return FabricLayer.instance
41+
}
42+
43+
}
44+
45+
/**
46+
* @function: Module:FabricLayer.pototype.initDevicePixelRatio
47+
* @description: 初始化dpi
48+
*/
49+
initDevicePixelRatio() {
50+
this.devicePixelRatio = window.devicePixelRatio || 1;
51+
}
52+
/**
53+
* @function: Module:FabricLayer.pototype._createFabricCanvas
54+
* @description: 创建fabric图层
55+
* @param {Object} fabricClass fabric canvas类
56+
* @param {Object} fabricOptions fabric options
57+
* @return {Object} fabric图层
58+
*/
59+
_createFabricCanvas(canvas, fabricClass, fabricOptions) {
60+
const m_fabricCanvas = new fabricClass(canvas, fabricOptions);
61+
m_fabricCanvas.setCoordSys(new PlotMapCoordSys(this.map));
62+
63+
m_fabricCanvas.on('selection:created', () => {
64+
this.map.dragRotate.disable();
65+
this.map.dragPan.disable();
66+
});
67+
m_fabricCanvas.on('selection:cleared', () => {
68+
this.map.dragRotate.enable();
69+
this.map.dragPan.enable();
70+
});
71+
return m_fabricCanvas
72+
}
73+
/**
74+
* @function: Module:FabricLayer.pototype.getFabricCanvas
75+
* @description: 获取fabricCanvas
76+
* @return {Object} fabricCanvas
77+
*/
78+
getFabricCanvas(){
79+
return this.m_fabricCanvas
80+
}
81+
82+
//-----------------------------------Event Methods----------------------------------------
83+
/**
84+
* @function: Module:FabricLayer.pototype.bindEvent
85+
* @description: 绑定地图事件
86+
*/
87+
bindEvent() {
88+
var map = this.map;
89+
//下面几个是mapboxgl专属事件,clickEvent和mousemoveEvent是mapv内部自带的方法不放出来
90+
this.innerMove= throttle(this.moveEvent,25,this)
91+
this.innerMoveStart = this.moveStartEvent.bind(this);
92+
this.innerMoveEnd = this.moveEndEvent.bind(this);
93+
94+
this.innnerZoomStart = this.zoomStartEvent.bind(this);
95+
this.innnerZoomEnd = this.zoomEndEvent.bind(this);
96+
97+
this.innnerRotateStart = this.rotateStartEvent.bind(this);
98+
this.innnerRotateEnd = this.rotateEndEvent.bind(this);
99+
100+
this.innnerPitchStart = this.pitchStartEvent.bind(this);
101+
this.innnerPitchEnd = this.pitchEndEvent.bind(this);
102+
103+
this.innerResize = this.resizeEvent.bind(this);
104+
105+
this.innerRemove = this.removeEvent.bind(this);
106+
107+
map.on('resize', this.innerResize);
108+
109+
map.on('zoomstart', this.innnerZoomStart);
110+
map.on('zoomend', this.innnerZoomEnd);
111+
112+
map.on('rotatestart', this.innnerRotateStart);
113+
map.on('rotateend', this.innnerRotateEnd);
114+
115+
map.on('pitchstart', this.innnerPitchStart);
116+
map.on('pitchend', this.innnerPitchEnd);
117+
118+
// map.on('move',this.innerMove)
119+
map.on('movestart', this.innerMoveStart);
120+
map.on('moveend', this.innerMoveEnd);
121+
122+
this.map.on('remove', this.innerRemove);
123+
}
124+
125+
/**
126+
* @function: Module:FabricLayer.pototype.unbindEvent
127+
* @description: 解绑地图事件
128+
*/
129+
unbindEvent() {
130+
var map = this.map;
131+
map.off('resize', this.innerResize);
132+
133+
map.off('zoomstart', this.innnerZoomStart);
134+
map.off('zoomend', this.innnerZoomEnd);
135+
136+
map.off('rotatestart', this.innnerRotateStart);
137+
map.off('rotateend', this.innnerRotateEnd);
138+
139+
map.off('pitchstart', this.innnerPitchStart);
140+
map.off('pitchend', this.innnerPitchEnd);
141+
142+
// map.off('move',this.innerMove)
143+
map.off('movestart', this.innerMoveStart);
144+
map.off('moveend', this.innerMoveEnd);
145+
}
146+
147+
moveEvent(){
148+
this._reset();
149+
}
150+
moveStartEvent() {
151+
this._unvisiable();
152+
}
153+
154+
moveEndEvent() {
155+
this._reset();
156+
this._visiable()
157+
}
158+
159+
zoomStartEvent() {
160+
this._unvisiable();
161+
}
162+
zoomEndEvent() {
163+
this._reset();
164+
this._visiable();
165+
}
166+
167+
rotateStartEvent() {
168+
this._unvisiable();
169+
170+
}
171+
rotateEndEvent() {
172+
this._reset();
173+
this._visiable();
174+
}
175+
176+
pitchStartEvent() {
177+
this._unvisiable();
178+
179+
}
180+
pitchEndEvent() {
181+
this._reset();
182+
this._visiable();
183+
}
184+
185+
resizeEvent() {
186+
this._reset();
187+
this._visiable();
188+
}
189+
removeEvent() {
190+
this.mapContainer.removeChild(this.canvas);
191+
}
192+
193+
/**
194+
* @function: Module:FabricLayer.pototype._createCanvas
195+
* @description: 创建canvas
196+
* @return {HTMLCanvasElement}
197+
*/
198+
_createCanvas() {
199+
var canvas = document.createElement('canvas');
200+
var devicePixelRatio = this.devicePixelRatio;
201+
canvas.style.position = 'absolute';
202+
canvas.style.top = '0px';
203+
canvas.style.left = '0px';
204+
canvas.width = parseInt(this.map.getCanvas().style.width) * devicePixelRatio;
205+
canvas.height = parseInt(this.map.getCanvas().style.height) * devicePixelRatio;
206+
canvas.getContext('2d').scale(devicePixelRatio, devicePixelRatio);
207+
canvas.style.width = this.map.getCanvas().style.width;
208+
canvas.style.height = this.map.getCanvas().style.height;
209+
return canvas;
210+
}
211+
212+
/**
213+
* @function: Module:FabricLayer.pototype._reset
214+
* @description: 重置视图
215+
*/
216+
_reset() {
217+
if (this.canvas === null || this.m_fabricCanvas === null) {
218+
return;
219+
}
220+
this.resizeCanvas();
221+
this.fixPosition();
222+
this.onResize();
223+
this.render();
224+
}
225+
226+
/**
227+
* @function: Module:FabricLayer.pototype.draw
228+
* @description: 绘制
229+
*/
230+
draw() {
231+
return this._reset();
232+
}
233+
234+
_visiable() {
235+
this.canvas.style.display = 'block';
236+
return this;
237+
}
238+
239+
_unvisiable() {
240+
this.canvas.style.display = 'none';
241+
return this;
242+
}
243+
244+
/**
245+
* @function: Module:FabricLayer.pototype.resizeCanvas
246+
* @description: 刷新canvas
247+
*/
248+
resizeCanvas() {
249+
this.mapContainer.style.perspective = this.map.transform.cameraToCenterDistance + 'px';
250+
if (this.canvas == undefined || this.canvas == null) return;
251+
252+
const x = parseInt(this.map.getCanvas().style.width) * this.devicePixelRatio;
253+
const y = parseInt(this.map.getCanvas().style.height) * this.devicePixelRatio;
254+
this.m_fabricCanvas.setCanvasDimensionsSize({ height: y, width: x });
255+
256+
257+
const topLeft ={x:0,y:0};
258+
259+
// 偏移canvas
260+
this._applyPosition(this.m_fabricCanvas.lowerCanvasEl, topLeft);
261+
this._applyPosition(this.m_fabricCanvas.upperCanvasEl, topLeft);
262+
263+
this.m_fabricCanvas.renderAll();
264+
}
265+
/**
266+
* @function: Module:FabricLayer.pototype._applyPosition
267+
* @description: 偏移fabriccanvas
268+
* @param {HTMLCanvasElement} canvas
269+
* @param {{x:number,y:number}} origin
270+
*/
271+
_applyPosition(canvas, origin) {
272+
if (!canvas) return;
273+
const temp = canvas;
274+
temp.style.left = origin.x + 'px';
275+
temp.style.top = origin.y + 'px';
276+
}
277+
278+
fixPosition() {}
279+
280+
onResize() {}
281+
282+
/**
283+
* @function: Module:FabricLayer.pototype.render
284+
* @description: 渲染
285+
*/
286+
render() {
287+
if (!this.canvas || !this.m_fabricCanvas) return;
288+
this.m_fabricCanvas.requestRenderAll();
289+
}
290+
291+
/**
292+
* @function: Module:FabricLayer.pototype.destroy
293+
* @description:
294+
*/
295+
destroy() {
296+
this.unbindEvent();
297+
this.mapContainer.removeChild(this.canvas);
298+
this.m_fabricCanvas = null;
299+
}
300+
}
301+
302+
mapboxgl.zondy.FabricLayer = FabricLayer;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @Description:坐标转换类(标绘)
3+
* @Author: zk
4+
* @Date: 2022-04-26 09:05:34
5+
* @LastEditors: zk
6+
* @LastEditTime: 2022-05-24 09:49:28
7+
*/
8+
9+
/**
10+
* mapbox 坐标转换类(标绘)
11+
* @constructor
12+
* @param {Object} map mapbox Map对象
13+
*/
14+
export function PlotMapCoordSys(map) {
15+
this.m_mapboxMap = map;
16+
}
17+
18+
/**
19+
* 数据坐标转屏幕坐标
20+
* @function
21+
* @param {[number,number]} data 数据坐标
22+
* @returns {[number,number]} 屏幕坐标
23+
*/
24+
PlotMapCoordSys.prototype.dataToPoint = function dataToPoint(data) {
25+
const px = this.m_mapboxMap.project([data[0], data[1]]);
26+
return [px.x, px.y];
27+
};
28+
29+
/**
30+
* 屏幕坐标转数据坐标
31+
* @function
32+
* @param {[number,number]} pt 屏幕坐标
33+
* @returns {[number,number]} 数据坐标
34+
*/
35+
PlotMapCoordSys.prototype.pointToData = function pointToData(pt) {
36+
const point = this.m_mapboxMap.unproject([pt[0], pt[1]]);
37+
return [point.lng, point.lat];
38+
};
39+
40+
41+
/**
42+
* @description: 获取当前视图下的像素块
43+
* @return {number}
44+
*/
45+
PlotMapCoordSys.prototype.getScale = function getScale() {
46+
let n=this.m_mapboxMap.getZoom()
47+
48+
return 256* Math.pow(2,n+1);
49+
};
50+
51+
/**
52+
* @description: 获取地图边界
53+
* @return {Array<Array<number>>} 边界
54+
*/
55+
PlotMapCoordSys.prototype.getBounds= function getBounds(){
56+
/**
57+
* toArray方法
58+
* var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
59+
llb.toArray(); // = [[-73.9876, 40.7661], [-73.9397, 40.8002]]
60+
*/
61+
const lnglatBounds= this.m_mapboxMap.getBounds().toArray()
62+
return lnglatBounds
63+
}

src/mapboxgl/overlay/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
import { EchartsLayer } from './EchartsLayer';
55
import { MapvLayer } from './MapvLayer';
66
import { DeckglLayer } from './DeckglLayer';
7+
import { FabricLayer } from './FabricLayer';
78

8-
export { EchartsLayer, MapvLayer,DeckglLayer };
9+
export { EchartsLayer, MapvLayer,DeckglLayer, FabricLayer };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import SimpleFactory from "../../PlotUtilBase/SimpleFactory";
2+
3+
export const DrawPlotObjectFactory2D = new SimpleFactory();

0 commit comments

Comments
 (0)