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;
    };
})

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

(0)

相关推荐

  • openlayers实现地图测距测面

    本文实例为大家分享了openlayers实现地图测距测面的具体代码,供大家参考,具体内容如下 项目背景vue-cli3.0 public下html需要引入文件 <link rel="stylesheet" href="<%= BASE_URL %>./css/gr-ol.css" type="text/css"> <script src="<%= BASE_URL %>./js/ol.js&qu

  • Openlayers测量距离与面积的实现方法

    本文实例为大家分享了Openlayers测量距离与面积的具体代码,供大家参考,具体内容如下 1.地图测量功能 一般的地图的测量功能主要表现在两个方面,一是测量距离,一是测量面积:面积的测量是根据鼠标绘制的范围,通过地理坐标系的转换而计算出实际面积大小,距离的测量是根据鼠标在地图上绘制的点,实时计算出两点之间的实际距离,下面我们就在Openlayers3中来实现这一功能: 2.代码实现 <!DOCTYPE html> <html xmlns="http://www.w3.org/

  • OpenLayers3实现测量功能

    本文实例为大家分享了OpenLayers3实现测量功能的具体代码,供大家参考,具体内容如下 1. 前言 测量功能实现面积的测量以及长度的测量.通过鼠标绘制区域以及长度来进行测量.OpenLayers 3 框架没有提供测量控件,但提供了相应的接口,需要需要基于几何对象的相应接口,结合图形绘制功能实现. 2. 实现思路 (1)新建一个网页,引用 openlayers 3 开发库.jQuery 库与 bootstrap 库,并参照前面显示地图的文章,加载 OSM 瓦片图层. (2)在地图容器中,创建一

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

    本文实例为大家分享了openlayers4.6.5实现距离量测和面积量测的具体代码,供大家参考,具体内容如下 版本: openlayers4.6.5 效果图: 小插曲: 原本使用ol官方提供的 量测例子,就挺不错的.但是由于放在项目中后.量测样式不知道为啥出不来,找了半天原因 也没有找到,单独在一个html中完全没问题.所以推测可能和项目中哪些地方有冲突,但是问题暂时没找出来,项目也比较急,所以只能自己实现文字标注部门的样式,实现效果如上图gif所示. 实现原理: 量测功能还是使用了ol例子提供

  • 人脸识别测颜值、测脸龄、测相似度微信接口

    人脸评分微信接口,获取微信图片地址,curl请求face++接口.解析json数据,计算颜值.返回用户. 颜值匹配版,请到腾讯微校上体验.http://weixiao.qq.com <?php /** * 人脸识别测颜值.测脸龄.测相似度微信接口 * @Created by MOS.Ving. * @Author: MOS.Ving * @Mail 904679843@qq.com * @Date: 2016-01-31 */ define("TOKEN", 'weixin');

  • 浅谈js之字面量、对象字面量的访问、关键字in的用法

    一:字面量含义 字面量表示如何表达这个值,一般除去表达式,给变量赋值时,等号右边都可以认为是字面量. 字面量分为字符串字面量(string literal ).数组字面量(array literal)和 对象字面量(object literal),另外还有函数字面量(function literal). 示例: var test="hello world!"; "hello world!"就是字符串字面量,test是变量名. 二:对象字面量 对象字面量有两种访问方式

  • js 多种变量定义(对象直接量,数组直接量和函数直接量)

    对象直接量创建一个对象: 复制代码 代码如下: var obj = {x:[1,2],y:23}; 代码跟下面是一样的. 复制代码 代码如下: var obj=new Object(); obj.x=new Array(1,2); obj.y=23; 测试: 复制代码 代码如下: for(var i in obj) alert(obj[i]); 函数直接量:它是一个表达式而不是语句. 复制代码 代码如下: (function(){})() 如下例: 复制代码 代码如下: (function(){

  • python 经纬度求两点距离、三点面积操作

    给出地球上两点的经纬度,计算两点之间的球面距离.给出地球上三点的经纬度,求形成的三角形面积. 对于这样的需求,可以通过使用半正失公式来计算得到我们想要的距离,面积值. 先给出半正失公式(haversine formula): 先看第一个式,等号的右边,输入参数有φ.λ,等号的右边有d.r,其中φ表示纬度,λ表示经度,d是我们要的两点的距离, r是地球半径,d/r表示两点在圆上的弧度θ. 通过整理两个式子可得: 也就是说,只要根据上面最后这行式子,带入相应的经纬度值,以及地球半径,就可以得到我们想

  • Jmerte分布式压测及分布式压测配置教程

    目录 1.本地基于jmeter创建压测项目 2.将项目打包 3.Master配置 4.Slave配置 5.启动Slave 6.master启动压测 7查看报告 1.本地基于jmeter创建压测项目 (1)pom中依赖jmeter包: <dependency> <groupId>org.apache.jmeter</groupId> <artifactId>ApacheJMeter_java</artifactId> <version>

  • javascript开发技术大全 第4章 直接量与字符集

    4.1 字符串直接量 1.字符串直接量 .可以是0个或多个. .字符串必须写在同一行.太长用+号 .用于界定开始与结束双引号和单引号必须匹配 2.字符串中的转义字符      转义字符 所代表的字符 \' 单引号 \" 双引号 \\ 反斜杠 \b 退格符 \f 换页符 \n 换行符 \r 回车符 \t 水平制表符 \v 垂直制表符 \xXX 由2位十六进制(xx)指定的latin-1字符 \XXX 由1位到3位的八进制(1-377)指定的latin-1字符 \UXXXX 由4位十六进制数(xxx

  • JS特殊函数(Function()构造函数、函数直接量)区别介绍

    函数定义 函数是由这样的方式进行声明的:关键字 function.函数名.一组参数,以及置于括号中的待执行代码. 函数的构造语法有这三种: Js代码 复制代码 代码如下: 1.function functionName(arg0, arg1, ... argN) { statements }//function语句 2.var function_name = new Function(arg1, arg2, ..., argN, function_body);//Function()构造函数 3

  • 大数据量高并发的数据库优化详解

    如果不能设计一个合理的数据库模型,不仅会增加客户端和服务器段程序的编程和维护的难度,而且将会影响系统实际运行的性能.所以,在一个系统开始实施之前,完备的数据库模型的设计是必须的. 一.数据库结构的设计 在一个系统分析.设计阶段,因为数据量较小,负荷较低.我们往往只注意到功能的实现,而很难注意到性能的薄弱之处,等到系统投入实际运行一段时间后,才发现系统的性能在降低,这时再来考虑提高系统性能则要花费更多的人力物力,而整个系统也不可避免的形成了一个打补丁工程. 所以在考虑整个系统的流程的时候,我们必须

  • 详解TypeScript映射类型和更好的字面量类型推断

    概述 TypeScript 2.1 引入了映射类型,这是对类型系统的一个强大的补充.本质上,映射类型允许w咱们通过映射属性类型从现有类型创建新类型.根据咱们指定的规则转换现有类型的每个属性.转换后的属性组成新的类型. 使用映射类型,可以捕获类型系统中类似Object.freeze()等方法的效果.冻结对象后,就不能再添加.更改或删除其中的属性.来看看如何在不使用映射类型的情况下在类型系统中对其进行编码: interface Point { x: number; y: number; } inte

随机推荐