作者热门文章
- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章openlayers实现地图测距测面由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例为大家分享了openlayers实现地图测距测面的具体代码,供大家参考,具体内容如下 。
项目背景vue-cli3.0 。
public下html需要引入文件 。
1
2
|
<
link
rel
=
"stylesheet"
href="<%= BASE_URL %>./css/gr-ol.css" type="text/css">
<
script
src="<%= BASE_URL %>./js/ol.js" type="text/javascript"></
script
>
|
这里地图为公共组件,方法写在公共组件的init方法里,kpst._this为地图对象 。
调用 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
//测距/面
var
draw = me.map._this.interactions.getArray()[10]
me.map._this.removeInteraction(draw);
if
(data.name ==
'测距'
|| data.name ==
'测面'
) {
me.map._this.interactions.array_ = arr.slice(0, 10)
if
(data.name ==
'测距'
) {
me.mtype =
'LineString'
}
else
{
me.mtype =
'Polygon'
}
me.map._this.measure(me.mtype)
//map已挂载到vue原型Vue.prototype.map = map
}
else
if
(data.name ==
'清除'
) {
me.map._this.clear()
}
|
方法挂载 。
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
|
// 测距、面
//创建一个当前要绘制的对象
var
sketch
//创建一个帮助提示框对象
var
helpTooltipElement;
//创建一个帮助提示信息对象
var
helpTooltip;
//创建一个测量提示框对象
var
measureTooltipElement;
//创建一个测量提示信息对象
var
measureTooltip;
//继续绘制多边形的提示信息
var
continuePolygonMsg
//继续绘制线段的提示信息
var
continueLineMsg
//帮助提示信息
var
helpMsg
//定义矢量数据源
var
source =
new
ol.source.Vector();
//定义矢量图层
var
vector =
new
ol.layer.Vector({
source: source,
style:
new
ol.style.Style({
fill:
new
ol.style.Fill({
color:
'rgba(255,255,255,0.2)'
}),
stroke:
new
ol.style.Stroke({
color:
'#e21e0a'
,
width: 2
}),
image:
new
ol.style.Circle({
radius: 5,
fill:
new
ol.style.Fill({
color:
'#ffcc33'
})
})
})
});
//创建比例尺控件
var
scaleLineControl =
new
ol.control.ScaleLine({
units:
'metric'
,
target:
'scalebar'
,
className:
'ol-scale-line'
});
function
measure(mtype) {
sketch =
new
ol.Feature();
// continuePolygonMsg = 'Click to continue drawing the polygon';
// continueLineMsg = 'Click to continue drawing the line';
//将矢量图层添加到地图中
kpst._this.removeLayer(vector);
kpst._this.addLayer(vector);
//添加比例尺控件
kpst._this.removeControl(scaleLineControl);
kpst._this.addControl(scaleLineControl);
//鼠标移动触发的函数
var
pointerMoveHandler =
function
(evt) {
//如果是平移地图则直接结束
if
(evt.dragging) {
return
;
}
//帮助提示信息
helpMsg =
'Click to start drawing'
;
if
(sketch) {
//获取绘图对象的几何要素
var
geom = sketch.getGeometry();
//如果当前绘制的几何要素是多线段,则将绘制提示信息设置为多线段绘制提示信息
// if (geom instanceof ol.geom.Polygon) {
// helpMsg = continuePolygonMsg;
// } else if (geom instanceof ol.geom.LineString) {
// helpMsg = continueLineMsg;
// }
}
//设置帮助提示要素的内标签为帮助提示信息
// if (helpTooltipElement)
// helpTooltipElement.innerHTML = helpMsg;
//设置帮助提示信息的位置
// if (helpTooltip)
helpTooltip.setPosition(evt.coordinate);
//移除帮助提示要素的隐藏样式
// $(helpTooltipElement).removeClass('hidden');
removeClass(document.getElementsByClassName(
'tooltip'
)[0],
'hidden'
)
};
//触发pointermove事件
kpst._this.on(
'pointermove'
, pointerMoveHandler);
//当鼠标移除地图视图的时为帮助提示要素添加隐藏样式
document.querySelector(
'.ol-viewport'
).onmouseout =
function
() {
addClass(document.getElementsByClassName(
'tooltip'
)[0],
'hidden'
)
}
// 判断class有无
function
hasClass(ele, cls) {
if
(ele) {
cls = cls ||
''
;
if
(cls.replace(/\s/g,
''
).length == 0)
return
false
;
//当cls没有参数时,返回false
return
new
RegExp(
' '
+ cls +
' '
).test(
' '
+ ele.className +
' '
);
}
}
//添加class
function
addClass(ele, cls) {
if
(!hasClass(ele, cls) && ele) {
ele.className = ele.className ==
''
? cls : ele.className +
' '
+ cls;
}
}
// 去除class
function
removeClass(ele, cls) {
if
(hasClass(ele, cls) && ele) {
var
newClass =
' '
+ ele.className.replace(/[\t\r\n]/g,
''
) +
' '
;
while
(newClass.indexOf(
' '
+ cls +
' '
) >= 0) {
newClass = newClass.replace(
' '
+ cls +
' '
,
' '
);
}
ele.className = newClass.replace(/^\s+|\s+$/g,
''
);
}
}
//定义一个交互式绘图对象
var
draw;
//添加交互式绘图对象的函数
function
addInteraction() {
//创建一个交互式绘图对象
draw =
new
ol.interaction.Draw({
//绘制的数据源
source: source,
//绘制类型
type: mtype,
//样式
style:
new
ol.style.Style({
fill:
new
ol.style.Fill({
color:
'rgba(255,255,255,0.2)'
}),
stroke:
new
ol.style.Stroke({
color:
'rgba(0,0,0,0.5)'
,
lineDash: [10, 10],
width: 2
}),
image:
new
ol.style.Circle({
radius: 5,
stroke:
new
ol.style.Stroke({
color:
'rgba(0,0,0,0.7)'
}),
fill:
new
ol.style.Fill({
color:
'rgba(255,255,255,0.2)'
})
})
})
});
//将交互绘图对象添加到地图中
kpst._this.addInteraction(draw);
//创建测量提示框
createMeasureTooltip();
//创建帮助提示框
createHelpTooltip();
//定义一个事件监听
var
listener;
//定义一个控制鼠标点击次数的变量
var
count = 0;
//绘制开始事件
draw.on(
'drawstart'
,
function
(evt) {
//The feature being drawn.
sketch = evt.feature;
//提示框的坐标
var
tooltipCoord = evt.coordinate;
//监听几何要素的change事件
//Increases the revision counter and dispatches a 'change' event.
listener = sketch.getGeometry().on(
'change'
,
function
(evt) {
//The event target.
//获取绘制的几何对象
var
geom = evt.target;
//定义一个输出对象,用于记录面积和长度
var
output;
if
(geom
instanceof
ol.geom.Polygon) {
kpst._this.removeEventListener(
'singleclick'
);
kpst._this.removeEventListener(
'dblclick'
);
//输出多边形的面积
output = formatArea(geom);
//Return an interior point of the polygon.
//获取多变形内部点的坐标
tooltipCoord = geom.getInteriorPoint().getCoordinates();
}
else
if
(geom
instanceof
ol.geom.LineString) {
//输出多线段的长度
output = formatLength(geom);
//Return the last coordinate of the geometry.
//获取多线段的最后一个点的坐标
tooltipCoord = geom.getLastCoordinate();
}
//设置测量提示框的内标签为最终输出结果
// if (measureTooltipElement)
measureTooltipElement.innerHTML = output;
//设置测量提示信息的位置坐标
// if (measureTooltip)
measureTooltip.setPosition(tooltipCoord);
});
//地图单击事件
kpst._this.on(
'singleclick'
,
function
(evt) {
//设置测量提示信息的位置坐标,用来确定鼠标点击后测量提示框的位置
// if (measureTooltip)
measureTooltip.setPosition(evt.coordinate);
//如果是第一次点击,则设置测量提示框的文本内容为起点
if
(count == 0 && measureTooltipElement) {
measureTooltipElement.innerHTML =
"起点"
;
}
//根据鼠标点击位置生成一个点
var
point =
new
ol.geom.Point(evt.coordinate);
//将该点要素添加到矢量数据源中
source.addFeature(
new
ol.Feature(point));
//更改测量提示框的样式,使测量提示框可见
measureTooltipElement.className =
'tooltip tooltip-static'
;
//创建测量提示框
createMeasureTooltip();
//点击次数增加
count++;
});
//地图双击事件
kpst._this.on(
'dblclick'
,
function
(evt) {
//根据
var
point =
new
ol.geom.Point(evt.coordinate);
source.addFeature(
new
ol.Feature(point));
});
},
this
);
//绘制结束事件
draw.on(
'drawend'
,
function
(evt) {
count = 0;
//设置测量提示框的样式
measureTooltipElement.className =
'tooltip tooltip-static'
;
//Set the offset for this overlay.
//设置偏移量
measureTooltip.setOffset([0, -7]);
//清空绘制要素
sketch =
null
;
//清空测量提示要素
measureTooltipElement =
null
;
//创建测量提示框
createMeasureTooltip();
//Removes an event listener using the key returned by on() or once().
//移除事件监听
ol.Observable.unByKey(listener);
//移除地图单击事件
kpst._this.removeEventListener(
'singleclick'
);
},
this
);
}
//创建帮助提示框
function
createHelpTooltip() {
//如果已经存在帮助提示框则移除
if
(helpTooltipElement) {
helpTooltipElement.parentNode.removeChild(helpTooltipElement);
}
//创建帮助提示要素的div
if
(!helpTooltipElement)
helpTooltipElement = document.createElement(
'div'
);
//设置帮助提示要素的样式
helpTooltipElement.className =
'tooltip hidden'
;
//创建一个帮助提示的覆盖标注
helpTooltip =
new
ol.Overlay({
element: helpTooltipElement,
offset: [15, 0],
positioning:
'center-left'
});
//将帮助提示的覆盖标注添加到地图中
kpst._this.addOverlay(helpTooltip);
}
//创建测量提示框
function
createMeasureTooltip() {
//创建测量提示框的div
// if (!measureTooltipElement)
measureTooltipElement = document.createElement(
'div'
);
measureTooltipElement.setAttribute(
'id'
,
'lengthLabel'
);
//设置测量提示要素的样式
measureTooltipElement.className =
'tooltip tooltip-measure'
;
//创建一个测量提示的覆盖标注
measureTooltip =
new
ol.Overlay({
element: measureTooltipElement,
offset: [0, -15],
positioning:
'bottom-center'
});
//将测量提示的覆盖标注添加到地图中
kpst._this.addOverlay(measureTooltip);
}
//格式化测量长度
var
formatLength =
function
(line) {
//定义长度变量
var
length;
//计算平面距离
length = Math.round(line.getLength() * 100) / 100;
//定义输出变量
var
output;
//如果长度大于1000,则使用km单位,否则使用m单位
if
(length > 100) {
output = (Math.round(length / 1000 * 100) / 100) +
' '
+
'km'
;
//换算成KM单位
}
else
{
output = (Math.round(length * 100) / 100) +
' '
+
'm'
;
//m为单位
}
return
output;
};
//格式化测量面积
var
formatArea =
function
(polygon) {
//定义面积变量
var
area;
//获取平面面积
area = polygon.getArea();
// }
//定义输出变量
var
output;
//当面积大于10000时,转换为平方千米,否则为平方米
if
(area > 1000) {
output = (Math.round(area / 1000000 * 100) / 100) +
' '
+
'km<sup>2</sup>'
;
}
else
{
output = (Math.round(area * 100) / 100) +
' '
+
'm<sup>2</sup>'
;
}
return
output;
};
//添加交互绘图对象
addInteraction();
}
// 清除提示对象
function
clear() {
source.clear()
kpst._this.getOverlays().clear();
kpst._this.removeLayer(vector);
kpst._this.removeControl(scaleLineControl);
}
kpst._this.measure = measure
kpst._this.clear = clear
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://www.cnblogs.com/wwj007/p/11526159.html 。
最后此篇关于openlayers实现地图测距测面的文章就讲到这里了,如果你想了解更多关于openlayers实现地图测距测面的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我是一名优秀的程序员,十分优秀!