当前位置:网站首页>Mapbox 创建多个可拖动的标记点
Mapbox 创建多个可拖动的标记点
2022-04-21 09:47:00 【Windyluna】
官方文档:创建可拖动的标记(Marker)

前言:
(1)由于采用图层的方式添加标记点 addImage,存在多个点时,无法判断被拖动的是哪一个点,因此采用 marker 标记点来实现。
(2)marker 添加标记点:给每个标记点添加索引 data-index、拖拽结束后根据对应索引值,找到拖拽前后标记点的坐标值变化。
实现:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>创建可拖动的标记(Marker)</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.js"></script>
<link href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.1/mapbox-gl.css" rel="stylesheet" />
<style> body {
margin: 0; padding: 0; } #map {
position: absolute; top: 0; bottom: 0; width: 100%; } </style>
</head>
<body>
<style> .coordinates {
background: rgba(0, 0, 0, 0.5); color: #fff; position: absolute; bottom: 40px; left: 10px; padding: 5px 10px; margin: 0; font-size: 11px; line-height: 18px; border-radius: 3px; display: none; } </style>
<div id="map"></div>
<pre id="coordinates" class="coordinates"></pre>
<script> mapboxgl.accessToken = "pk.eyJ1IjoibWVpaW4xMjM0NTYiLCJhIjoiY2tqbWtlemR5MGt4MTJ4bjBxcjNmcng5NCJ9.GRpGEmZhxJ58EkNW6Ta_AQ"; var coordinates = document.getElementById("coordinates"); var map = new mapboxgl.Map({
container: "map", style: "mapbox://styles/mapbox/streets-v11", center: [0, 0], zoom: 2 }); // // 单个标记点(可拖动) // var marker = new mapboxgl.Marker({
// draggable: true // }) // .setLngLat([0, 0]) // .addTo(map); // function onDragEnd(e) {
// var lngLat = marker.getLngLat(); // coordinates.style.display = "block"; // coordinates.innerHTML = // "Longitude: " + lngLat.lng + "<br />Latitude: " + lngLat.lat; // } // marker.on("dragend", onDragEnd); // 多个标记点(可拖动) let markerList = []; //标记点集合 var isDrag = true; //可以拖动:draggable: true // 经纬度点集合 var markerMore = [ {
longitude: -25.400390624998096, latitude: 4.91583280132032 }, {
longitude: -23.818359375007987, latitude: -3.1624555302312416 }, {
longitude: -11.337890625005741, latitude: -12.554563528580573 } ]; for (let i = 0; i < markerMore.length; i++) {
let dom = document.createElement("div"); dom.innerHTML = `<div class="sampling-box" data-index="${
i}" style="width: 16px;height: 16px;background: #5393E7;border-radius: 50%;"></div>'`; var lngLat = [markerMore[i].longitude, markerMore[i].latitude]; let marker = new mapboxgl.Marker(dom) .setLngLat(lngLat) .setDraggable(isDrag) .addTo(this.map); // console.log("marker", marker); marker.on("dragend", this.onDragEnd); markerList.push(marker); } function onDragEnd(e) {
let marker = e.target; let lngLat = marker.getLngLat(); let element = marker.getElement(); // console.log("marker拖动结束坐标:", lngLat); // console.log("marker拖动结束HTML:", element); // 读取html元素 data携带的index属性 let dataset = element.querySelector(".sampling-box").dataset; let currneMarkerIndex = dataset.index ? Number(dataset.index) : 0; // 对应的是markerMore数组的序号 } </script>
</body>
</html>
版权声明
本文为[Windyluna]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Windyluna/article/details/124243296
边栏推荐
- 【手拉手 带你准备电赛】April Tag标记跟踪(3D定位)详解
- 【愚公系列】2022年04月 微信小程序-地图的使用之面聚合
- 每日一题(2022-04-20)——文件的最长绝对路径
- Linux中,MySQL的常用命令
- 事务的隔离级别与MVCC
- Question brushing record (Niuke MySQL)
- Is synchronized really heavy?
- [summary] 1296 - summarize 12 common mobile terminal H5 and hybrid development problems
- 字符串匹配 KMP BF
- Interview question of a small factory: what is false awakening?
猜你喜欢
随机推荐
[hand in hand to prepare you for the electric game] use the timer interrupt to change the PWM duty cycle
[notes] Launch file syntax record
利用已有的标签文件生成训练集和验证集(YOLO)
【笔记】.launch文件语法记录
CommDGI: Community detection oriented deep graph infomax 2020 CIKM
synchronized真的很重么?
wap图片
[pycharm plugins] download online to install the translation plug-in
ESP32 寻迹模块测试
MySQL基础之写表(创建表)
ArrayList collection
Daily question (2022-04-20) - the longest absolute path of the file
常用文本处理命令
Installation de MySQL dans docker sous CentOS
ROS与MATLAB网络连接
[probability theory and mathematical statistics] 1.4 conditional probability
Gunicorn usage - server project deployment
Interview question of a small factory: what is false awakening?
Daily question (2022-04-19) - the shortest distance of characters
【栈和队列】C语言简单应用 ⌊栈和队列互相实现,循环队列⌉
![Kali:sqlmap :[10:39:37] [CRITICAL] unable to connect to the target URL](/img/bf/123e6f5eadb8d502e135a7cff9b120.png)

![SQL题集[(2)]](/img/cb/2b8dda83cbd610065632109f26ff21.png)





