基于canvas和ol的点标注的避让实现

2024-03-07 23:30
文章标签 实现 canvas 标注 ol 避让

本文主要是介绍基于canvas和ol的点标注的避让实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

在做地图的时候,点的标注展示是一个非常常见的功能,但是十几种点在某些区域比较密集是非常常见的,但是业务表达中却需要将之展示出来。基于此需求,本文结合canvas和ol做一简单的实现。

效果

image.png

image.png

实现:

  1. 密集区点的标注通过牵引线的方式引出展示;
  2. 地图放大的时候更新展示;

思路

image.png

实现代码

const points = [{ "properties": {"name":"测试名称应该"}, "geometry": { "type": "Point", "coordinates": [ 110.15558, 19.91038 ] } },{ "properties": {"name":"测试名称应"}, "geometry": { "type": "Point", "coordinates": [ 113.52309, 22.21177 ] } },{ "properties": {"name":"测试名称应该"}, "geometry": { "type": "Point", "coordinates": [ 114.23454, 22.21177 ] } },{ "properties": {"name":"测该很长"}, "geometry": { "type": "Point", "coordinates": [ 119.35695, 26.06293 ] } },{ "properties": {"name":"测试名称应该称应该"}, "geometry": { "type": "Point", "coordinates": [ 120.11582, 30.07927 ] } },{ "properties": {"name":"测试名称应"}, "geometry": { "type": "Point", "coordinates": [ 121.49129, 31.14058 ] } },{ "properties": {"name":"测试名称应该"}, "geometry": { "type": "Point", "coordinates": [ 117.03289, 23.5228 ] } }
]
let canvasFunction = function (extent, resolution, pixelRatio, size, projection) {const canvasWidth = size[0]const canvasHeight = size[1]const [w, h] = map.getSize()const xoff = canvasWidth - w,yoff = canvasHeight - hconst canvas = document.createElement('canvas');canvas.width = canvasWidthcanvas.height = canvasHeightconst context = canvas.getContext('2d');// 数据聚类处理,根据上下和左右的距离进行判断function clusterData(data) {let res = {}let clusterTest = function (pixel, tolrance = [200, 30]) {let r = pixel.join(',')const [x, y] = pixelfor (let key in res) {const [_x, _y] = key.split(',').map(Number)const dx = Math.abs(x - _x),dy = Math.abs(y - _y)if(dx < tolrance[0] && dy < tolrance[1]) {r = keybreak}}return r}for (let i = 0; i < data.length; i++) {const d = data[i]const coords = ol.proj.fromLonLat(d.geometry.coordinates)let pixel = map.getPixelFromCoordinate(coords)pixel = [pixel[0]  + xoff / 2, pixel[1] + yoff / 2].map(p => Math.round(p))d.pixel = pixellet key = pixel.join(',')const width = 26 * 2 + 6 + context.measureText(d.properties.name).widthkey = clusterTest(pixel, [width, 30])if(!res[key]) res[key] = []res[key].push(d)}return res}// 绘制两边为圆的矩形function drawRoundRect (ctx, x, y, width, height = 24, fillStyle = 'rgba(14,77,137,0.75)') {const r = height / 2ctx.fillStyle = fillStylectx.beginPath()ctx.moveTo(x + r, y)ctx.lineTo(x + width - r, y)ctx.arc(x + width - r, y + r, r, Math.PI * 1.5, Math.PI * 0.5)ctx.lineTo(x + r, y + height)ctx.arc(x + r, y + height - r, r, Math.PI * 0.5, Math.PI * 1.5)ctx.closePath()ctx.fill()}// 绘制featurefunction drawFeature (ctx, x = 10, y = 10, text, notCluster = true, index = 0) {let height = 26, width = height * 2 + 6,  r = height / 2if(notCluster) width += ctx.measureText(text).widthctx.save()// 如果有聚类,则避让绘制文字,放在前面是为了让指引线在下面if(!notCluster && map.getView().getZoom() > 4) {const radius = 60const ang  = (-index * 40 - 115) / 180 * Math.PI ;const cx = x + r, cy = y + height - rconst px = cx + Math.sin(ang) * radius,py = cy + Math.cos(ang) * radius// 绘制牵引线ctx.beginPath()ctx.strokeStyle = 'rgba(14,77,137,0.75)'ctx.lineWidth = 2ctx.moveTo(cx, cy)ctx.lineTo(px, py)ctx.stroke()// 绘制牵引线终点小圆圈ctx.beginPath()ctx.fillStyle = 'rgba(14,77,137,1)'ctx.arc(px, py, 2, 0, Math.PI * 2)ctx.fill()// 绘制矩形const h = 18const w = ctx.measureText(text).width + 12drawRoundRect(ctx, px - w - 2, py - h / 2 - 1, w, h, 'rgba(14,77,137,0.5)')// 绘制文字ctx.fillStyle = 'rgb(255,255,255)'ctx.beginPath()ctx.textAlign = 'right'ctx.textBaseline = 'middle'ctx.fillText(text, px - 7, py)}// 绘制矩形drawRoundRect(ctx, x, y, width, height)// 绘制左边的图标ctx.beginPath()const radialLeft = ctx.createRadialGradient(x + r, y + height - r, 0, x + r, y + height - r, r)radialLeft.addColorStop(0, '#fff')radialLeft.addColorStop(1, 'rgba(255,255,255,0)')ctx.fillStyle = radialLeftctx.arc(x + r, y + height - r, r, 0, Math.PI * 2)ctx.fill()// 绘制右边的图标ctx.beginPath()const radialRight = ctx.createRadialGradient(x + width - r, y + r, 0, x + width - r, y + r, r)radialRight.addColorStop(0, '#fff')radialRight.addColorStop(1, 'rgba(255,255,255,0)')ctx.fillStyle = radialRightctx.arc(x + width - r, y + r, r, 0, Math.PI * 2)ctx.fill()// 如果没有聚类,则绘制文字if(notCluster) {ctx.fillStyle = '#fff'ctx.beginPath()ctx.textAlign = 'left'ctx.textBaseline = 'middle'ctx.fillText(text, x + height + 3, y + height - r)}ctx.restore()}const cluster = clusterData(points)for (let key in cluster) {const data = cluster[key]const showText = data.length === 1data.forEach((d, index) => {const [x, y] = d.pixeldrawFeature(context, x, y, d.properties.name, showText, index)})}return canvas;
}
const layer = new ol.layer.Image({source: new ol.source.ImageCanvas({canvasFunction: canvasFunction})
});
map.addLayer(layer);
script>

这篇关于基于canvas和ol的点标注的避让实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并