参考 : Leaflet 简单测距_大洪的博客-CSDN博客_leaflet 测距

采用vue框架,map为leaflet的地图对象

支持重复测距

<template><button @click="openDistanceCount()">开启测距</button><button @click="closeDistanceCount()">关闭测距</button>
</template><script>
export default {data() {return {clickHandler: null,mousemoveHandler: null,distanceCountStart:false,distanceCountEnd:false,tempLayer:null,layer2:null,layer: null,popup: null}},created() {},methods: {openDistanceCount(){// 变量let tempPoints = [];// 自定义距离展示框const setTipText = content => {const el = document.createElement("div");el.className = "measure-marker";const text = document.createElement("span");text.className = "measure-text";text.innerHTML = content;const close = document.createElement("span");close.className = "measure-close";close.addEventListener("click", () => {remove();});el.appendChild(text);el.appendChild(close);return el;};// 移动事件this.mousemoveHandler = e => {if (tempPoints.length) {// 记录标记,用于计算最终距离tempPoints[1] = e.latlng;this.tempLayer.setLatLngs(tempPoints);// 线移动时显示距离this.layer2.setLatLngs(tempPoints);const len = turf.length(this.layer2.toGeoJSON(), { units: "kilometers" });this.popup.setLatLng(e.latlng).setContent(setTipText(len.toFixed(2)*1000 + " 米")).openOn(map);}};// 点击事件this.clickHandler = e => {if (this.distanceCountStart){// 测距进行中if (this.distanceCountEnd) {// 已结束,清除测量tempPoints = [];this.tempLayer.remove();this.layer.remove();this.layer2.remove();this.popup.remove();this.distanceCountStart = falsethis.distanceCountEnd = false} else {// 结束测量map.doubleClickZoom.enable()this.distanceCountEnd = true// 移除鼠标移动事件map.off("mousemove", this.mousemoveHandler);// 显示最终距离tempPoints[1] = e.latlng;this.layer2.setLatLngs(tempPoints);const len = turf.length(this.layer2.toGeoJSON(), { units: "kilometers" });this.popup.setLatLng(e.latlng).setContent(setTipText(len.toFixed(2)*1000 + " 米")).openOn(map);return null}}// 初始化变量this.distanceCountInit()// 执行测距// 绑定数据移动事件map.on("mousemove", this.mousemoveHandler);this.layer.addLatLng(e.latlng);tempPoints[0] = e.latlng;// 禁止地图拖动map.doubleClickZoom.disable()this.distanceCountStart = true}// 绑定事件map.on("click", this.clickHandler);map.on("mousemove", this.mousemoveHandler);},distanceCountInit(){// 测距变量初始化this.layer = L.polyline([], {interactive: false}).addTo(map);// 移到距离测量用this.layer2 = L.polyline([], {interactive: false});// 弹出展示距离信息this.popup = L.popup({autoClose: false,closeButton: false});// 划线this.tempLayer = L.polyline([], {interactive: false}).addTo(map);},closeDistanceCount(){// 删除线this.layer.remove();this.layer2.remove();this.popup.remove();this.tempLayer.remove();// 删除事件map.off("click", this.clickHandler);map.off("mousemove", this.mousemoveHandler);}},mounted(){// 引入jsconst jss = ['https://unpkg.com/@turf/turf@6.3.0/turf.min.js']jss.forEach(item=>{let script = document.createElement('script');script.type = 'text/javascript';script.src = item;document.body.appendChild(script);})}
}
</script><style>
</style>

vue(17) : leaflet(2) : 距离测量相关推荐

  1. vue+vuex+leaflet Jest单测踩坑指南

    环境 Vue CLI 3 TypeScript 3.1.4 Leaflet 1.3.4 相关文档 vue-test-utils.vuejs.org/zh/guides/#- 踩坑 1.iview按需引 ...

  2. vue 使用leaflet绘制平面图(二)

    首先在vue中使用,需要安装相关依赖 npm install leaflet 基于前面使用leaflet绘制平面图(一)的代码,修改得一下内容,相对与html会出现一些问题例如:标记图标未显示,需要声 ...

  3. vue使用leafLet(绘图工具详解)

    目录 安装 引入,在项目main.js中 初始化地图 初始化 引入图层,可以引入多个图层 地图事件 添加绘制工具 引入leafle.pm原生组件 设置绘图样式 设置语言 自定义绘图按钮 Drawing ...

  4. Vue渲染Leaflet GIS地图,边界线,点位组件

    package.json中引入并安装: "leaflet": "~1.4.0", "vue2-leaflet": "^2.5.2& ...

  5. VUE·17:事件处理之 @keyup、@keydown 等键盘按键触发及 @keyup.enter 等别名

    一.keyup · 最常用的键盘按键模式 1.原理 只要按下按键,如果不抬起手来,就不会触发事件.只有将按键抬起,这个事件才算是触发. 2.代码示例 2.1 我们可以控制按下哪个键才能 触发事件,而其 ...

  6. vue 使用leaflet 加载地图服务 (可切换天地图 影像地图)(一)

    // 下载 npm install leaflet // 引入 main.js import L from "leaflet"; import "leaflet/dist ...

  7. Android OpenGL ES 2.0 屏幕坐标和3D世界坐标转换

    Android OpenGL ES 2.0 屏幕坐标和3D世界坐标转换 查看全文 http://www.taodudu.cc/news/show-6705596.html 相关文章: word中如何加 ...

  8. vue使用sweetalert2弹窗插件

    1). 安装 sweetalert2 npm install sweetalert2@7.15.1 --save 2). 封装 sweetalert2 在 src 新建 plugins 文件夹,然后新 ...

  9. Vue文件导致的报错  'axios' is not defined

    ✘  http://eslint.org/docs/rules/no-undef  'axios' is not defined     src/App.vue:17:5       axios   ...

最新文章

  1. 去除(还原)git diff 时出现的 ^M
  2. IIS5.1错误,启动时WEB服务提示:服务器没有及时响应启动或控制请求 之终极解决方案。...
  3. springMVC解析视图
  4. 【BZOJ1082】【codevs2456】栅栏,让人抓狂的优化剪枝
  5. 实现加载页Loading Page 的几种方法
  6. linux 触屏设备 无法处理touchevent_一次通讯干扰处理
  7. codeforces 665E Beautiful Subarrays
  8. 据说是学习python最全的资料
  9. 基于android的订餐系统 答辩ppt,外卖订餐系统答辩PPT
  10. 内核的解压缩过程详解
  11. 分治——线性时间选择算法
  12. 干货来袭!几行代码实现pdf添加水印和去除水印
  13. ble原理(1)蓝牙ble协议知识
  14. word2016添加题注|图注文献标号的交叉引用及引用的更新|添加不同类型的页码|文献自动编号|文献编号的自动引用|删除空白页
  15. bc vc投资_天使投资、VC 以及 PE 的区别是什么?
  16. html中鼠标点击图片变动,JS实现页面鼠标点击出现图片特效
  17. Java笔试题(三)简答题
  18. 纯HTML代码绘制表格--初入HTML1
  19. Cache与内存二三事
  20. Firefox - 附加组件 - 插件 - Shockwave Flash

热门文章

  1. libxml/parser.h not found
  2. Python基础学完了再学什么?
  3. 3dmax 3dmax计算机要求 3dmax下载
  4. 【学习笔记】React.js
  5. 联咏平台wifi吞吐量测试方法
  6. css 绘制 上,下,右,左箭头
  7. Android默认电话卡,Android智能终端SIM卡自动切换方法与流程
  8. pcb上模拟地和数字地怎么隔离
  9. 第二章、URL与资源
  10. html如何设置有序列表的列表项,HTML的有序列表