SuperMap
v10.0.0
引入
SuperMap.Include.js
:1
<script type="text/javascript" src="http://iclient.supermap.io/libs/iclient8c/libs/SuperMap.Include.js"></script>
vue中安装
@supermap/iclient-classic
,SuperMap
构造函数就是从它暴露1
import { SuperMap } from "@supermap/iclient-classic";
公共封装
根据需求自行调整
1 | import { GUID } from "@/utils/tool"; |
vue示例
示例整合: vm
为vue组件实例,会为组件data中定义相关数据保存超图创建的实例,id
为地图容器id
撒点
点击撒点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21export function clickAddMarker(id, vm) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.markLayer = initMarkLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer, vm.markLayer]);
vm.mapDrawPoint = drawFeature(
vm.smap,
vm.drawLayer,
"Point",
f => {
const geo = f.geometry;
const mark = {
coord: `${geo.x},${geo.y}`
};
clearMarker(vm.markers, vm.markLayer);
vm.markers = addMaker([mark], vm.markLayer);
},
true
);
}根据数据撒点(marker方式: 自定义图标)**并创建点击弹窗**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21// 示例数据
// const markData = [
// { key: "id1", coord: "110.38416383175,23.536161129632", descArr: [{type: 'title', value: '广西南城第三中学'}, {type: '', name: '地址', value: '平南街道二环大道816号'}, {type: '', name: '电话', value: '07757860173'}] },
// { key: "id2", coord: "110.24612955917,23.275241314091", descArr: [{type: 'title', value: '广西南城百货有限责任公司平南分公司'}, {type: '', name: '地址', value: '平南镇朝阳路中心广场地下负二层'}, {type: '', name: '电话', value: '07752795222'}] }
//];
export function addMarkerByData(id, vm, markData) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.markLayer = initMarkLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer, vm.markLayer]);
clearMarker(vm.markers, vm.markLayer);
vm.markers = addMaker(markData, vm.markLayer, (marker, m) => {
//点击撒点创建弹窗
const curWindow = marker.infoWindow;
if (curWindow) {
return clearInfoWindow(marker);
}
createInfoWindow(vm.smap, marker, m);
});
}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//撒点弹窗样式
.smPopupContent {
padding: 0 ;
.supermap-infowindow-content {
background-color: #fff;
border-radius: 5px;
cursor: default;
height: 100%;
width: 100%;
.row {
display: flex;
line-height: 25px;
max-width: 200px;
padding: 0 10px;
&.head {
display: flex;
justify-content: center;
align-items: center;
background: #5b6996;
line-height: 30px;
padding: 0 20px;
span {
text-align: center;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: white;
}
.icon-more {
width: 15px;
height: 15px;
background: url("./assets/imgs/pub/bars.png") 0 0 no-repeat;
background-size: 100%;
position: absolute;
right: 2px;
&:hover {
cursor: pointer;
}
}
}
&.bottom {
line-height: normal;
padding: 10px 0;
display: flex;
justify-content: center;
color: #43cdd3;
&:hover {
cursor: pointer;
}
}
span {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
color: #666;
display: block;
flex-shrink: 1;
font-size: 12px;
width: 100%;
&.title {
color: #333;
flex-shrink: 0;
text-align: right;
width: auto;
}
}
}
.close {
display: block;
position: absolute;
right: 6px;
top: 6px;
z-index: 1;
.icon {
color: #72a9e4;
font-weight: bold;
}
}
}
}
根据数据撒点(vector方式: 统计数字 + hover变色 + 提示)
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// 示例数据
// const pointData = [
// { label: "1", name: "平南街道二环大道816号", coord: "110.38416383175,23.536161129632" },
// { label: "2", name: "平南镇朝阳路中心广场地下负二层", coord: "110.24612955917,23.275241314091" }
// ];
export function addVectorPoints(id, vm, pointData) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer]);
//撒点添加
pointData.map(item => {
const coord = item.coord.split(",");
const point = new SuperMap.Geometry.Point(coord[0], coord[1]);
const style = {
fontColor: "#fff",
fontSize: "12px",
label: item.label + "",
labelAlign: "cm",
pointRadius: 15,
strokeColor: "#3b41c5",
strokeWidth: 1,
fillColor: "#a981bb"
};
const feature = new SuperMap.Feature.Vector(point);
feature.data = JSON.parse(JSON.stringify(item));
feature.style = style;
vm.drawLayer && vm.drawLayer.addFeatures(feature);
});
vm.selectLabel = selectFeature(vm.smap, vm.drawLayer, {
click(f) {
console.log(f, "点击触发");
}
}, {
hover: true, //hover: true使点击无效仅hover生效
onSelect(feature) {
clearInfoWindow();
// 选中要素操作
feature.style.fillColor = "#20E2D7";
vm.drawLayer.redraw(); //重新设置颜色后重新绘制
const contentHTML = `<div style='width:max-content;padding:8px;color:black;font-size:12px;border:1px solid #ccc'>${
feature.data.name
}</div>`;
vm.infoWindow = new SuperMap.Popup.Anchored(
"chicken",
new SuperMap.LonLat(
feature.data.coord.split(",")[0],
feature.data.coord.split(",")[1]
),
null,
contentHTML,
new SuperMap.Icon(
"",
new SuperMap.Size(1, 1),
new SuperMap.Pixel(0, -2)
),
false,
null
);
vm.infoWindow.autoSize = true;
vm.smap.addPopup(vm.infoWindow);
},
onUnselect(feature) {
clearInfoWindow();
if (feature.data.isSelected) {
return;
} else {
// 未选中要素操作
feature.style.fillColor = "#a981bb";
vm.drawLayer.redraw();
}
},
})
//清除弹窗
function clearInfoWindow() {
const w = vm.infoWindow;
if (w) {
w.destroy();
vm.infoWindow = null;
}
}
}
绘制
绘制多边形
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
26export function drawPolygon(id, vm) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer]);
vm.mapDrawPolygon = drawFeature(
vm.smap,
vm.drawLayer,
"Polygon",
f => {
//获得图层几何对象(保存绘制成功的点)
const geometry = f.geometry;
const polygonPoints = [];
const root = geometry.components[0];
root.components.map(point => {
polygonPoints.push({
x: point.x,
y: point.y
});
});
console.log(polygonPoints);
vm.mapDrawPolygon.deactivate();
},
false
);
}绘制矩形
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
39export function drawRect(id, vm) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer]);
vm.drawRect = drawFeature(vm.smap, vm.drawLayer, "Box", rect => {
const geometry = rect.geometry;
const points = [];
//Point创建点实例
points.push(new SuperMap.Geometry.Point(geometry.x, geometry.y));
points.push(
new SuperMap.Geometry.Point(geometry.x + geometry.width, geometry.y)
);
points.push(
new SuperMap.Geometry.Point(
geometry.x + geometry.width,
geometry.y + geometry.height
)
);
points.push(
new SuperMap.Geometry.Point(
geometry.x,
geometry.y + geometry.height
)
);
points.push(new SuperMap.Geometry.Point(geometry.x, geometry.y));
//通过点坐标绘制图形,可通过多个点绘制任意地图区域
//LinearRing通过多个点创建线环
const linearRings = new SuperMap.Geometry.LinearRing(points);
//Polygon通过LinearRing创建多边形
const polygon = new SuperMap.Geometry.Polygon([linearRings]);
const style = superMapDrawStyle();
//Feature.Vector创建矢量要素,参数:1-代表要素的几何实例,2-描述要素的可序列化属性,将映射到attributes,3-样式对象
const newrect = new SuperMap.Feature.Vector(polygon, null, style);
vm.drawLayer.removeFeatures(rect); //删除绘制控件生成的默认矩形,再自定义设置后添加
vm.drawLayer.addFeatures(newrect);
})
// vm.drawRect.activate() 激活
}绘制圆形
1
2
3
4
5
6
7
8
9
10
11
12
13
14export function drawCircular(id, vm) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer]);
vm.drawCircular = drawFeature(vm.smap, vm.drawLayer, "RegularPolygon", cir => {
const circular = cir;
const style = superMapDrawStyle();
circular.style = style;
vm.drawLayer.removeFeatures(cir); //删除默认圆形,再自定义设置后添加
vm.drawLayer.addFeatures(circular);
}, false, { handlerOptions: { sides: 50 } })
//vm.drawCircular.activate() 激活
}绘制带箭头的线
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//绘制带箭头的线
export function arrLine(id, vm) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer]);
vm.arrLine = drawFeature(vm.smap, vm.drawLayer, "Path", line => {
// 删除绘制的线
const removeLine = line => {
vm.drawLayer.removeFeatures(line);
};
// 获得图层几何对象
const geometry = line.geometry;
const root = geometry.components[0];
const l = root.components.length;
if (!root || l < 2) {
removeLine(line);
return;
}
// 绘制箭头线
const Arrow = lineString => {
const linepoints = lineString.components;
const endPoint1 = linepoints[linepoints.length - 1];
const endPoint2 = linepoints[linepoints.length - 2];
const midPointX = endPoint2.x + 0.9 * (endPoint1.x - endPoint2.x);
const midPointY = endPoint2.y + 0.9 * (endPoint1.y - endPoint2.y);
const midPoint1 = new SuperMap.Geometry.Point(midPointX, midPointY);
const midPoint2 = new SuperMap.Geometry.Point(midPointX, midPointY);
// 在线上9/10的位置取一点连接直线末端做两条相同的短直线
const points2 = [midPoint1, endPoint1];
const rightLine = new SuperMap.Geometry.LineString(points2);
rightLine.rotate(15, endPoint1);
// 此为直线旋转,旋转角度可自由调节
const points3 = [midPoint2, endPoint1];
const leftLine = new SuperMap.Geometry.LineString(points3);
leftLine.rotate(345, endPoint1);
// 分别旋转两条短直线于lineString两侧,构成一个箭头样式
const multiLineString = new SuperMap.Geometry.MultiLineString([
lineString,
rightLine,
leftLine
]);
const arrowLine = new SuperMap.Feature.Vector(multiLineString);
const style = superMapDrawStyle();
arrowLine.style = style;
removeLine(line);
vm.drawLayer.addFeatures(arrowLine);
};
Arrow(root);
}, false, { multi: true })
// vm.arrLine.activate() 激活
}带编辑框的文字(基于点绘制)
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
123export function drawTextPoint(id, vm) {
const strategy = new SuperMap.Strategy.GeoText()
strategy.style = superMapDrawStyle();
strategy.style.fontColor = "#ff3300";
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer({
strategies: [strategy]
});
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer]);
selectFeature(vm.smap, vm.drawLayer, {
click(f) {
console.log('点击文字重新编辑');
clearTextEdit(vm);
addTextEdit(vm, f);
}
})
vm.mapTexts = vm.mapTexts || [];
vm.drawTextPoint = drawFeature(vm.smap, vm.drawLayer, "Point", p => {
// 获得图层几何对象
const geometry = p.geometry;
const coord = `${geometry.x},${geometry.y}`;
const data = {
key: GUID(),
coord: coord
};
vm.drawLayer.removeFeatures(p)
addText(vm, coord, data)
vm.drawTextPoint.deactivate();
})
//vm.drawTextPoint.activate() 激活
}
// 根据坐标和数据添加文字标签
export function addText(vm, coordStr, data) {
const coord = coordStr.split(",");
const text = data.text || "请输入文字";
const geoText = new SuperMap.Geometry.GeoText(coord[0], coord[1], text);
const feature = new SuperMap.Feature.Vector(geoText);
feature.data = JSON.parse(JSON.stringify(data));
feature.data.text = text;
feature.data.type = "text";
vm.drawLayer.addFeatures(feature);
//存放便于清除
vm.mapTexts.push({ key: data.key, text: feature });
addTextEdit(vm, feature);
}
// 创建可输入的文字标签
export function addTextEdit(vm, feature) {
clearTextEdit(vm);
const data = feature.data || {};
const key = data.key || "";
const closeId = `supermap-textedit-close${key}`;
const inputId = `supermap-textedit-input${key}`;
const html = editTextHtml(data, closeId, inputId);
let coord = (data.coord || ",").split(",");
const lonlat = new SuperMap.LonLat(coord[0], coord[1]);
const size = new SuperMap.Size(0, 0);
const offset = new SuperMap.Pixel(0, 0);
const icon = new SuperMap.Icon(null, size, offset);
vm.textWindow = new SuperMap.Popup.Anchored(
`textEdit${data.key}`,
lonlat,
null,
html,
icon,
false,
null
);
vm.textWindow.autoSize = true;
vm.textWindow.setBackgroundColor("rgba(255,255,255,0)");
// 添加自定义信息框
vm.smap.addPopup(vm.textWindow);
// 添加自定义信息框输入回车事件
const domInput = document.getElementById(inputId);
if (domInput) {
const enterEvent = e => {
const theEvent = window.event || e;
const code = theEvent.keyCode || theEvent.which || theEvent.charCode;
if (code == 13) {
const data = JSON.parse(JSON.stringify(feature.data));
data.text = domInput.value;
removeTextByKey(data.key, vm);
addText(vm, data.coord, data);
clearTextEdit(vm);
}
};
domInput.removeEventListener("keydown", enterEvent);
domInput.addEventListener("keydown", enterEvent);
}
// 添加自定义信息框关闭事件
const domClose = document.getElementById(closeId);
if (domClose) {
const closeEvent = () => {
clearTextEdit(vm);
};
domClose.removeEventListener("click", closeEvent);
domClose.addEventListener("click", closeEvent);
}
}
// 删除文字通过key
export function removeTextByKey(key, vm) {
let removeIndex = -1;
vm.mapTexts.map((textObj, index) => {
if (textObj.key === key) {
vm.drawLayer.removeFeatures(textObj.text);
removeIndex = index;
}
});
if (removeIndex !== -1) {
vm.mapTexts.splice(removeIndex, 1)
}
}
// 删除上一个文字框
export function clearTextEdit(vm) {
const textW = vm.textWindow
if (textW) {
textW.destroy();
vm.textWindow = null;
}
}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//文字编辑框样式
.supermap-textedit-content {
background-color: #fff;
cursor: default;
padding: 8px 16px 8px 8px;
position: relative;
input {
border: solid 1px #bdc4ca;
border-radius: 3px;
height: 30px;
padding: 0 4px;
width: 200px;
}
.close {
display: block;
position: absolute;
right: 6px;
top: 6px;
z-index: 1;
.icon {
color: #666;
font-size: 16px;
font-weight: bold;
}
}
}
测算
距离测算线
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
109export function distanceLine(id, vm) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer]);
vm.labelCollection = vm.labelCollection || [];
vm.distanceLabels = vm.distanceLabels || [];
vm.drawDisLine = drawFeature(
vm.smap,
vm.drawLayer,
"Path",
f => {
const ft = f;
const key = GUID();
ft.data.key = key; //添加唯一key
ft.data.type = "distancecal";
// vm.distanceLines.push(ft);//保存要素对象,方便通过绘制图层的removeFeatures方法清除
vm.distanceLabelsCal = [];
vm.distanceTotal = 0;
// 获得图层几何对象
const geometry = ft.geometry;
const root = geometry.components[0];
if (!root) {
return;
}
const l = root.components.length;
if (l < 2) {
return;
}
vm.labelCollection.push(0);
let i = 0;
for (i = 0; i < l; i++) {
if (i === l - 1) {
return;
}
// 开始点结束点
const points = [
new SuperMap.Geometry.Point(
root.components[i].x,
root.components[i].y
),
new SuperMap.Geometry.Point(
root.components[i + 1].x,
root.components[i + 1].y
)
];
const line = new SuperMap.Geometry.LineString(points);
createMeasureSvr(true, line, measureRes => {
const distance = parseFloat(measureRes.result.distance.toFixed(2));
vm.distanceTotal += distance;
const points = JSON.parse(
measureRes.object.options.params.point2Ds.replace(/'/g, '"')
);
const style = superMapDrawStyle();
style.fontColor = "#ff3300";
style.labelAlign = "rt";
// 添加每段长度标签
const point = new SuperMap.Geometry.Point(points[1].x, points[1].y);
const label = new SuperMap.Feature.Vector(point);
label.style = style;
label.style.label = `${distance}米`;
label.data.key = key;
label.data.type = "distancecal";
vm.drawLayer.addFeatures(label);
vm.distanceLabels.push(label);
vm.distanceLabelsCal.push({
point: point,
label: label
});
vm.labelCollection[vm.labelCollection.length - 1] += 1;
if (vm.distanceLabelsCal.length === 1) {
// 第一个添加起点标签
const pointStart = new SuperMap.Geometry.Point(
root.components[0].x,
root.components[0].y
);
const label = new SuperMap.Feature.Vector(pointStart);
const labelStyle = superMapDrawStyle();
labelStyle.fontColor = "#ff3300";
labelStyle.labelAlign = "rb";
labelStyle.label = "起点";
label.style = labelStyle;
label.data.key = key;
label.data.type = "distancecal";
vm.drawLayer.addFeatures(label);
vm.distanceLabels.push(label);
vm.labelCollection[vm.labelCollection.length - 1] += 1;
}
if (vm.distanceLabelsCal.length === l - 1) {
// 更新最后一个标签显示总长度
vm.distanceLabelsCal.map(item => {
if (
item.point.x === root.components[l - 1].x &&
item.point.y === root.components[l - 1].y
) {
item.label.style.label = `${
item.label.style.label
},共${vm.distanceTotal.toFixed(2)}米`;
vm.drawLayer.redraw();
}
});
}
});
}
},
false,
{ multi: true }
);
//vm.drawDisLine.activate()激活
}面积测算(多边形)
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
45export function measureArea(id, vm) {
vm.smap = initMap(id);
vm.drawLayer = initDrawLayer();
vm.baseLayer = initBaseLayer(vm.smap, [vm.drawLayer]);
vm.mapAreaLabels = vm.mapAreaLabels || [];
vm.mapLabelCollection = vm.mapLabelCollection || [];
vm.areaMeasure = drawFeature(
vm.smap,
vm.drawLayer,
"Polygon",
f => {
const ft = f;
const key = GUID();
ft.data.key = key; //添加唯一key
ft.data.type = "areacal";
// 获得图层几何对象
const geometry = ft.geometry;
const root = geometry.components[0];
if (!root) {
return;
}
createMeasureSvr(false, geometry, (measureRes) => {
const area = measureRes.result.area;
console.log(measureRes);
const points = JSON.parse(
measureRes.object.options.params.point2Ds.replace(/'/g, '"')
);
const style = superMapDrawStyle();
style.fontColor = "#ff3300";
style.labelAlign = "rt";
// 添加面积标签
const point = new SuperMap.Geometry.Point(points[0].x, points[0].y);
const label = new SuperMap.Feature.Vector(point);
label.style = style;
label.style.label = `${area.toFixed(2)}平方米`;
label.data.key = key;
label.data.type = "areacal";
vm.drawLayer.addFeatures(label);
vm.mapAreaLabels.push(label);
vm.mapLabelCollection[vm.mapLabelCollection.length - 1] += 1;
})
},
);
//vm.areaMeasure.activate()激活
}
地址匹配
1 | //地址匹配 |
清除(撤销)绘制
marker撒点,
markerLayer.removeMarker(marker)
指定移除,markerLayer.clearMarkers()
清空矢量绘制,
vectorLayer.removeFeatures(Array[feature])
指定移除,vectorLayer.removeAllFeatures()
清空Anchored浮动弹窗,
popWin.destroy()
销毁,销毁不会删除该对象,清空变量需要手动赋值逐步撤销功能需要记录绘制的对象调用对应的移除方法
截图
引入
MapToImg.js
MapToImg.excute(超图实例, "容器父id");
设置预览样式:
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// 超图截图预览
.supermap-screenshot-preview {
.preview-tool {
display: flex;
background-color: #fff;
border: solid 1px #999;
border-radius: 3px;
height: 30px;
overflow: hidden;
position: absolute;
right: 15px;
top: 15px;
z-index: 1;
.button {
background-color: #fff;
border-right: dashed 1px #999;
color: #666;
display: block;
flex-shrink: 0;
font-size: 14px;
height: 30px;
line-height: 30px;
margin: 0;
padding: 0;
text-align: center;
width: 50px;
input {
background-color: rgba(0, 0, 0, 0);
border: none;
}
}
.button:last-child {
border-right-width: 0;
}
.button:hover {
background-color: #eaeaea;
}
}
}
注意:
1.创建底图图层时第四个参数必须要有
useCORS: true
允许跨域2.截图工具是绝对定位,父容器最好相对定位不会变形。
3.超图的z-index尽量小于截图工具的2使工具能覆盖之上
百度地图
1 | // 8、百度地图 |
样式覆盖
1 | // 百度地图样式 |
自定义信息框
InfoWindow样式单一,使用InfoBox可高度定制样式:
1 | //marker为点击撒点的maeker实例 |
需要引入百度地图和InfoBox
地址匹配
1 | const _this = this; |
距离测算
1 | const start = new this.BMap.Point(lngS, latS); |
绘制线条
有两种方式:
使用
PolyLine
类或vue-baidu-map
的bm-polyline
组件,监听线条点击和lineupdate切换编辑状态。监听地图点击增加点,双击结束编辑: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
40clickMap(e) {
if (!this.drawPolylineEdit) return;
if (this.dbTimer) {
//双击,结束编辑,自带的dbclick事件在编辑时失效
clearTimeout(this.dbTimer);
this.dbTimer = null;
return (this.drawPolylineEdit = false);
}
this.dbTimer = setTimeout(() => {
clearTimeout(this.dbTimer);
this.dbTimer = null;
this.drawPolyline.push(e.point);
if (this.drawPolyline.length > 1) {
this.$emit(
"drawFinish",
this.drawPolyline.map(item => {
return [item.lng, item.lat];
})
);
}
this.initDrawPolylineDistances();
}, 200);
},
//点击线开始编辑
clickDrawLine(e) {
this.drawPolylineEdit = true;
e.domEvent.stopPropagation();
},
// 编辑线
updatePolyline(e) {
this.drawPolyline = e.target.getPath();
this.initDrawPolylineDistances();
this.$emit(
"drawFinish",
this.drawPolyline.map(item => {
return [item.lng, item.lat];
})
);
},
// 清空画线使用DrawingManager,绘制管理,除画线外还支持其他矢量图形
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//需要引入百度地图及其DrawingManager.js
export const BMAP_DRAWING_MARKER = "marker";
export const BMAP_DRAWING_POLYLINE = "polyline";
export const BMAP_DRAWING_CIRCLE = "circle";
export const BMAP_DRAWING_RECTANGLE = "rectangle";
export const BMAP_DRAWING_POLYGON = "polygon";
export function drawingManager(map, BMap) {
// 绘制框的样式设置
// http://api.map.baidu.com/library/DrawingManager/1.4/docs/symbols/BMapLib.DrawingManager.html
let styleOptions = {
strokeColor: "", // 边线颜色。
fillColor: "#1989fa", // 填充颜色。当参数为空时,圆形将没有填充效果。
strokeWeight: 2, // 边线的宽度,以像素为单位。
// strokeOpacity: 0.8, //边线透明度,取值范围0 - 1。
fillOpacity: 0.2, // 填充的透明度,取值范围0 - 1。
strokeStyle: "solid" // 边线的样式,solid或dashed。
};
/* global BMapLib */
return new BMapLib.DrawingManager(map, {
isOpen: false, // 是否开启绘制模式
enableDrawingTool: false, // 是否显示工具栏
// drawingType: BMAP_DRAWING_CIRCLE,
enableCalculate: false, // 绘制是否进行测距
drawingToolOptions: {
anchor: "BMAP_ANCHOR_TOP_RIGHT", // 位置
offset: new BMap.Size(5, 5), // 偏离值
scale: 0.8, // 工具栏缩放比例
drawingTypes: [
// BMAP_DRAWING_MARKER, // 画点
// BMAP_DRAWING_CIRCLE, // 画圆
BMAP_DRAWING_POLYLINE // 画线
// BMAP_DRAWING_POLYGON, // 画多边形
// BMAP_DRAWING_RECTANGLE // 画矩形
]
},
rectangleOptions: styleOptions, // 矩形的样式
circleOptions: styleOptions, // 圆形的样式
polygonOptions: styleOptions // 多边形的样式
});
}
//绘制完成事件中,通过overlay.getPath()获取点集
每次增加点需要进行距离测算叠加,在前后两点重点显示距离;为避免标注太多杂乱,可设置小于一定值不显示,若最终都没有显示就取首位两个点的中点显示
其他
缩放等级
使用百度地图过程中,发现地图的最大缩放等级最多为19,20m级,怎么设置maxZoom都没用;但是官方示例中却能缩放到5m级?
可以设置全局变量解决:
1 | //定义后默认最大缩放等级就是maxZoom,不同实例可以自行再加限制 |
一般情况下不要超过20,底图会是空白。
思极地图
js api文档:https://lbs.sgmap.cn/products/docs/#/guide/quick-quickstart
导入样式和js文件
1
2<link rel="stylesheet" type="text/css" href="https://lbs.sgmap.cn/api/epgis-1.5.0.css" />
<script src="https://lbs.sgmap.cn/api/epgis-js-1.5.0.min.js"></script>
公共
1 | // 思极地图账号 |
示例
1 | //设置中心后立即改变缩放会失效,需要使用定时器间隔 |
其他
样式注意
1 | // 思极地图隐藏自带logo |
地图坐标转换
各方定位经纬度格式有区别,可能造成定位偏移不准确,需要判断经纬度格式并进行转换
可以使用coordtransform.js
设备中获取经纬度(GPS)
1 | //百度sdk默认是DB-09,也可以是GCJ-02 |
互联网在线地图
1 | GCJ-02: |
- 本文作者: MR-QXJ
- 本文链接: https://mr-qxj.github.io/2021/06/07/其他/地图总结/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!