cd-mapgis 1.0.26 → 1.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/LayerUtil.js +18 -1
- package/src/MapDrawUtil.js +155 -0
- package/src/MapView.js +74 -27
- package/src/StyleUtil.js +74 -0
package/package.json
CHANGED
package/src/LayerUtil.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// 使用 CommonJS 模块系统
|
|
2
1
|
import XYZ from 'ol/source/XYZ.js';
|
|
3
2
|
import Tile from 'ol/layer/Tile.js';
|
|
4
3
|
import Image from 'ol/layer/Image.js';
|
|
@@ -8,6 +7,10 @@ import TilegridWMTS from 'ol/tilegrid/WMTS.js';
|
|
|
8
7
|
import * as proj from 'ol/proj.js';
|
|
9
8
|
import * as extent from 'ol/extent.js';
|
|
10
9
|
import WMTSCapabilities from 'ol/format/WMTSCapabilities.js';
|
|
10
|
+
import GeoJSON from 'ol/format/GeoJSON.js';
|
|
11
|
+
import Feature from 'ol/Feature.js';
|
|
12
|
+
import VectorSource from 'ol/source/Vector.js';
|
|
13
|
+
import VectorLayer from 'ol/layer/Vector.js';
|
|
11
14
|
|
|
12
15
|
/**
|
|
13
16
|
* 图层工具
|
|
@@ -186,4 +189,18 @@ export default class LayerUtil {
|
|
|
186
189
|
}
|
|
187
190
|
return null;
|
|
188
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* 将geoJson数据包装为一个图层
|
|
194
|
+
* @param {*} json
|
|
195
|
+
*/
|
|
196
|
+
static getJsonLayer(p) {
|
|
197
|
+
let geometry = new GeoJSON().readGeometry(p.data);
|
|
198
|
+
let feature = new Feature({ geometry: geometry });
|
|
199
|
+
let vectorSource = new VectorSource({ features: [feature] });
|
|
200
|
+
let layer = new VectorLayer({
|
|
201
|
+
source: vectorSource,
|
|
202
|
+
style: p.style
|
|
203
|
+
});
|
|
204
|
+
return { layer, geometry };
|
|
205
|
+
}
|
|
189
206
|
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import Draw from 'ol/interaction/Draw.js';
|
|
2
|
+
import VectorSource from 'ol/source/Vector.js';
|
|
3
|
+
import VectorLayer from 'ol/layer/Vector.js';
|
|
4
|
+
import StyleUtil from './StyleUtil.js';
|
|
5
|
+
import { getLength, getArea } from 'ol/sphere.js';
|
|
6
|
+
import { unByKey } from 'ol/Observable.js';
|
|
7
|
+
import Overlay from 'ol/Overlay.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 地图绘图工具
|
|
11
|
+
*/
|
|
12
|
+
export default class MapDrawUtil {
|
|
13
|
+
constructor(p) {
|
|
14
|
+
this.map = p.map;
|
|
15
|
+
this.type = null;//几何类型,包括Point、LineString、Polygon、Circle
|
|
16
|
+
this.drawObj = null;//
|
|
17
|
+
this._tempLayer = null;//绘制要素的临时图层
|
|
18
|
+
this._overlay = null;//临时覆盖物图层
|
|
19
|
+
this._overlay_div = null;//临时覆盖物图层上的div
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//初始化交互绘制的工具
|
|
23
|
+
init(type) {
|
|
24
|
+
this.type = type;
|
|
25
|
+
this.drawObj = new Draw({
|
|
26
|
+
type: type,
|
|
27
|
+
source: new VectorSource(),
|
|
28
|
+
free: false,//徒手模式,值为true时可鼠标跟随绘制
|
|
29
|
+
style: StyleUtil.getInteractionStyle()
|
|
30
|
+
});
|
|
31
|
+
this.map.addInteraction(this.drawObj);
|
|
32
|
+
}
|
|
33
|
+
//将要数绘制到临时图层
|
|
34
|
+
_drawTempLayer(feature) {
|
|
35
|
+
this._clearTempLayer();
|
|
36
|
+
let vectorSource = new VectorSource({ features: [feature] });
|
|
37
|
+
this._tempLayer = new VectorLayer({
|
|
38
|
+
source: vectorSource,
|
|
39
|
+
style: StyleUtil.getMapDrawStyle()
|
|
40
|
+
});
|
|
41
|
+
this.map.addLayer(this._tempLayer);
|
|
42
|
+
}
|
|
43
|
+
//绘制完成事件
|
|
44
|
+
drawend(fun, stop) {
|
|
45
|
+
this.drawObj.on('drawend', (e) => {
|
|
46
|
+
this._drawTempLayer([e.feature]);
|
|
47
|
+
try {
|
|
48
|
+
fun(e.feature.getGeometry().getCoordinates());//绘制完成后,将绘制的坐标串传入回调函数
|
|
49
|
+
} catch (ex) {
|
|
50
|
+
console.error(ex);
|
|
51
|
+
}
|
|
52
|
+
if (stop) this._closeDrawObj();
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
//关闭工具
|
|
56
|
+
_closeDrawObj() {
|
|
57
|
+
if (this.drawObj != null) {
|
|
58
|
+
this.map.removeInteraction(this.drawObj);
|
|
59
|
+
this.drawObj = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//清除覆盖物
|
|
63
|
+
_clearOverlay() {
|
|
64
|
+
if (this._overlay != null) {
|
|
65
|
+
this.map.removeOverlay(this._overlay);
|
|
66
|
+
this._overlay = null;
|
|
67
|
+
}
|
|
68
|
+
if (this._overlay_div != null) {
|
|
69
|
+
this._overlay_div.parentNode.removeChild(this._overlay_div);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
//清除临时图层
|
|
73
|
+
_clearTempLayer() {
|
|
74
|
+
if (this._tempLayer != null) {
|
|
75
|
+
this._tempLayer.getSource().clear();//清空图层
|
|
76
|
+
this.map.removeLayer(this._tempLayer);//移除图层
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//退出工具
|
|
80
|
+
close() {
|
|
81
|
+
this._closeDrawObj();
|
|
82
|
+
this._clearOverlay();
|
|
83
|
+
this._clearTempLayer();
|
|
84
|
+
}
|
|
85
|
+
//工具类
|
|
86
|
+
tool(toolType) {
|
|
87
|
+
let _sketch = null;
|
|
88
|
+
let _listener = null;
|
|
89
|
+
let _this = this;
|
|
90
|
+
//绘制开始事件
|
|
91
|
+
this.drawObj.on('drawstart', (e) => {
|
|
92
|
+
_sketch = e.feature;
|
|
93
|
+
_listener = _sketch.getGeometry().on('change', function (evt) {
|
|
94
|
+
let geom = evt.target;
|
|
95
|
+
var viewTxt;
|
|
96
|
+
if (toolType == "len") {
|
|
97
|
+
viewTxt = _this._formatLength(geom.clone());
|
|
98
|
+
} else if (toolType == "area") {
|
|
99
|
+
viewTxt = _this._formatArea(geom.clone());
|
|
100
|
+
}
|
|
101
|
+
_this._viewToolText(viewTxt, geom.getLastCoordinate());
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
//绘制完成事件
|
|
105
|
+
this.drawObj.on('drawend', function (e) {
|
|
106
|
+
this._drawTempLayer([e.feature]);
|
|
107
|
+
_sketch = null;
|
|
108
|
+
unByKey(_listener);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
//获取线的长度字符串
|
|
113
|
+
_formatLength(line) {
|
|
114
|
+
var length = getLength(line, { projection: 'EPSG:4326' });
|
|
115
|
+
var output;
|
|
116
|
+
if (length > 1000) {
|
|
117
|
+
output = Math.round((length / 1000) * 100) / 100 + ' 千米';
|
|
118
|
+
} else {
|
|
119
|
+
output = Math.round(length * 100) / 100 + ' 米';
|
|
120
|
+
}
|
|
121
|
+
return output;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
//获取面积的字符串
|
|
125
|
+
_formatArea(polygon) {
|
|
126
|
+
var area = getArea(polygon, { projection: 'EPSG:4326' });
|
|
127
|
+
var output = Math.round(area * 100) / 100 + ' 平方米';
|
|
128
|
+
return output;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
//显示工具的文本
|
|
132
|
+
_viewToolText(txt, position) {
|
|
133
|
+
this._clearOverlay();
|
|
134
|
+
let div = document.createElement('div');
|
|
135
|
+
div.className = 'tooltip hidden';
|
|
136
|
+
div.innerHTML = txt;
|
|
137
|
+
div.style.position = 'relative';
|
|
138
|
+
div.style.background = 'rgba(59, 20, 233, 0.7)';
|
|
139
|
+
div.style.borderRadius = '4px';
|
|
140
|
+
div.style.color = 'white';
|
|
141
|
+
div.style.padding = '4px 8px';
|
|
142
|
+
div.style.opacity = 0.7;
|
|
143
|
+
div.style.whiteSpace = 'nowrap';
|
|
144
|
+
div.style.fontWeight = '';
|
|
145
|
+
div.classList.remove('hidden');
|
|
146
|
+
this._overlay_div = div;
|
|
147
|
+
this._overlay = new Overlay({
|
|
148
|
+
element: div,
|
|
149
|
+
offset: [15, 0],
|
|
150
|
+
positioning: 'center-left'
|
|
151
|
+
});
|
|
152
|
+
this._overlay.setPosition(position);
|
|
153
|
+
this.map.addOverlay(this._overlay);
|
|
154
|
+
}
|
|
155
|
+
}
|
package/src/MapView.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
import GeoJSON from 'ol/format/GeoJSON.js';
|
|
2
|
-
import Feature from 'ol/Feature.js';
|
|
3
|
-
import VectorSource from 'ol/source/Vector.js';
|
|
4
|
-
import Style from 'ol/style/Style.js';
|
|
5
|
-
import Stroke from 'ol/style/Stroke.js';
|
|
6
|
-
import Fill from 'ol/style/Fill.js';
|
|
7
|
-
import VectorLayer from 'ol/layer/Vector.js';
|
|
8
1
|
import View from 'ol/View.js';
|
|
9
2
|
import Map from 'ol/Map.js';
|
|
10
|
-
import
|
|
3
|
+
import LayerUtil from './LayerUtil.js';
|
|
4
|
+
import StyleUtil from './StyleUtil.js';
|
|
5
|
+
import MapDrawUtil from './MapDrawUtil.js';
|
|
6
|
+
|
|
11
7
|
/**
|
|
12
8
|
* ol地图显示
|
|
13
9
|
*/
|
|
@@ -28,6 +24,8 @@ export default class MapView {
|
|
|
28
24
|
this._view_templayer = null;
|
|
29
25
|
this._init(this.params);
|
|
30
26
|
MapView.Instance = this;
|
|
27
|
+
|
|
28
|
+
this.mapDrawUtil = null;//绘制工具
|
|
31
29
|
}
|
|
32
30
|
//初始化地图及其他参数
|
|
33
31
|
_init(p) {
|
|
@@ -74,26 +72,12 @@ export default class MapView {
|
|
|
74
72
|
* @param {*} p
|
|
75
73
|
*/
|
|
76
74
|
viewOnTemplayer(p) {
|
|
77
|
-
|
|
78
|
-
let feature = new Feature({ geometry: geometry });
|
|
79
|
-
let vectorSource = new VectorSource({ features: [feature] });
|
|
80
|
-
let style = new Style({
|
|
81
|
-
stroke: new Stroke({
|
|
82
|
-
color: '#0af5e6', // 默认选择色
|
|
83
|
-
width: 2 // 线宽
|
|
84
|
-
}),
|
|
85
|
-
fill: new Fill({
|
|
86
|
-
color: 'rgba(0, 0, 0, 0)' // 透明填充
|
|
87
|
-
})
|
|
88
|
-
});
|
|
89
|
-
this.clearTemplayer(); //移除之前的显示
|
|
75
|
+
p.style = StyleUtil.getViewOnTemplayerStyle();
|
|
90
76
|
//重建图层,避免在其他地方全部移除图层后,templayer不为空,但是已经不在地图中的情况
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// 添加到地图
|
|
96
|
-
this.map.addLayer(this._view_templayer);
|
|
77
|
+
let { layer, geometry } = LayerUtil.getJsonLayer(p);
|
|
78
|
+
this._view_templayer = layer;
|
|
79
|
+
this.clearTemplayer(); //移除之前的显示
|
|
80
|
+
this.map.addLayer(this._view_templayer);// 添加到地图
|
|
97
81
|
// fit方法 - 自动适应范围
|
|
98
82
|
let extent = geometry.getExtent();
|
|
99
83
|
let extent2 = this.expandExtent(extent);
|
|
@@ -139,6 +123,7 @@ export default class MapView {
|
|
|
139
123
|
var newMaxY = centerY + newHeight / 2;
|
|
140
124
|
return [newMinX, newMinY, newMaxX, newMaxY];
|
|
141
125
|
}
|
|
126
|
+
//设置中心点
|
|
142
127
|
setCenter(center, useAnimation) {
|
|
143
128
|
if (useAnimation) {
|
|
144
129
|
this.map.getView().animate({ center: center });
|
|
@@ -146,7 +131,69 @@ export default class MapView {
|
|
|
146
131
|
this.map.getView().setCenter(center);
|
|
147
132
|
}
|
|
148
133
|
}
|
|
134
|
+
//设置显示级别
|
|
149
135
|
setZoom(zoom) {
|
|
150
136
|
this.map.getView().setZoom(zoom);
|
|
151
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* 添加地图事件
|
|
140
|
+
* click(单击) dblclick(双击) singleclick(单击,延迟250毫秒)
|
|
141
|
+
* moveend(鼠标滚动事件) pointermove(鼠标移动事件) pointerdrag(鼠标拖动事件) pointerdown pointerup
|
|
142
|
+
* precompose(地图准备渲染,为渲染) postcompose(地图渲染中) postrender(地图渲染全部结束)
|
|
143
|
+
* change:layerGroup(地图图层增删时触发) change:size:(地图窗口发生变化就会触发) change:target(地图绑定的div发生更改时触发) change:view(地图view对象发生变化触发)
|
|
144
|
+
* propertychange(Map对象中任意的property值改变时触发)
|
|
145
|
+
* @param {*} name
|
|
146
|
+
* @param {*} fun
|
|
147
|
+
*/
|
|
148
|
+
addEvent(name, fun) {
|
|
149
|
+
return this.map.on(name, fun);
|
|
150
|
+
}
|
|
151
|
+
//添加地图事件一次
|
|
152
|
+
addEventOnce(name, fun) {
|
|
153
|
+
const key = this.map.on(name, function (e) {
|
|
154
|
+
try {
|
|
155
|
+
fun(e);
|
|
156
|
+
} catch (ex) {
|
|
157
|
+
console.error(ex);
|
|
158
|
+
}
|
|
159
|
+
this.removeEvent(key);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
//移除事件
|
|
163
|
+
removeEvent(key) {
|
|
164
|
+
this.map.un(key.type, key.listener);
|
|
165
|
+
}
|
|
166
|
+
//注册显示鼠标的地图坐标
|
|
167
|
+
viewMousePoint(call) {
|
|
168
|
+
this.addEvent('pointermove', (e) => {
|
|
169
|
+
call(e.coordinate[0].toFixed(6) + ',' + e.coordinate[1].toFixed(6));
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
//绘制一次性的图形
|
|
173
|
+
startDraw(type, fun) {
|
|
174
|
+
this.clearDraw();
|
|
175
|
+
this.mapDrawUtil = new MapDrawUtil({ map: this.map });
|
|
176
|
+
this.mapDrawUtil.init(type);
|
|
177
|
+
this.mapDrawUtil.drawend(fun, true);
|
|
178
|
+
}
|
|
179
|
+
//地图工具 len、area
|
|
180
|
+
tool(type) {
|
|
181
|
+
this.clearDraw();
|
|
182
|
+
let dic = { len: 'LineString', area: 'Polygon' };
|
|
183
|
+
this.mapDrawUtil = new MapDrawUtil({ map: this.map });
|
|
184
|
+
let t = dic[type];
|
|
185
|
+
if (!t) {
|
|
186
|
+
console.error("type不是len/area");
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
this.mapDrawUtil.init(t);
|
|
190
|
+
this.mapDrawUtil.tool(type);
|
|
191
|
+
}
|
|
192
|
+
//关闭绘制功能
|
|
193
|
+
clearDraw() {
|
|
194
|
+
if (this.mapDrawUtil != null) {
|
|
195
|
+
this.mapDrawUtil.close();
|
|
196
|
+
this.mapDrawUtil = null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
152
199
|
}
|
package/src/StyleUtil.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import Style from 'ol/style/Style.js';
|
|
2
|
+
import Stroke from 'ol/style/Stroke.js';
|
|
3
|
+
import Fill from 'ol/style/Fill.js';
|
|
4
|
+
import Circle from 'ol/style/Circle.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 样式工具类
|
|
8
|
+
*/
|
|
9
|
+
export default class StyleUtil {
|
|
10
|
+
/**
|
|
11
|
+
* 获取默认的显示要素到临时图层的样式(一般用于高亮显示选中的图斑)
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
static getViewOnTemplayerStyle() {
|
|
15
|
+
return new Style({
|
|
16
|
+
stroke: new Stroke({
|
|
17
|
+
color: '#0af5e6', // 默认选择色
|
|
18
|
+
width: 2 // 线宽
|
|
19
|
+
}),
|
|
20
|
+
fill: new Fill({
|
|
21
|
+
color: 'rgba(0, 0, 0, 0)' // 透明填充
|
|
22
|
+
})
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 获取地图绘制的默认样式
|
|
27
|
+
*/
|
|
28
|
+
static getMapDrawStyle() {
|
|
29
|
+
return new Style({
|
|
30
|
+
fill: new Fill({
|
|
31
|
+
color: 'rgba(255, 255, 255, 0.2)'
|
|
32
|
+
}),
|
|
33
|
+
stroke: Stroke({
|
|
34
|
+
color: 'rgba(0, 0, 255, 1)',
|
|
35
|
+
lineDash: [10, 10],
|
|
36
|
+
width: 2
|
|
37
|
+
}),
|
|
38
|
+
image: Circle({
|
|
39
|
+
radius: 5,
|
|
40
|
+
stroke: Stroke({
|
|
41
|
+
color: 'rgba(0, 0, 0, 0.7)'
|
|
42
|
+
}),
|
|
43
|
+
fill: Fill({
|
|
44
|
+
color: 'rgba(255, 255, 255, 0.2)'
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 获取交互工具的默认样式
|
|
51
|
+
* @returns
|
|
52
|
+
*/
|
|
53
|
+
static getInteractionStyle() {
|
|
54
|
+
return new Style({
|
|
55
|
+
fill: new Fill({
|
|
56
|
+
color: 'rgba(0, 0, 255, 0.5)'
|
|
57
|
+
}),
|
|
58
|
+
stroke: new Stroke({
|
|
59
|
+
color: 'rgba(0, 0, 255, 1)',
|
|
60
|
+
lineDash: [10, 10],
|
|
61
|
+
width: 2
|
|
62
|
+
}),
|
|
63
|
+
image: new Circle({
|
|
64
|
+
radius: 5,
|
|
65
|
+
stroke: new Stroke({
|
|
66
|
+
color: 'rgba(0, 0, 0, 0.7)'
|
|
67
|
+
}),
|
|
68
|
+
fill: new Fill({
|
|
69
|
+
color: 'rgba(255, 255, 255, 0.2)'
|
|
70
|
+
})
|
|
71
|
+
})
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|