Openlayers實(shí)現(xiàn)距離面積測(cè)量
本文實(shí)例為大家分享了Openlayers實(shí)現(xiàn)距離面積測(cè)量的具體代碼,供大家參考,具體內(nèi)容如下
CSS
.ol-tooltip {
position: relative;
background: rgba(0, 0, 0, 0.5);
border-radius: 4px;
color: white;
padding: 4px 8px;
opacity: 0.7;
white-space: nowrap;
font-size: 12px;
}
.ol-tooltip-measure {
opacity: 1;
font-weight: bold;
}
.ol-tooltip-static {
background-color: #ffcc33;
color: black;
border: 1px solid white;
}
.ol-tooltip-measure:before,
.ol-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%;
}
.ol-tooltip-static:before {
border-top-color: #ffcc33;
}
具體實(shí)現(xiàn)
let layer_1 =new ol.layer.Tile({
source: new ol.source.OSM()
});
let source = new ol.source.Vector();
let 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: '#ffcc33',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: '#ffcc33'
})
})
})
});
let map=new ol.Map({
layers: [
layer_1 ,vector
],
view: new ol.View({
center: [-11000000, 4600000],
zoom: 5,
}),
target: 'map'
});
let count=0;
let draw;
let sketch=new ol.Feature();
let listener;
let helpTooltipElement;
let helpTooltip;
let measureTooltipElement;
let measureTooltip;
let continuePolygonMsg="繼續(xù)點(diǎn)擊繪制多邊形";
let continueLineMsg="繼續(xù)點(diǎn)擊繪制線";
createMeasureTooltip();
createHelpTooltip();
let pointerMoveHandler=function(evt){
if (evt.dragging) {
return;
}
/** @type {string} */
let helpMsg = 'Click to start drawing';
if (sketch) {
let geom = (sketch.getGeometry());
if (geom instanceof ol.geom.Polygon) {
helpMsg = continuePolygonMsg;
} else if (geom instanceof ol.geom.LineString) {
helpMsg = continueLineMsg;
}
}
helpTooltipElement.classList.remove('hidden');
};
map.on('pointermove', pointerMoveHandler);
map.getViewport().addEventListener('mouseout', function() {
});
let formatLength=function (line) {
let length = ol.sphere.getLength(line);
let output;
if(length>100){
output=(Math.round(length/1000*100)/100)+''+'km'
}else{
output=(Math.round(length*100)/100)+''+'m'
}
return output;
};
let formatArea=function (polygon) {
let area = ol.sphere.getArea(polygon);
let 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;
};
function addInteraction(){
let type="Polygon";
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)'
})
})
}),
snapTolerance:50
});
map.addInteraction(draw);
map.on('singleclick',function (evt) {
measureTooltip.setPosition(evt.coordinate);
if(count==0){
measureTooltipElement.innerHTML='起點(diǎn)'
}else{
measureTooltipElement.className = 'ol-tooltip ol-tooltip-static';
measureTooltip.setOffset([0, -20]);
// unset sketch
sketch = null;
// unset tooltip so that a new one can be created
measureTooltipElement = null;
createMeasureTooltip();
//map.removeInteraction(draw);
}
createMeasureTooltip();
//點(diǎn)擊次數(shù)增加
count++;
});
draw.on('drawstart',function (evt) {
sketch=evt.feature;
let tooltipCoord=evt.coordinate;
listener=sketch.getGeometry().on('change',function (evt) {
let geom=evt.target;
let output;
if(geom instanceof ol.geom.Polygon){
map.removeEventListener('singleclick');
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);
})
});
draw.on('drawend',
function() {
measureTooltipElement.className = 'ol-tooltip ol-tooltip-static';
measureTooltip.setOffset([0, -7]);
// unset sketch
sketch = null;
// unset tooltip so that a new one can be created
measureTooltipElement = null;
createMeasureTooltip();
map.removeInteraction('singleclick');
count=0;
ol.Observable.unByKey(listener);
});
}
function createHelpTooltip() {
if (helpTooltipElement) {
helpTooltipElement.parentNode.removeChild(helpTooltipElement);
}
helpTooltipElement = document.createElement('div');
helpTooltipElement.className = 'ol-tooltip hidden';
helpTooltip = new ol.Overlay({
element: helpTooltipElement,
offset: [15, 0],
positioning: 'center-left'
});
map.addOverlay(helpTooltip);
}
/**
* Creates a new measure tooltip
*/
function createMeasureTooltip() {
if (measureTooltipElement) {
measureTooltipElement.parentNode.removeChild(measureTooltipElement);
}
measureTooltipElement = document.createElement('div');
measureTooltipElement.className = 'ol-tooltip ol-tooltip-measure';
measureTooltip = new ol.Overlay({
element: measureTooltipElement,
offset: [-30, -30],
positioning: 'center-center'
});
map.addOverlay(measureTooltip);
}
/**
* Let user change the geometry type.
*/
addInteraction();
在線引用地址
<link rel="stylesheeth" ref="https://openlayers.org/en/v5.3.0/css/ol.css" type="text/css"> <script src="https://openlayers.org/en/v5.3.0/build/ol.js"></script>
多了一個(gè)singleclick來用于顯示距離測(cè)量時(shí)每個(gè)線段節(jié)點(diǎn)到起點(diǎn)的距離
繪制類型我寫死為Polygon了 注意自己修改一下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
原生JS封裝animate運(yùn)動(dòng)框架的實(shí)例
下面小編就為大家?guī)硪黄鶭S封裝animate運(yùn)動(dòng)框架的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
JS沙箱繞過以及競(jìng)爭(zhēng)條件型漏洞復(fù)現(xiàn)
沙箱繞過"是指攻擊者利用各種方法和技術(shù)來規(guī)避或繞過應(yīng)用程序或系統(tǒng)中的沙箱,本文主要介紹了JS沙箱繞過以及競(jìng)爭(zhēng)條件型漏洞復(fù)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
使用js在layui中實(shí)現(xiàn)上傳圖片壓縮
這篇文章主要介紹了使用js在layui中實(shí)現(xiàn)上傳圖片壓縮,layui 是一款采用自身模塊規(guī)范編寫的前端 UI 框架,js上傳圖片壓縮百度有很多方法,,需要的朋友可以參考下2019-06-06
js數(shù)字轉(zhuǎn)換為float,取N位小數(shù)
在javascript中不分單精度float和雙精度double,凡事有小數(shù)的變量都認(rèn)為是float,因此要取小數(shù)后的n位,要用方法toFixed(n)來得到2014-02-02
使用js檢測(cè)瀏覽器是否支持html5中的video標(biāo)簽的方法
這篇文章主要介紹了使用js檢測(cè)瀏覽器是否支持html5中的video標(biāo)簽的方法,需要的朋友可以參考下2014-03-03
深入剖析JavaScript中Geolocation?API的使用
這篇文章主要來和大家一起深入探討?JavaScript?的?Geolocation?API,看看它的強(qiáng)大之處以及如何在你的項(xiàng)目中應(yīng)用它,感興趣的可以了解下2024-03-03
深入淺出理解JavaScript高級(jí)定時(shí)器原理與用法
這篇文章主要介紹了JavaScript高級(jí)定時(shí)器原理與用法,結(jié)合實(shí)例形式分析了javascript重復(fù)定時(shí)器相關(guān)問題與解決方法,并描述了函數(shù)節(jié)流的原理與相關(guān)操作方法,需要的朋友可以參考下2018-08-08

