cd-mapgis 1.0.80 → 1.1.1
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/LineLayer.js +81 -0
- package/src/PointLayer.js +85 -0
- package/src/index.js +5 -1
package/package.json
CHANGED
package/src/LineLayer.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import VectorSource from 'ol/source/Vector.js';
|
|
2
|
+
import VectorLayer from 'ol/layer/Vector.js';
|
|
3
|
+
import Feature from 'ol/Feature.js';
|
|
4
|
+
import LineString from 'ol/geom/LineString.js';
|
|
5
|
+
import Style from 'ol/style/Style.js';
|
|
6
|
+
import MapView from './MapView.js';
|
|
7
|
+
import Stroke from 'ol/style/Stroke.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 自定义线图层
|
|
11
|
+
*/
|
|
12
|
+
export default class LineLayer {
|
|
13
|
+
constructor(p) {
|
|
14
|
+
this.vectorSource = new VectorSource();
|
|
15
|
+
this.vectorLayer = new VectorLayer({
|
|
16
|
+
source: this.vectorSource
|
|
17
|
+
});
|
|
18
|
+
this.mapView = p.mapView ? p.mapView : MapView.Instance;
|
|
19
|
+
this.map = this.mapView.map;
|
|
20
|
+
this.map.addLayer(this.vectorLayer);
|
|
21
|
+
// 存储点击回调函数(可选,支持多个回调时可用数组)
|
|
22
|
+
this.clickCallbacks = [];
|
|
23
|
+
// 绑定地图点击事件
|
|
24
|
+
this.map.on('click', (evt) => this.handleMapClick(evt));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
addLine(p) {
|
|
28
|
+
this.vectorSource.addFeature(this._getFeature(p));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
addLines(arr) {
|
|
32
|
+
let a = [];
|
|
33
|
+
for (let i = 0; i < arr.length; i++) {
|
|
34
|
+
a.push(this._getFeature(arr[i]));
|
|
35
|
+
}
|
|
36
|
+
this.vectorSource.addFeatures(a);
|
|
37
|
+
}
|
|
38
|
+
_getFeature(p) {
|
|
39
|
+
let feature = new Feature({
|
|
40
|
+
geometry: new LineString(p.points)
|
|
41
|
+
});
|
|
42
|
+
let color = p.color ? p.color : '#0af5e6';
|
|
43
|
+
let lineWidth = p.lineWidth ? p.lineWidth : 2;
|
|
44
|
+
feature.setStyle(new Style({
|
|
45
|
+
stroke: new Stroke({
|
|
46
|
+
color: color, // 默认选择色
|
|
47
|
+
width: lineWidth // 线宽
|
|
48
|
+
})
|
|
49
|
+
}));
|
|
50
|
+
feature.set('attr', p.attr);
|
|
51
|
+
return feature;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 注册点要素的点击事件处理器
|
|
55
|
+
* @param {Function} callback - 回调函数,接收参数 (feature, event)
|
|
56
|
+
*/
|
|
57
|
+
onClick(callback) {
|
|
58
|
+
if (typeof callback === 'function') {
|
|
59
|
+
this.clickCallbacks.push(callback);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 处理地图点击事件
|
|
64
|
+
* @private
|
|
65
|
+
*/
|
|
66
|
+
handleMapClick(evt) {
|
|
67
|
+
const map = this.map;
|
|
68
|
+
const pixel = evt.pixel;
|
|
69
|
+
|
|
70
|
+
// 遍历点击位置的所有要素,只取第一个(或全部)
|
|
71
|
+
map.forEachFeatureAtPixel(pixel, (feature, layer) => {
|
|
72
|
+
// 只处理属于当前图层的要素,并且是点类型(可选)
|
|
73
|
+
if (layer === this.vectorLayer) {
|
|
74
|
+
// 触发所有已注册的回调
|
|
75
|
+
this.clickCallbacks.forEach(cb => cb(feature, evt));
|
|
76
|
+
}
|
|
77
|
+
// 返回 true 表示停止遍历(如果只想要第一个)
|
|
78
|
+
// 可根据需求决定是否停止
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import VectorSource from 'ol/source/Vector.js';
|
|
2
|
+
import VectorLayer from 'ol/layer/Vector.js';
|
|
3
|
+
import Feature from 'ol/Feature.js';
|
|
4
|
+
import Point from 'ol/geom/Point.js';
|
|
5
|
+
import Style from 'ol/style/Style.js';
|
|
6
|
+
import Icon from 'ol/style/Icon.js';
|
|
7
|
+
import MapView from './MapView.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 自定义点图层
|
|
11
|
+
*/
|
|
12
|
+
export default class PointLayer {
|
|
13
|
+
constructor(p) {
|
|
14
|
+
this.vectorSource = new VectorSource();
|
|
15
|
+
this.vectorLayer = new VectorLayer({
|
|
16
|
+
source: this.vectorSource
|
|
17
|
+
});
|
|
18
|
+
this.mapView = p.mapView ? p.mapView : MapView.Instance;
|
|
19
|
+
this.map = this.mapView.map;
|
|
20
|
+
this.map.addLayer(this.vectorLayer);
|
|
21
|
+
// 存储点击回调函数(可选,支持多个回调时可用数组)
|
|
22
|
+
this.clickCallbacks = [];
|
|
23
|
+
// 绑定地图点击事件
|
|
24
|
+
this.map.on('click', (evt) => this.handleMapClick(evt));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 添加带图标的点要素
|
|
28
|
+
* @param {*} p
|
|
29
|
+
*/
|
|
30
|
+
addPoint(p) {
|
|
31
|
+
this.vectorSource.addFeature(this._getFeature(p));
|
|
32
|
+
}
|
|
33
|
+
addPoints(arr) {
|
|
34
|
+
let a = [];
|
|
35
|
+
for (let i = 0; i < arr.length; i++) {
|
|
36
|
+
a.push(this._getFeature(arr[i]));
|
|
37
|
+
}
|
|
38
|
+
this.vectorSource.addFeatures(a);
|
|
39
|
+
}
|
|
40
|
+
_getFeature(p) {
|
|
41
|
+
let feature = new Feature({
|
|
42
|
+
geometry: new Point(p.point)
|
|
43
|
+
});
|
|
44
|
+
feature.setStyle(new Style({
|
|
45
|
+
image: new Icon({
|
|
46
|
+
src: p.img, // 外部图标路径
|
|
47
|
+
scale: 1, // 缩放比例
|
|
48
|
+
anchor: [0.5, 0.5], // 锚点为中心
|
|
49
|
+
anchorOrigin: 'center',
|
|
50
|
+
anchorXUnits: 'fraction',
|
|
51
|
+
anchorYUnits: 'fraction'
|
|
52
|
+
})
|
|
53
|
+
}));
|
|
54
|
+
feature.set('attr', p.attr);
|
|
55
|
+
return feature;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 注册点要素的点击事件处理器
|
|
59
|
+
* @param {Function} callback - 回调函数,接收参数 (feature, event)
|
|
60
|
+
*/
|
|
61
|
+
onClick(callback) {
|
|
62
|
+
if (typeof callback === 'function') {
|
|
63
|
+
this.clickCallbacks.push(callback);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 处理地图点击事件
|
|
68
|
+
* @private
|
|
69
|
+
*/
|
|
70
|
+
handleMapClick(evt) {
|
|
71
|
+
const map = this.map;
|
|
72
|
+
const pixel = evt.pixel;
|
|
73
|
+
|
|
74
|
+
// 遍历点击位置的所有要素,只取第一个(或全部)
|
|
75
|
+
map.forEachFeatureAtPixel(pixel, (feature, layer) => {
|
|
76
|
+
// 只处理属于当前图层的要素,并且是点类型(可选)
|
|
77
|
+
if (layer === this.vectorLayer) {
|
|
78
|
+
// 触发所有已注册的回调
|
|
79
|
+
this.clickCallbacks.forEach(cb => cb(feature, evt));
|
|
80
|
+
}
|
|
81
|
+
// 返回 true 表示停止遍历(如果只想要第一个)
|
|
82
|
+
// 可根据需求决定是否停止
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
package/src/index.js
CHANGED
|
@@ -7,6 +7,8 @@ import HttpUtil from './HttpUtil.js';
|
|
|
7
7
|
import InnerUtil from './InnerUtil.js';
|
|
8
8
|
import StyleUtil from './StyleUtil.js';
|
|
9
9
|
import Location from './Location.js';
|
|
10
|
+
import PointLayer from './PointLayer.js';
|
|
11
|
+
import LineLayer from './LineLayer.js';
|
|
10
12
|
|
|
11
13
|
// 导出模块
|
|
12
14
|
export {
|
|
@@ -17,5 +19,7 @@ export {
|
|
|
17
19
|
HttpUtil,
|
|
18
20
|
InnerUtil,
|
|
19
21
|
StyleUtil,
|
|
20
|
-
Location
|
|
22
|
+
Location,
|
|
23
|
+
PointLayer,
|
|
24
|
+
LineLayer
|
|
21
25
|
};
|