当前位置: 首页 > news >正文

mapboxgl 中热力图的实现以及给热力图点增加鼠标移上 popup 效果

文章目录

    • 概要
    • 效果预览
    • 技术思路
    • 技术细节
    • 小结

概要

本篇文章还是关于最近做到的 mapboxgl 地图展开的。
借鉴官方示例:https://iclient.supermap.io/examples/mapboxgl/editor.html#heatMapLayer

效果预览

请添加图片描述

技术思路

  1. 将接口数据渲染到地图中形成热力图。
  2. 还需要将热力图中渲染的点做鼠标移上显示详情 popup 的效果。

注意:因为热力图本身不可以添加鼠标以上效果,所以还是使用了点,将鼠标以上效果加给点,然后把点的透明度设置为0,大小和热力图中点相同,即可完成上图中效果。

技术细节

  1. 地图的加载不再赘述,之前文章中写到了。
  2. 所需要规范的点数据
    其中point是自定义的,传啥都可以, createPopupStyle 就是生成 popup 的 html
featuresList.push({'type': 'Feature','properties': {...point,'description': that.createPopupStyle(point)},'geometry': {'type': 'Point','coordinates': [Number(point.lng), Number(point.lat)]}})
  1. 新建热力图
/*** 添加热力图*/
createHeatPoints(featuresList) {const that = thisconst map = this.map      let heatMapLayer = new mapboxgl.supermap.HeatMapLayer("heatMap",{"map": map,"id": "heatmap","radius": 50,// 设置图层透明度:(参数方式)"opacity": 0.6,// featureWeight指定以哪个属性值为热力权重值创建热力图:"featureWeight": "value",});let heatPoints = {"type": "FeatureCollection","features": featuresList};heatMapLayer.addFeatures(heatPoints);//        设置图层透明度:(函数方式)//        heatMapLayer.setOpacity(0.5);map.addLayer(heatMapLayer);      
},
  1. 添加透明度为0的点以及鼠标移上效果
/*** 添加坐标点及鼠标移上效果*/
addPoints(featuresList) {      const map = this.mapmap.addSource('places', {'type': 'geojson','data': {'type': 'FeatureCollection','features': featuresList}})// 加载 circle 定位圆let img = {name: 'circle_img',sdf: true}this.addCircleImage(img)map.addLayer({'id': 'places','type': 'symbol','source': 'places','layout': {'icon-image': img.name, // 图标ID'icon-size': 0.4, // 图标的大小// 'icon-size': ['get', 'imgSize'], // 图标的大小'icon-anchor': 'center', // 图标的位置// 'text-field': ['get', 'num'],},'paint': {'text-color': '#333','icon-color': 'rgba(0,0,0,0)'},});// Create a popup, but don't add it to the map yet.const popup = new mapboxgl.Popup({closeButton: false,closeOnClick: false});map.on('mouseenter', 'places', (e) => {// Change the cursor style as a UI indicator.map.getCanvas().style.cursor = 'pointer';// Copy coordinates array.const coordinates = e.features[0].geometry.coordinates.slice();const description = e.features[0].properties.description;// Ensure that if the map is zoomed out such that multiple// copies of the feature are visible, the popup appears// over the copy being pointed to.while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;}// Populate the popup and set its coordinates// based on the feature found.popup.setLngLat(coordinates).setHTML(description).addTo(map);});map.on('mouseleave', 'places', () => {map.getCanvas().style.cursor = '';popup.remove();});
},
  1. 引入图片使用方法
    注意:vue中引入图片要使用require引入,路径不能以传参的形式传入,最好写相对路径。不然都会报错。
/**
* 引入图片* img obj : name, sdf*/
addCircleImage(img) {const map = this.mapmap.loadImage(require('./circle.png'), (error, image) => {if (error) throw error;if (!map.hasImage(img.name)) map.addImage(img.name, image, {sdf: img.sdf || false});})
}

小结

本方法主要还是使用点和热力图重叠同时显示效果。

相关文章:

  • 【云原生】Kubernetes Operator模式
  • 基于飞浆OCR的文本框box及坐标中心点检测JSON格式保存文本
  • 系列十(实战)、发送 接收批量消息(Java操作RocketMQ)
  • 图像处理-周期噪声
  • 云计算:OpenStack 配置二层物理网卡为三层桥的接口
  • 文件监控-IT安全管理软件
  • 鸿蒙(HarmonyOS)项目方舟框架(ArkUI)之线性布局容器Row组件
  • OpenHarmony城市技术论坛武汉站:探索大模型时代的终端操作系统创新
  • 二叉树的非递归遍历|前中后序遍历
  • android studio导入module
  • 解决:Vue2项目兼容IE,页面出现白屏
  • 单集群400TB,OceanBase稳定支撑快手核心业务场景
  • Django信号机制源码分析(观察者模式)
  • docker学习(二十一、network使用示例container、自定义)
  • 【自然语言处理】【大模型】 ΨPO:一个理解人类偏好学习的统一理论框架
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • 【node学习】协程
  • CentOS 7 防火墙操作
  • CentOS从零开始部署Nodejs项目
  • ESLint简单操作
  • express如何解决request entity too large问题
  • Java程序员幽默爆笑锦集
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • Python连接Oracle
  • React Native移动开发实战-3-实现页面间的数据传递
  • Redis字符串类型内部编码剖析
  • use Google search engine
  • Vue组件定义
  • 不上全站https的网站你们就等着被恶心死吧
  • 从重复到重用
  • 分类模型——Logistics Regression
  • 服务器从安装到部署全过程(二)
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 前端攻城师
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 思否第一天
  • 小李飞刀:SQL题目刷起来!
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • ​DB-Engines 11月数据库排名:PostgreSQL坐稳同期涨幅榜冠军宝座
  • ​Java并发新构件之Exchanger
  • ​secrets --- 生成管理密码的安全随机数​
  • #微信小程序:微信小程序常见的配置传旨
  • $.ajax()参数及用法
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (2)Java 简介
  • (20050108)又读《平凡的世界》
  • (bean配置类的注解开发)学习Spring的第十三天
  • (JS基础)String 类型
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)springboot宠物医疗服务网站 毕业设计688413
  • (强烈推荐)移动端音视频从零到上手(下)
  • (已解决)什么是vue导航守卫
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • .CSS-hover 的解释