mapbox gl 测量

一、代码

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Measure distances</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.1.2/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.1.2/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>.distance-container {position: absolute;top: 10px;left: 10px;z-index: 1;}.distance-container > * {background-color: rgba(0, 0, 0, 0.5);color: #fff;font-size: 11px;line-height: 18px;display: block;margin: 0;padding: 5px 10px;border-radius: 3px;}
</style><div id="map"></div>
<div id="distance" class="distance-container"></div><script src="https://unpkg.com/@turf/turf@6/turf.min.js"></script>
<script>mapboxgl.accessToken = '';const map = new mapboxgl.Map({container: 'map',style: 'mapbox://styles/mapbox/streets-v12',center: [2.3399, 48.8555],zoom: 12});const distanceContainer = document.getElementById('distance');// GeoJSON object to hold our measurement featuresconst geojson = {'type': 'FeatureCollection','features': []};// Used to draw a line between pointsconst linestring = {'type': 'Feature','geometry': {'type': 'LineString','coordinates': []}};map.on('load', () => {map.addSource('geojson', {'type': 'geojson','data': geojson});// Add styles to the mapmap.addLayer({id: 'measure-points',type: 'circle',source: 'geojson',paint: {'circle-radius': 5,'circle-color': '#000'},filter: ['in', '$type', 'Point']});map.addLayer({id: 'measure-lines',type: 'line',source: 'geojson',layout: {'line-cap': 'round','line-join': 'round'},paint: {'line-color': '#000','line-width': 2.5},filter: ['in', '$type', 'LineString']});map.on('click', (e) => {const features = map.queryRenderedFeatures(e.point, {layers: ['measure-points']});// Remove the linestring from the group// so we can redraw it based on the points collection.if (geojson.features.length > 1) geojson.features.pop();// Clear the distance container to populate it with a new value.distanceContainer.innerHTML = '';// If a feature was clicked, remove it from the map.if (features.length) {const id = features[0].properties.id;geojson.features = geojson.features.filter((point) => point.properties.id !== id);} else {const point = {'type': 'Feature','geometry': {'type': 'Point','coordinates': [e.lngLat.lng, e.lngLat.lat]},'properties': {'id': String(new Date().getTime())}};geojson.features.push(point);}if (geojson.features.length > 1) {linestring.geometry.coordinates = geojson.features.map((point) => point.geometry.coordinates);geojson.features.push(linestring);// Populate the distanceContainer with total distanceconst value = document.createElement('pre');const distance = turf.length(linestring);value.textContent = `Total distance: ${distance.toLocaleString()}km`;distanceContainer.appendChild(value);}map.getSource('geojson').setData(geojson);});});map.on('mousemove', (e) => {const features = map.queryRenderedFeatures(e.point, {layers: ['measure-points']});// Change the cursor to a pointer when hovering over a point on the map.// Otherwise cursor is a crosshair.map.getCanvas().style.cursor = features.length? 'pointer': 'crosshair';});
</script></body>
</html>

二、截图

三、知识点 

1、相同数据源添加点、线

// 点
filter: ['in', '$type', 'Point']// 线
filter: ['in', '$type', 'LineString']

2、地图点击事件获取指定图层要素

map.on('click', (e) => {const features = map.queryRenderedFeatures(e.point, {layers: ['measure-points']});
})

3、修改地图鼠标手势

 map.getCanvas().style.cursor = features.length? 'pointer': 'crosshair';

 4、添加点、线图层

map.addSource('geojson', {'type': 'geojson','data': geojson});// Add styles to the mapmap.addLayer({id: 'measure-points',type: 'circle',source: 'geojson',paint: {'circle-radius': 5,'circle-color': '#000'},filter: ['in', '$type', 'Point']});map.addLayer({id: 'measure-lines',type: 'line',source: 'geojson',layout: {'line-cap': 'round','line-join': 'round'},paint: {'line-color': '#000','line-width': 2.5},filter: ['in', '$type', 'LineString']});

5、turf 计算长度

let linestring = {'type': 'Feature','geometry': {'type': 'LineString','coordinates': []}};const geojson = {'type': 'FeatureCollection','features': []};linestring.geometry.coordinates = geojson.features.map((point) => point.geometry.coordinates);const distance = turf.length(linestring);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/661301.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Typora导出html文件图片自动转换成base64

Typora导出html文件图片自动转换成base64 一、出现问题二、解决方案三、编码实现3.1.创建Java项目3.2.代码3.3.打包成Jar包 四、如何使用endl 一、出现问题 typora 导出 html 的时候必须带有原图片&#xff0c;不方便交流学习&#xff0c;文件太多显得冗余&#xff0c;只有将图…

【vim 学习系列文章 3.2 -- vim 删除 空格】

文章目录 vim 删除行尾空格 vim 删除行尾空格 在代码开发的过程中&#xff0c;经常会遇到行尾有空格的现象&#xff0c;如下&#xff1a; 我们可以在 .vimrc 中通过map 命令来映射删除行尾空格的快捷键&#xff0c;如下&#xff1a; map d<space> :%s/\s*$//g <cr…

Spring速成(一)

文章目录 Spring速成&#xff08;一&#xff09;1&#xff0c;课程介绍1.1 为什么要学?1.2 学什么?1.3 怎么学? 2&#xff0c;Spring相关概念2.1 初识Spring2.1.1 Spring家族2.1.2 了解Spring发展史 2.2 Spring系统架构2.2.1 系统架构图2.2.2 课程学习路线 2.3 Spring核心概…

Linux实验记录:使用Apache的虚拟主机功能

前言&#xff1a; 本文是一篇关于Linux系统初学者的实验记录。 参考书籍&#xff1a;《Linux就该这么学》 实验环境&#xff1a; VmwareWorkStation 17——虚拟机软件 RedHatEnterpriseLinux[RHEL]8——红帽操作系统 正文&#xff1a; 目录 前言&#xff1a; 正文&…

【重温设计模式】构建器及其Java示例

设计模式中的构建器模式介绍 在编程的世界里&#xff0c;设计模式是一种让我们的代码更加优雅、可读、可维护的工具。其中&#xff0c;构建器模式是一种创建型模式&#xff0c;它提供了一种高效且灵活的方式来创建复杂对象。这种模式的主要特点是&#xff0c;它分离了对象的构…

易点易动设备管理平台助力制造企业实现设备的智能化维修和保养管理

在制造业领域&#xff0c;设备的维修和保养是保障生产运行和产品质量的关键环节。然而&#xff0c;传统的维修和保养管理方式往往存在效率低下、难以及时发现问题等问题。为了解决这些挑战&#xff0c;易点易动设备管理平台应运而生。该平台利用物联网和数据分析技术&#xff0…

回归预测 | Matlab实现CPO-GRU【24年新算法】冠豪猪优化门控循环单元多变量回归预测

回归预测 | Matlab实现CPO-GRU【24年新算法】冠豪猪优化门控循环单元多变量回归预测 目录 回归预测 | Matlab实现CPO-GRU【24年新算法】冠豪猪优化门控循环单元多变量回归预测效果一览基本介绍程序设计参考资料 效果一览 基本介绍 1.Matlab实现CPO-GRU【24年新算法】冠豪猪优化…

sectigo ip ssl证书有哪些

Sectigo是移交成立时间较久的CA认证机构&#xff0c;几十年来在全球颁发了各种各样的数字证书&#xff0c;例如&#xff0c;单域名SSL证书、多域名SSL证书、通配符SSL证书等域名SSL证书。Sectigo旗下也有一些不常见的数字证书&#xff0c;例如&#xff0c;代码签名证书、IP证书…

【设计模式】六大原则详解,每个原则提供代码示例

设计模式六大原则 目录 一、单一职责原则——SRP 1、作用2、基本要点3、举例 二、开放封闭原则——OCP 1、作用2、基本要点3、举例 三、里氏替换原则——LSP 1、作用2、基本要点3、举例 四、依赖倒置原则——DLP 1、作用2、基本要点3、举例 五、迪米特法则——LoD 1、作用2、…

C++数据结构——红黑树

一&#xff0c;关于红黑树 红黑树也是一种平衡二叉搜索树&#xff0c;但在每个节点上增加一个存储位表示节点的颜色&#xff0c;颜色右两种&#xff0c;红与黑&#xff0c;因此也称为红黑树。 通过对任意一条从根到叶子的路径上各个节点着色方式的限制&#xff0c;红黑树可以…

(2)SpringBoot学习——芋道源码

Spring Boot 的自动配置 1.概述 EmbeddedWebServerFactoryCustomizerAutoConfiguration 类 Configuration // <1.1> ConditionalOnWebApplication // <2.1> EnableConfigurationProperties(ServerProperties.class) // <3.1> public class EmbeddedWebSe…

推荐一个好用的旧版本软件安装包下载地址

最近要下载旧版本的mysql和postman&#xff0c;发现官网和其他博客里边提供的地址&#xff0c;要不都非常难找到相应的下载链接&#xff0c;要不就是提供的从别的地方复制过来的垃圾教程&#xff0c;甚至有的下载还要积分&#xff0c;反正是最后都没下载成功&#xff0c;偶然发…

nodejs基于vue奖学金助学金申请系统08ktb

高校奖助学金系统的目的是让使用者可以更方便的将人、设备和场景更立体的连接在一起。能让用户以更科幻的方式使用产品&#xff0c;体验高科技时代带给人们的方便&#xff0c;同时也能让用户体会到与以往常规产品不同的体验风格。 与安卓&#xff0c;iOS相比较起来&#xff0c;…

期末成绩群发给家长

每当学期结束&#xff0c;老师们的邮箱和手机便会被成绩报告单填满。那么&#xff0c;如何高效地将成绩群发给家长呢&#xff1f; 一、邮件还是短信&#xff1f; 首先&#xff0c;选择一个合适的通讯方式是关键。邮件正式且便于附件&#xff0c;但短信更快捷。考虑到大多数家长…

windows平台使用tensorRT部署yolov5详细介绍,整个流程思路以及细节。

目录 Windows平台上使用tensorRT部署yolov5 前言&#xff1a; 环境&#xff1a; 1.为什么要部署&#xff1f; 2.那为什么部署可以解决这个问题&#xff1f;&#xff08;基于tensorRT&#xff09; 3.怎么部署&#xff08;只讨论tensorRT&#xff09; 3.0部署的流程 3.1怎…

UE4学习笔记 FPS游戏制作2 制作第一人称控制器

文章目录 章节目标前置概念Rotator与Vector&#xff1a;roll与yaw与pitch 添加按键输入蓝图结构区域1区域2区域3区域4 章节目标 本章节将实现FPS基础移动 前置概念 Rotator与Vector&#xff1a; Vector是用向量表示方向&#xff0c;UE中玩家的正前方是本地坐标系的(1,0,0)&…

【Linux】信号量

信号量 一、POSIX信号量1、信号量的原理2、信号量的概念&#xff08;1&#xff09;PV操作必须是原子操作&#xff08;2&#xff09;申请信号量失败被挂起等待 3、信号量函数4、销毁信号量5、等待信号量&#xff08;申请信号量&#xff09;6、发布信号量&#xff08;释放信号量&…

20240131在WIN10下配置whisper

20240131在WIN10下配置whisper 2024/1/31 18:25 首先你要有一张NVIDIA的显卡&#xff0c;比如我用的PDD拼多多的二手GTX1080显卡。【并且极其可能是矿卡&#xff01;】800&#xffe5; 2、请正确安装好NVIDIA最新的545版本的驱动程序和CUDA。 2、安装Torch 3、配置whisper http…

仰暮计划|“从米票、肉票、糖果票到肥皂票、煤票、棉花票等,生活里头的方方面面都能用粮票买到”

口述人&#xff1a;牛翠英(女) 整理人&#xff1a;霍芝冉 口述人基本信息&#xff1a;现68岁&#xff0c;河南省安阳市北关区霍家村人&#xff0c;现居河南安阳市区。 奶奶一生辛劳&#xff0c;操持家务&#xff1b;亲眼见证了时代变迁&#xff0c;社会发展&#xff0c;…

docker笔记整理

Docker 安装 添加yum源 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 安装docker yum -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin 启动docker systemctl start docker 查看docker状态 s…