网络编程 
首页 > 网络编程 > 浏览文章

openlayers实现地图测距测面

(编辑:jimmy 日期: 2024/5/8 浏览:3 次 )

本文实例为大家分享了openlayers实现地图测距测面的具体代码,供大家参考,具体内容如下

项目背景vue-cli3.0

public下html需要引入文件

<link rel="stylesheet" href="<%= BASE_URL %>./css/gr-ol.css" type="text/css">
<script src="/UploadFiles/2021-04-02/ol.js">

这里地图为公共组件,方法写在公共组件的init方法里,kpst._this为地图对象

调用

//测距/面
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()
  }

方法挂载

// 测距、面
//创建一个当前要绘制的对象
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 == '' "起点";
   }
   //根据鼠标点击位置生成一个点
   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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:openlayers实现地图弹窗
下一篇:openlayers 3实现车辆轨迹回放
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网