测量功能
距离的测量是根据鼠标在地图上绘制的点,实时计算出两点之间的实际距离,面积的测量是根据鼠标绘制的范围,通过地理坐标系的转换而计算出实际面积大小。 本博客参考:https://blog.csdn.net/SmileCoffin/article/details/64122338 在此基础上添加了清除功能
代码实现
<!DOCTYPE html
>
<html xmlns
="http://www.w3.org/1999/xhtml">
<head
>
<meta http
-equiv
="Content-Type" content
="text/html; charset=utf-8"/>
<title
></title
>
<script src
="E:/Test_Release/gis/ol/ol.js"></script
>
<link href
="E:/Test_Release/gis/ol/ol.css" rel
="stylesheet" />
<script src
="E:/Test_Release/gis/ol/jquery.min.js"></script
>
<link href
="E:/Test_Release/gis/ol/bootstrap.min.css" rel
="stylesheet" />
<script src
="https://openlayers.org/en/v3.20.1/build/ol.js" type
="text/javascript"></script
>
<script src
="E:/Test_Release/gis/ol/bootstrap-3.3.7.min.js"></script
>
<!DOCTYPE html
>
<html xmlns
="http://www.w3.org/1999/xhtml">
<head
>
<style type
="text/css">
#map
{
width
: 100%;
height
: 100%;
position
: absolute
;
}
#menu
{
float
: left
;
position
: absolute
;
bottom
: 10px
;
left
: 10px
;
z
-index
: 2000;
}
.checkbox
{
left
: 20px
;
}
.tooltip
{
position
: relative
;
background
: rgba(0, 0, 0, 0.5);
border
-radius
: 4px
;
color
: white
;
padding
: 4px
8px
;
opacity
: 0.7;
white
-space
: nowrap
;
}
.tooltip
-measure
{
opacity
: 1;
font
-weight
: bold
;
}
.tooltip
-static {
background
-color
: #ffffff
;
color
: black
;
border
: 1px solid white
;
}
.tooltip
-measure
:before
,
.tooltip
-static:before
{
border
-top
: 6px solid
rgba(0, 0, 0, 0.5);
border
-right
: 6px solid transparent
;
border
-left
: 6px solid transparent
;
content
: "";
position
: absolute
;
bottom
: -6px
;
margin
-left
: -7px
;
left
: 50%;
}
.tooltip
-static:before
{
border
-top
-color
: #ffffff
;
}
#scalebar
{
float
: left
;
margin
-bottom
: 10px
;
}
</style
>
<script type
="text/javascript">
var map
;
var vector
;
function clearMeasure() {
vector
.getSource().clear();
$("div.tooltip-static").remove();
}
$(function () {
var tian_di_tu
= new ol.layer.Tile({
source
:new ol.source.OSM()
});
map
= new ol.Map({
target
: 'map',
layers
: [
tian_di_tu
],
view
: new ol.View({
center
: new ol.proj.fromLonLat([114.4250, 23.0890]),
zoom
: 18,
maxZoom
: 20
})
});
var source
= new ol.source.Vector();
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'
})
})
})
});
map
.addLayer(vector
);
var scaleLineControl
= new ol.control.ScaleLine({
units
: 'metric',
target
: 'scalebar',
className
: 'ol-scale-line'
});
map
.addControl(scaleLineControl
);
var wgs84Sphere
= new ol.Sphere(6378137);
var sketch
= new ol.Feature();
var helpTooltipElement
;
var helpTooltip
;
var measureTooltipElement
;
var measureTooltip
;
var continuePolygonMsg
= 'Click to continue drawing the polygon';
var continueLineMsg
= 'Click to continue drawing the line';
var pointerMoveHandler = function (evt
) {
if (evt
.dragging
) {
return;
}
var 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
;
}
}
helpTooltipElement
.innerHTML
= helpMsg
;
helpTooltip
.setPosition(evt
.coordinate
);
$(helpTooltipElement
).removeClass('hidden');
};
map
.on('pointermove', pointerMoveHandler
);
$(map
.getViewport()).on('mouseout', function () {
$(helpTooltipElement
).addClass('hidden');
});
var geodesicCheckbox
= document
.getElementById('geodesic');
var typeSelect
= document
.getElementById('type');
var draw
;
function addInteraction() {
var type
= typeSelect
.value
== 'area' ? 'Polygon' : 'LineString';
var draw
= new ol.interaction.Draw({
source
: source
,
type
: type
,
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)'
})
})
})
});
map
.addInteraction(draw
);
createMeasureTooltip();
createHelpTooltip();
var listener
;
var count
= 0;
draw
.on('drawstart', function (evt
) {
sketch
= evt
.feature
;
var tooltipCoord
= evt
.coordinate
;
listener
= sketch
.getGeometry().on('change', function (evt
) {
var geom
= evt
.target
;
var output
;
if (geom
instanceof ol.geom.Polygon) {
map
.removeEventListener('singleclick');
map
.removeEventListener('dblclick');
output
= formatArea(geom
);
tooltipCoord
= geom
.getInteriorPoint().getCoordinates();
} else if (geom
instanceof ol.geom.LineString) {
output
= formatLength(geom
);
tooltipCoord
= geom
.getLastCoordinate();
}
measureTooltipElement
.innerHTML
= output
;
measureTooltip
.setPosition(tooltipCoord
);
});
map
.on('singleclick', function (evt
) {
measureTooltip
.setPosition(evt
.coordinate
);
if (count
== 0) {
measureTooltipElement
.innerHTML
= "起点";
}
var point
= new ol.geom.Point(evt
.coordinate
);
source
.addFeature(new ol.Feature(point
));
measureTooltipElement
.className
= 'tooltip tooltip-static';
createMeasureTooltip();
count
++;
});
map
.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';
measureTooltip
.setOffset([0, -7]);
sketch
= null;
measureTooltipElement
= null;
createMeasureTooltip();
ol
.Observable
.unByKey(listener
);
map
.removeEventListener('singleclick');
}, this);
}
function createHelpTooltip() {
if (helpTooltipElement
) {
helpTooltipElement
.parentNode
.removeChild(helpTooltipElement
);
}
helpTooltipElement
= document
.createElement('div');
helpTooltipElement
.className
= 'tooltip hidden';
helpTooltip
= new ol.Overlay({
element
: helpTooltipElement
,
offset
: [15, 0],
positioning
:'center-left'
});
map
.addOverlay(helpTooltip
);
}
function createMeasureTooltip() {
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'
});
map
.addOverlay(measureTooltip
);
}
typeSelect
.onchange = function() {
map
.removeInteraction(draw
);
addInteraction();
};
var formatLength = function (line
) {
var length
;
if (geodesicCheckbox
.checked
) {
var coordinates
= line
.getCoordinates();
length
= 0;
var sourceProj
= map
.getView().getProjection();
for (var i
= 0; i
< coordinates
.length
- 1; i
++) {
var c1
= ol
.proj
.transform(coordinates
[i
], sourceProj
, 'EPSG:4326');
var c2
= ol
.proj
.transform(coordinates
[i
+ 1], sourceProj
, 'EPSG:4326');
length
+= wgs84Sphere
.haversineDistance(c1
,c2
);
}
} else {
length
= Math
.round(line
.getLength() * 100) / 100;
}
var output
;
if (length
> 1000) {
output
= (Math
.round(length
/ 1000 * 100) / 100) + ' ' + 'km';
} else {
output
= (Math
.round(length
* 100) / 100) + ' ' + 'm';
}
return output
;
};
var formatArea = function (polygon
) {
var area
;
if (geodesicCheckbox
.checked
) {
var sourceProj
= map
.getView().getProjection();
var geom
= polygon
.clone().transform(sourceProj
, 'EPSG:4326');
var coordinates
= geom
.getLinearRing(0).getCoordinates();
area
= Math
.abs(wgs84Sphere
.geodesicArea(coordinates
));
} else {
area
= polygon
.getArea();
}
var output
;
if (area
> 10000) {
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();
});
</script
>
</head
>
<body
>
<div id
="map">
<div id
="menu">
<label
>测量类型选择
</label
>
<button onclick
="clearMeasure()">清除
</button
>
<select id
="type">
<option value
="length">长度
</option
>
<option value
="area">面积
</option
>
</select
>
<label
class="checkbox"><input type
="checkbox" id
="geodesic" />使用大地测量
</label
>
</div
>
</div
>
<div id
="scalebar"></div
>
</body
>
</html
>
结果展示
转载请注明原文地址: https://lol.8miu.com/read-28039.html