mapboxGL测量实现

2024-02-04 05:10
文章标签 实现 测量 mapboxgl

本文主要是介绍mapboxGL测量实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

讲真,MapboxGL里面虽然有测量的功能,但是不太好用,于是就萌生了自己实现的方法。本文几个turf.js来说说mapboxGL中测量的实现。

效果

测距

测面

实现

1、实现思路

  1. 按照绘制的流程,需要涉及到map的三个事件:click,dblclick,mousemove,其中click为绘制,dblclick为结束绘制,mousemove为绘制中。这样,定义一个状态标识isMeasure,在点击开始绘制的按钮的时候,将标识设置为true,在map的三个事件中都会根据这个标识判断是否为绘制状态。
  2. 地图的展示分layer和marker来分别展示;
  3. layer里面区分点和线(面)图层,以达到比较好的展示效果。

2、实现代码

1.样式
.measure-result {background-color: white;border-radius: 3px;height: 16px;line-height: 16px;padding: 0 3px;font-size: 12px;box-shadow: 0 0 0 1px #ccc;&.close {cursor: pointer;width: 14px;height: 14px;line-height: 16px;text-align: center;padding: 0;}
}
2.测量距离
function measureLength() {var isMeasure = true;// 禁止双击缩放map.doubleClickZoom.disable();map.getCanvas().style.cursor = 'default';function clearMeasure() {$(".measure-result").remove();var source = map.getSource('points');var json = {'type': 'FeatureCollection','features': []};if(source) {map.getSource('points').setData(json);map.getSource('line-move').setData(json);map.getSource('line').setData(json);}var sourceArea = map.getSource('points-area');if(sourceArea) {map.getSource('points-area').setData(json);map.getSource('line-area').setData(json);}}clearMeasure();var jsonPoint = {'type': 'FeatureCollection','features': []};var jsonLine = {'type': 'FeatureCollection','features': []};var points = [];const ele = document.createElement('div');ele.setAttribute('class', 'measure-result');const option = {element: ele,anchor: 'left',offset: [8, 0]};var tooltip = new mapboxgl.Marker(option).setLngLat([0, 0]).addTo(map);var markers = [];var source = map.getSource('points');if(source) {map.getSource('points').setData(jsonPoint);map.getSource('line-move').setData(jsonLine);map.getSource('line').setData(jsonLine);} else {map.addSource('points', {type: 'geojson',data: jsonPoint});map.addSource('line', {type: 'geojson',data: jsonLine});map.addSource('line-move', {type: 'geojson',data: jsonLine});map.addLayer({id: 'line-move',type: 'line',source: 'line-move',paint: {'line-color': '#ff0000','line-width': 2,'line-opacity': 0.65}});map.addLayer({id: 'line',type: 'line',source: 'line',paint: {'line-color': '#ff0000','line-width': 2,'line-opacity': 0.65}});map.addLayer({id: 'points',type: 'circle',source: 'points',paint: {'circle-color': '#ffffff','circle-radius': 3,'circle-stroke-width': 2,'circle-stroke-color': '#ff0000'}});}function addPoint(coords) {if(jsonPoint.features.length > 0) {var prev = jsonPoint.features[jsonPoint.features.length - 1];jsonLine.features.push({type: 'Feature',geometry: {type: 'LineString',coordinates: [prev.geometry.coordinates, coords]}});map.getSource('line').setData(jsonLine);}jsonPoint.features.push({type: 'Feature',geometry: {type: 'Point',coordinates: coords}});map.getSource('points').setData(jsonPoint);}function getLength(coords) {var _points = points.concat([coords]);var line = turf.lineString(_points);var len = turf.length(line);if(len < 1) {len = Math.round(len * 1000) + 'm';} else {len = len.toFixed(2) + 'km';}return len;}function addMeasureRes(coords) {const ele = document.createElement('div');ele.setAttribute('class', 'measure-result');const option = {element: ele,anchor: 'left',offset: [8, 0]};ele.innerHTML = points.length === 0 ? '起点' : getLength(coords);var marker = new mapboxgl.Marker(option).setLngLat(coords).addTo(map);markers.push(marker);}map.on('click', function (_e) {if(isMeasure) {var coords = [_e.lngLat.lng, _e.lngLat.lat];addMeasureRes(coords);addPoint(coords);points.push(coords);}});map.on('mousemove', function (_e) {if(isMeasure) {var coords = [_e.lngLat.lng, _e.lngLat.lat];if (jsonPoint.features.length > 0) {var prev = jsonPoint.features[jsonPoint.features.length - 1];var json = {type: 'Feature',geometry: {type: 'LineString',coordinates: [prev.geometry.coordinates, coords]}};map.getSource('line-move').setData(json);ele.innerHTML = getLength(coords);} else {ele.innerHTML = '点击地图开始测量';}tooltip.setLngLat(coords);}});map.on('dblclick', function (_e) {if(isMeasure) {var coords = [_e.lngLat.lng, _e.lngLat.lat];addPoint(coords);isMeasure = false;map.getCanvas().style.cursor = '';jsonPoint.features = [];jsonLine.features = [];tooltip.remove();// 添加关闭按钮const ele = document.createElement('div');ele.setAttribute('class', 'measure-result close');const option = {element: ele,anchor: 'bottom-left',offset: [-5, -10]};ele.innerHTML = '×';new mapboxgl.Marker(option).setLngLat(coords).addTo(map);ele.onclick = function (__e) {__e.stopPropagation();map.doubleClickZoom.enable();clearMeasure();}}});
}  
3.测量面积
function measureArea() {  var isMeasure = true;// 禁止双击缩放map.doubleClickZoom.disable();map.getCanvas().style.cursor = 'default';function clearMeasure() {$(".measure-result").remove();var source = map.getSource('points');var json = {'type': 'FeatureCollection','features': []};if(source) {map.getSource('points').setData(json);map.getSource('line-move').setData(json);map.getSource('line').setData(json);}var sourceArea = map.getSource('points-area');if(sourceArea) {map.getSource('points-area').setData(json);map.getSource('line-area').setData(json);}}clearMeasure();var jsonPoint = {'type': 'FeatureCollection','features': []};var jsonLine = {'type': 'FeatureCollection','features': []};var points = [];var ele = document.createElement('div');ele.setAttribute('class', 'measure-result');const option = {element: ele,anchor: 'left',offset: [8, 0]};var tooltip = new mapboxgl.Marker(option).setLngLat([0, 0]).addTo(map);var source = map.getSource('points-area');if(source) {map.getSource('points-area').setData(jsonPoint);map.getSource('line-area').setData(jsonLine);} else {map.addSource('points-area', {type: 'geojson',data: jsonPoint});map.addSource('line-area', {type: 'geojson',data: jsonLine});map.addLayer({id: 'line-area',type: 'fill',source: 'line-area',paint: {'fill-color': '#ff0000','fill-opacity': 0.1}});map.addLayer({id: 'line-area-stroke',type: 'line',source: 'line-area',paint: {'line-color': '#ff0000','line-width': 2,'line-opacity': 0.65}});map.addLayer({id: 'points-area',type: 'circle',source: 'points-area',paint: {'circle-color': '#ffffff','circle-radius': 3,'circle-stroke-width': 2,'circle-stroke-color': '#ff0000'}});}function getArea(coords) {var pts = points.concat([coords]);pts = pts.concat([points[0]]);var polygon = turf.polygon([pts]);var area = turf.area(polygon);if(area < 1000) {area = Math.round(area) + 'm²';} else {area = (area / 1000000).toFixed(2) + 'km²';}return area;}function addPoint(coords) {jsonPoint.features.push({type: 'Feature',geometry: {type: 'Point',coordinates: coords}});map.getSource('points-area').setData(jsonPoint);}map.on('click', function (_e) {if(isMeasure) {var coords = [_e.lngLat.lng, _e.lngLat.lat];points.push(coords);addPoint(coords);}});map.on('dblclick', function (_e) {if(isMeasure) {var coords = [_e.lngLat.lng, _e.lngLat.lat];points.push(coords);isMeasure = false;ele.innerHTML = getArea(coords);tooltip.setLngLat(coords);// 添加关闭按钮var _ele = document.createElement('div');_ele.setAttribute('class', 'measure-result close');var option = {element: _ele,anchor: 'bottom-left',offset: [-5, -10]};_ele.innerHTML = '×';new mapboxgl.Marker(option).setLngLat(coords).addTo(map);_ele.onclick = function (__e) {__e.stopPropagation();map.doubleClickZoom.enable();clearMeasure();}}});map.on('mousemove', function (_e) {if(isMeasure) {var coords = [_e.lngLat.lng, _e.lngLat.lat];var len = jsonPoint.features.length;if (len === 0) {ele.innerHTML = '点击地图开始测量';} else if (len ===1) {ele.innerHTML = '点击地图继续绘制';} else {var pts = points.concat([coords]);pts = pts.concat([points[0]]);var json = {type: 'Feature',geometry: {type: 'Polygon',coordinates: [pts]}};map.getSource('line-area').setData(json);ele.innerHTML = getArea(coords);}tooltip.setLngLat(coords);}});
}  

这篇关于mapboxGL测量实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/676439

相关文章

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件