openlayers4.6.5实现距离量测和面积量测


Posted in Javascript onSeptember 25, 2020

本文实例为大家分享了openlayers4.6.5实现距离量测和面积量测的具体代码,供大家参考,具体内容如下

版本: openlayers4.6.5

效果图:

openlayers4.6.5实现距离量测和面积量测

小插曲:

原本使用ol官方提供的 量测例子,就挺不错的。但是由于放在项目中后。量测样式不知道为啥出不来,找了半天原因 也没有找到,单独在一个html中完全没问题。所以推测可能和项目中哪些地方有冲突,但是问题暂时没找出来,项目也比较急,所以只能自己实现文字标注部门的样式,实现效果如上图gif所示。

实现原理:

量测功能还是使用了ol例子提供的源码,修改部分主要是在标注这一块,另外就是时刻去添加这个标注 然后时刻删除这个标注 就可以了。

完整的js代码如下(鼠标样式图标 我没放上来,有需要的我给你发邮箱):

var draw;
var click=false;
var output=0;
var vector;
var source;
var lastPolygonLabelFeature;//记录上一个面标注要素 
var lastLengthLabelFeature;//记录上一个点标注要素
$(
 function(){
 $("#measureDistance").click(function(){
 if(draw){
 map.removeInteraction(draw); 
 }
 addInteraction("length");
 setMeasureCur();
 })
 $("#measureArea").click(function(){
 if(draw){
 map.removeInteraction(draw); 
 }
 addInteraction("area"); 
 setMeasureCur();
 })
 $("#measureClear").click(function(){
 map.removeInteraction(draw); 
 vector.setSource(null);
 source=new ol.source.Vector();
 vector.setSource(source);
 lastPolygonLabelFeature=null;
 lastLengthLabelFeature=null;
 click=false;
 
 sketch = null;
 output="0";
 
 reSetCur();
 })
 function setMeasureCur(){
 $('#map').css({
 cursor:"url(../../static/images/measureIcon/measure.cur), auto"
 });
 }
 
 function reSetCur(){
 $('#map').css('cursor','default');
 }
 
 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, 0, 0, 0.1)'
 }),
 stroke: new ol.style.Stroke({
 color: 'rgb(255,116,3)',
 width: 2
 }),
 image: new ol.style.Circle({
 radius: 3,
 stroke: new ol.style.Stroke({
 color: 'rgba(255, 0, 0,1)',
 width: 2
 }),
 fill: new ol.style.Fill({
 color: 'rgba(255,255,255)'
 })
 
 })
 })
 });
 map.addLayer(vector);
 
 var sketch;
 
 var pointerMoveHandler = function(evt) {
 if (evt.dragging) {
 return;
 }
 var Coord;
 
 if(sketch){
 var geom = sketch.getGeometry();
 if (geom instanceof ol.geom.Polygon) {
 
 if(lastPolygonLabelFeature){
  //鼠标移动 不停的添加和删除
 source.removeFeature(lastPolygonLabelFeature);
 }
 
 Coord = geom.getInteriorPoint().getCoordinates();
 
 //新建一个要素ol.Feature
 var newFeature = new ol.Feature({
  geometry: new ol.geom.Point(Coord), //几何信息
  name: output
 });
 lastPolygonLabelFeature=newFeature; 
 newFeature.setStyle(createLabelStyle(newFeature,0,0)); 
 } else if (geom instanceof ol.geom.LineString) {
 if(lastLengthLabelFeature){
 source.removeFeature(lastLengthLabelFeature);
 }
 
 Coord = geom.getLastCoordinate();
 //新建一个要素ol.Feature
 var newFeature = new ol.Feature({
  geometry: new ol.geom.Point(Coord), //几何信息
  name: output
 });
 lastLengthLabelFeature=newFeature;
 newFeature.setStyle(createLabelStyle(newFeature,35,-10)); 
 }
 //设置要素样式
 source.addFeature(newFeature);
 }
 };
 
 map.on('pointermove', pointerMoveHandler);
 map.on('click', function(evt){
 var coordinate = evt.coordinate; //鼠标单击点的坐标
 console.log(coordinate);
 if(output=="0"){
 lastPolygonLabelFeature=null;
 if(lastLengthLabelFeature){
 source.removeFeature(lastLengthLabelFeature);
 lastLengthLabelFeature=null;
 }
 return;
 }
 
 var Coord;
 if(sketch){
 var geom = sketch.getGeometry();
 if (geom instanceof ol.geom.Polygon) {
 
 if(lastPolygonLabelFeature){
 source.removeFeature(lastPolygonLabelFeature);
 }
 Coord = geom.getInteriorPoint().getCoordinates();
 
 //新建一个要素ol.Feature
 var newFeature = new ol.Feature({
  geometry: new ol.geom.Point(Coord), //几何信息
  name: output
 });
 lastPolygonLabelFeature=newFeature;
 newFeature.setStyle(createLabelStyle(newFeature,0,0)); //设置要素样式
 source.addFeature(newFeature);
 
 
 } else if (geom instanceof ol.geom.LineString) {
 
 Coord = geom.getLastCoordinate();
 //新建一个要素ol.Feature
 var newFeature = new ol.Feature({
  geometry: new ol.geom.Point(Coord), //几何信息
  name: output
 });
 newFeature.setStyle(createLabelStyle(newFeature,35,-10)); //设置要素样式
 source.addFeature(newFeature);
 }
 
 var pointFeature = new ol.Feature({
 geometry: new ol.geom.Point(coordinate), //几何信息
 name: output
 });
 source.addFeature(pointFeature);
 }
 
 });
 
 //矢量标注样式设置函数,设置image为图标ol.style.Icon
 function createLabelStyle(feature,offsetX,offsetY){
 return new ol.style.Style({
// image: new ol.style.Icon({
//  anchor: [0.5, 60], //锚点
//  anchorOrigin:'top-right', //锚点源
//  anchorXUnits: 'fraction', //锚点X值单位
//  anchorYUnits: 'pixels', //锚点Y值单位
//  offsetOrigin: 'top-right', //偏移原点
//  opacity: 0.75,
//  src: 'OL3Demo/images/label/blueIcon.png' //图标的URL
// }),
 text: new ol.style.Text({
  textAlign: 'center', //位置
  textBaseline: 'middle', //基准线
  font: 'normal 10px sans-serif', //文字样式
  text: feature.get('name'), //文本内容
  fill: new ol.style.Fill({ //文本填充样式(即文字颜色)
  color: 'white'
  }),
  stroke: new ol.style.Stroke({
  color: 'black', 
  width: 5
  }),
  offsetX:offsetX,
  offsetY:offsetY
 
 })
 });
 }
 
 
 
 function addInteraction(drawType) {
 var type = (drawType== 'area' ? 'Polygon' : 'LineString');
 draw = new ol.interaction.Draw({
 source: source,
 type: type,
 style: new ol.style.Style({
 fill: new ol.style.Fill({
 color: 'rgba(255, 0, 0, 0.2)'
 }),
 stroke: new ol.style.Stroke({
 color: 'rgb(255,116,3)',
// lineDash: [10, 10],//虚线
 width: 2
 }),
 image: new ol.style.Circle({
 radius: 5,
 stroke: new ol.style.Stroke({
 color: 'rgba(255, 0, 0, 0.1)'
 }),
 fill: new ol.style.Fill({
 color: 'rgba(255,116,3, 0.3)'
 })
 })
 })
 });
 map.addInteraction(draw);
 
 var listener;
 draw.on('drawstart',
 function(evt) {
 // set sketch
 sketch = evt.feature;
 listener = sketch.getGeometry().on('change', function(evt) {
 var geom = evt.target;
 
 if (geom instanceof ol.geom.Polygon) {
  output = formatArea(geom);
 } else if (geom instanceof ol.geom.LineString) {
  output = formatLength(geom);
 }
 
 });
 }, this);
 
 draw.on('drawend',
 function() {
 // unset sketch
 sketch = null;
 ol.Observable.unByKey(listener);
 output="0";
 }, this);
 }
 
 var formatLength = function(line) {
 var length = ol.Sphere.getLength(line);
 var output;
 if (length > 100) {
 output = (Math.round(length / 1000 * 100) / 100) +
 ' ' + '千米';
 } else {
 output = (Math.round(length * 100) / 100) +
 ' ' + '米';
 }
 return output;
 };
 
 var formatArea = function(polygon) {
 var area = ol.Sphere.getArea(polygon);
 var output;
 if (area > 10000) {
 output = (Math.round(area / 1000000 * 100) / 100) +
 ' ' + '平方千米';
 } else {
 output = (Math.round(area * 100) / 100) +
 ' ' + '平方米';
 }
 return output;
 };
})

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

Javascript 相关文章推荐
如何简单地用YUI做JavaScript动画
Mar 10 Javascript
JS的replace方法详细介绍
Nov 09 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(一)让静态人物动起来
Jan 23 Javascript
JS网页图片按比例自适应缩放实现方法
Jan 15 Javascript
js中iframe调用父页面的方法
Oct 30 Javascript
JavaScript事件委托技术实例分析
Feb 06 Javascript
jQuery实现自定义checkbox和radio样式
Jul 13 Javascript
jQuery如何使用自动触发事件trigger
Nov 29 Javascript
判断数组的最佳方法(推荐)
Oct 11 Javascript
Vue基本使用之对象提供的属性功能
Apr 30 Javascript
jQuery实现中奖播报功能(让文本滚动起来) 简单设置数值即可
Mar 20 jQuery
Vue实现返回顶部按钮实例代码
Oct 21 Javascript
JQuery Ajax如何实现注册检测用户名
Sep 25 #jQuery
Openlayers测量距离与面积的实现方法
Sep 25 #Javascript
基于JS实现操作成功之后自动跳转页面
Sep 25 #Javascript
OpenLayers3实现测量功能
Sep 25 #Javascript
OpenLayers3实现地图鹰眼以及地图比例尺的添加
Sep 25 #Javascript
OpenLayers3实现鼠标移动显示坐标
Sep 25 #Javascript
js获取url页面id,也就是最后的数字文件名
Sep 25 #Javascript
You might like
require(),include(),require_once()和include_once()区别
2008/03/27 PHP
php中debug_backtrace、debug_print_backtrace和匿名函数用法实例
2014/12/01 PHP
PHP中new static()与new self()的比较
2016/08/19 PHP
ThinkPHP中Widget扩展的两种写法及调用方法详解
2017/05/04 PHP
php中上传文件的的解决方案
2018/09/25 PHP
PHP实现简单日历类编写
2020/08/28 PHP
jquery事件机制扩展插件 jquery鼠标右键事件
2011/12/21 Javascript
javascript采用数组实现tab菜单切换效果
2012/12/12 Javascript
JQuery onload、ready概念介绍及使用方法
2013/04/27 Javascript
JQuery动画与特效实例分析
2015/02/02 Javascript
JS实现字符串转日期并比较大小实例分析
2015/12/09 Javascript
Linux系统中利用node.js提取Word(doc/docx)及PDF文本的内容
2017/06/17 Javascript
element实现合并单元格通用方法
2019/11/13 Javascript
Preload基础使用方法详解
2020/02/03 Javascript
详解微信小程序(Taro)手动埋点和自动埋点的实现
2021/03/02 Javascript
使用Python的Tornado框架实现一个一对一聊天的程序
2015/04/25 Python
Python3中的真除和Floor除法用法分析
2016/03/16 Python
详解python中的线程
2018/02/10 Python
Python解决线性代数问题之矩阵的初等变换方法
2018/12/12 Python
selenium+python自动化测试之页面元素定位
2019/01/23 Python
Python生成器的使用方法和示例代码
2019/03/04 Python
在Pandas中处理NaN值的方法
2019/06/25 Python
对Django的restful用法详解(自带的增删改查)
2019/08/28 Python
python两种注释用法的示例
2020/10/09 Python
python向xls写入数据(包括合并,边框,对齐,列宽)
2021/02/02 Python
巴西宠物商店:Cobasi
2019/04/19 全球购物
12月小学生校园广播稿
2014/02/04 职场文书
顶撞老师检讨书
2014/02/07 职场文书
银行授权委托书样本
2014/10/13 职场文书
保密工作整改报告
2014/11/06 职场文书
保留意见审计报告
2015/06/05 职场文书
2015暑假打工实践报告
2015/07/13 职场文书
go:垃圾回收GC触发条件详解
2021/04/24 Golang
只用40行Python代码就能写出pdf转word小工具
2021/05/31 Python
Golang 语言控制并发 Goroutine的方法
2021/06/30 Golang
MySQL系列之十五 MySQL常用配置和性能压力测试
2021/07/02 MySQL