vue 实现 leaflet的测绘,测距,测面

Easter79
• 阅读 923

参考1:https://blog.csdn.net/lonly\_maple/article/details/83658608

参考2:https://blog.csdn.net/xtfge0915/article/details/80275094#\_60

前提:vue项目在安装leaflet的基础上  cnpm install leaflet-draw --save
在 vue项目根目录的index.html文件中引入

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="stylesheet" href="https://my.oschina.net//u/4321106/blog/3528070/static/lib/CSS/leaflet.css">
    <link rel="stylesheet" href="https://my.oschina.net//u/4321106/blog/3528070/static/lib/CSS/leaflet-measure-path.css">
    <title>test-e-leaflet2</title>
  </head>
  <body>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
  <script src="https://my.oschina.net//u/4321106/blog/3528070/static/lib/js/leaflet/Leaflet.draw.js"></script>   <!--引入这个,不然会报错-->

  <script>

  </script>
</html>

 .vue文件

方法已经写好了,然后自己在vue的mounted方法内加载地图,在methods中调用方法即可

import leaflet from '_leaflet@1.4.0@leaflet'

import LeafletDraw from '_leaflet-draw@1.0.4@leaflet-draw'

var DRAWING = false; //是否正在绘制
var DRAWLAYERS = [];
var BarDRAWLAYERS = [];
var ISMEASURE = false;  //是否是量距
var MEASURETOOLTIP;  //量距提示
var MEASUREAREATOOLTIP;  //量面提示
var MEASURERESULT = 0;   //测量结果

var DRAWPOLYLINE; //绘制的折线
var DRAWMOVEPOLYLINE; //绘制过程中的折线
var DRAWPOLYLINEPOINTS = []; //绘制的折线的节点集

var DRAWPOLYGON; //绘制的面
var DRAWMOVEPOLYGON; //绘制过程中的面
var DRAWPOLYGONPOINTS = []; //绘制的面的节点集

// 测距
function startDrawLine(func) {
  MEASURERESULT = 0; //测量结果
  map.getContainer().style.cursor = 'crosshair';
  var shapeOptions = {
      color: '#F54124',
      weight: 3,
      opacity: 0.8,
      fill: false,
      clickable: true
  },
  DRAWPOLYLINE = new L.Polyline([], shapeOptions); //绘制的折线
  map.addLayer(DRAWPOLYLINE);
  if (ISMEASURE) { //是否是量距
      MEASURETOOLTIP = new L.Tooltip(map);  //量距提示
  }
  map.on('mousedown', onClick);
  map.on('dblclick', onDoubleClick);
  function onClick(e){
    DRAWING = true;  //是否正在绘制
    DRAWPOLYLINEPOINTS.push(e.latlng);  //绘制的折线的节点集
    if (DRAWPOLYLINEPOINTS.length > 1 && ISMEASURE) {  //是否是量距
        MEASURERESULT += e.latlng.distanceTo(DRAWPOLYLINEPOINTS[DRAWPOLYLINEPOINTS.length - 2]);
    }
    DRAWPOLYLINE.addLatLng(e.latlng);  //绘制的折线
    map.on('mousemove', onMove);
  }
  function onMove(e){
    if (DRAWING) {  //是否正在绘制
          if (DRAWMOVEPOLYLINE != undefined && DRAWMOVEPOLYLINE != null) {  //绘制过程中的折线
              map.removeLayer(DRAWMOVEPOLYLINE);
          }
          var prevPoint = DRAWPOLYLINEPOINTS[DRAWPOLYLINEPOINTS.length - 1];
          DRAWMOVEPOLYLINE = new L.Polyline([prevPoint, e.latlng], shapeOptions);
          map.addLayer(DRAWMOVEPOLYLINE);
          if (ISMEASURE) {
              var distance = MEASURERESULT + e.latlng.distanceTo(DRAWPOLYLINEPOINTS[DRAWPOLYLINEPOINTS.length - 1]);
              MEASURETOOLTIP.updatePosition(e.latlng);  //量距提示
              MEASURETOOLTIP.updateContent({
                  text: '单击确定点,双击结束!',
                  subtext: "总长:" + (distance / 1000).toFixed(2) + "公里"
              });
          }
      }
  }
  function onDoubleClick(e){
      map.getContainer().style.cursor = '';
      if (DRAWING) {
          if (DRAWMOVEPOLYLINE != undefined && DRAWMOVEPOLYLINE != null) {
              map.removeLayer(DRAWMOVEPOLYLINE);
              DRAWMOVEPOLYLINE = null;
          }
          // if (DRAWPOLYLINEPOINTS.length > 1 && ISMEASURE) {
          //     MEASURERESULT += e.latlng.distanceTo(DRAWPOLYLINEPOINTS[DRAWPOLYLINEPOINTS.length - 2]);
          //     var distanceLabel = L.marker(DRAWPOLYLINEPOINTS[DRAWPOLYLINEPOINTS.length - 1], {
          //         icon: new L.divIcon({
          //             className: 'DistanceLabelStyle',
          //             iconAnchor: [-8, 15],
          //             html: "<span class='bubbleLabel'><span class='bubbleLabel-bot bubbleLabel-bot-left'></span><span class='bubbleLabel-top bubbleLabel-top-left'></span><span>总长:" + (MEASURERESULT / 1000).toFixed(2) + "公里" + "</span></span>"
          //         }),
          //     }).addTo(_map);
          //     BarDRAWLAYERS.push(distanceLabel);
          // }
          // //移除提示框
          // if (MEASURETOOLTIP) {
          //     MEASURETOOLTIP.dispose();
          // }
            BarDRAWLAYERS.push(DRAWPOLYLINE);
          // if (func) {
          //     func(DRAWPOLYLINEPOINTS);
          // }
          DRAWPOLYLINEPOINTS = [];
          DRAWING = false;
          ISMEASURE = false;
          map.off('mousedown');
          map.off('mousemove');
          map.off('dblclick');
      }
  }

}

//绘制多边形
function startDrawPolygon(func) {
    MEASURERESULT = 0;
    map.getContainer().style.cursor = 'crosshair';
    map.on('mousedown', function (e) {
        DRAWING = true;
        DRAWPOLYGONPOINTS.push(e.latlng);
        DRAWPOLYGON.addLatLng(e.latlng);
    });
    map.on('mousemove', function (e) {
        if (DRAWING) {
            if (DRAWMOVEPOLYGON != undefined && DRAWMOVEPOLYGON != null) {
                map.removeLayer(DRAWMOVEPOLYGON);
            }
            var prevPoint = DRAWPOLYGONPOINTS[DRAWPOLYGONPOINTS.length - 1];
            var firstPoint = DRAWPOLYGONPOINTS[0];
            DRAWMOVEPOLYGON = new L.Polygon([firstPoint, prevPoint, e.latlng], shapeOptions);
            map.addLayer(DRAWMOVEPOLYGON);

            if (ISMEASURE && DRAWPOLYGONPOINTS.length > 1) {
                var tempPoints = [];
                for (var i = 0; i < DRAWPOLYGONPOINTS.length; i++) {
                    tempPoints.push(DRAWPOLYGONPOINTS[i]);
                }
                tempPoints.push(e.latlng);
                var distance = CalArea(tempPoints);
                MEASUREAREATOOLTIP.updatePosition(e.latlng);
                MEASUREAREATOOLTIP.updateContent({
                    text: '单击确定点,双击结束!',
                    subtext: "总面积:" + (distance / 1000000).toFixed(3) + '平方公里'
                });
            }
        }
    });
    map.on('dblclick', function (e) {
        map.getContainer().style.cursor = '';
        if (DRAWING) {
            if (DRAWMOVEPOLYGON != undefined && DRAWMOVEPOLYGON != null) {
                map.removeLayer(DRAWMOVEPOLYGON);
                DRAWMOVEPOLYGON = null;
            }
            // if (DRAWPOLYGONPOINTS.length > 2 && ISMEASURE) {
            //     MEASURERESULT = CalArea(DRAWPOLYGONPOINTS);
            //     var distanceLabel = L.marker(e.latlng, {
            //         icon: new L.divIcon({
            //             className: 'DistanceLabelStyle',
            //             iconAnchor: [-8, 15],
            //             html: "<span class='bubbleLabel'><span class='bubbleLabel-bot bubbleLabel-bot-left'></span><span class='bubbleLabel-top bubbleLabel-top-left'></span><span>总面积:" + (MEASURERESULT / 1000000).toFixed(3) + "平方公里" + "</span></span>"
            //         }),
            //     }).addTo(_map);
            //     BarDRAWLAYERS.push(distanceLabel);
            // }
            // 移除提示框
            // if (MEASUREAREATOOLTIP) {
            //     MEASUREAREATOOLTIP.dispose();
            // }
            BarDRAWLAYERS.push(DRAWPOLYGON);
            if (func) {
                func(DRAWPOLYGONPOINTS);
            }

            DRAWPOLYGONPOINTS = [];
            DRAWING = false;
            ISMEASURE = false;
            map.off('mousedown');
            map.off('mousemove');
            map.off('dblclick');
        }
    });
    var shapeOptions = {
            color: '#F54124',
            weight: 3,
            opacity: 0.8,
            fill: true,
            fillColor: null,
            fillOpacity: 0.2,
            clickable: true
        },

      DRAWPOLYGON = new L.Polygon([], shapeOptions);
      map.addLayer(DRAWPOLYGON);
    if (ISMEASURE) {
        MEASUREAREATOOLTIP = new L.Tooltip(map);
    }
}

//面积计算
function CalArea(latLngs) {
    var pointsCount = latLngs.length,
        area = 0.0,
        d2r = Math.PI / 180,
        p1, p2;
    if (pointsCount > 2) {
        for (var i = 0; i < pointsCount; i++) {
            p1 = latLngs[i];
            p2 = latLngs[(i + 1) % pointsCount];
            area += ((p2.lng - p1.lng) * d2r) *
                (2 + Math.sin(p1.lat * d2r) + Math.sin(p2.lat * d2r));
        }
        area = area * 6378137.0 * 6378137.0 / 2.0;
    }
    return Math.abs(area);
}

//清除标绘图层
function clearMap() {
    if (MEASURETOOLTIP) {
        MEASURETOOLTIP.dispose();
    }
    if (MEASUREAREATOOLTIP) {
        MEASUREAREATOOLTIP.dispose();
    }
    for (var i = 0; i < BarDRAWLAYERS.length; i++) {
        map.removeLayer(BarDRAWLAYERS[i]);
    }
    BarDRAWLAYERS = [];
}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k