Openlayers 距离环绘制
思路:利用layer的StyleFunction 来使地图移动或者放缩的时候,使圆保持在地图中心
/**
* 绘制距离环
* @param {number} distance 每环间隔距离,单位:米
* @param {array} texts 要显示的内容
* @description 创建了个layer,然后在layer的styleFunction中做了配置,这里搞了6个环,每两个是一组;搞两个的原因是为了在左侧和右侧都显示当前环的尺寸
*/
export function drawDistanceRing(distance, texts) {
const map = window.map
const layer = new VectorLayer({
source: new VectorSource(),
zIndex: 999,
projection: 'EPSG:4326',
style: function (feature) {
const center = map.getView().getCenter()
const proj = map.getView().getProjection()
const [offsetL1, offsetR1, offsetL2, offsetR2, offsetL3, offsetR3] = calcDistanceRingTextOffset(center, distance * 0.9) // 计算每个环标签像素偏移量
return [
new Style({ // 每个style都是一个环
geometry: circular(center, distance * 1, 128),
text: new Text({
text: texts[0],
font: '14px sans-serif',
fill: new Fill({
color: '#e72b19',
}),
offsetX: offsetL1[0],
offsetY: offsetL1[1]
}),
stroke: new Stroke({
color: '#e72b19',
width: 3
})
}),
new Style({
geometry: circular(center, distance * 1, 128),
text: new Text({
text: texts[0],
font: '14px sans-serif',
fill: new Fill({
color: '#e72b19',
}),
offsetX: offsetR1[0],
offsetY: offsetR1[1]
}),
stroke: new Stroke({
color: '#e72b19',
width: 3
})
}),
new Style({
geometry: circular(center, distance * 2, 128),
text: new Text({
text: texts[1],
font: '14px sans-serif',
fill: new Fill({
color: '#e72b19',
}),
offsetX: offsetL2[0],
offsetY: offsetL2[1]
}),
stroke: new Stroke({
color: '#e72b19',
width: 3
})
}),
new Style({
geometry: circular(center, distance * 2, 128),
text: new Text({
text: texts[1],
font: '14px sans-serif',
fill: new Fill({
color: '#e72b19',
}),
offsetX: offsetR2[0],
offsetY: offsetR2[1]
}),
stroke: new Stroke({
color: '#e72b19',
width: 3
})
}),
new Style({
geometry: circular(center, distance * 3, 128),
text: new Text({
text: texts[2],
font: '14px sans-serif',
fill: new Fill({
color: '#e72b19',
}),
offsetX: offsetL3[0],
offsetY: offsetL3[1]
}),
stroke: new Stroke({
color: '#e72b19',
width: 3
})
}),
new Style({
geometry: circular(center, distance * 3, 128),
text: new Text({
text: texts[2],
font: '14px sans-serif',
fill: new Fill({
color: '#e72b19',
}),
offsetX: offsetR3[0],
offsetY: offsetR3[1]
}),
stroke: new Stroke({
color: '#e72b19',
width: 3
})
}),
]
}
})
const feature = new Feature(fromExtent(map.getView().getProjection().getExtent())) // layer得有feature,不然不显示
layer.getSource().addFeature(feature)
map.addLayer(layer)
}
/**
* 移除距离环
*/
export function removeDistanceRing(layer) {
if (layer) {
layer.getSource().clear()
window.map.removeLayer(layer)
}
}
/**
* 计算距离环文字偏移量
* @param {array} location 距离环圆心坐标
* @param {number} distance 每个环间距
* @returns 六个环偏移量
*/
function calcDistanceRingTextOffset(location, distance) {
const point = turf.point(location) // 用到了turf这个库
const destination1R = turf.destination(point, distance, 90, 'meters' ).geometry.coordinates
const destination1L = turf.destination(point, distance, 270, 'meters' ).geometry.coordinates
const destination2R = turf.destination(point, distance * 2, 90, 'meters' ).geometry.coordinates
const destination2L = turf.destination(point, distance * 2, 270, 'meters' ).geometry.coordinates
const destination3R = turf.destination(point, distance * 3, 90, 'meters' ).geometry.coordinates
const destination3L = turf.destination(point, distance * 3, 270, 'meters').geometry.coordinates
const pixelCenter = getPixelFromCoordinate(location)
const pixel1R = getPixelFromCoordinate(destination1R)
const pixel1L = getPixelFromCoordinate(destination1L)
const pixel2R = getPixelFromCoordinate(destination2R)
const pixel2L = getPixelFromCoordinate(destination2L)
const pixel3R = getPixelFromCoordinate(destination3R)
const pixel3L = getPixelFromCoordinate(destination3L)
const offsetR1 = [pixel1R[0] - pixelCenter[0] - 5, pixel1R[1] - pixelCenter[1]]
const offsetL1 = [pixel1L[0] - pixelCenter[0], pixel1L[1] - pixelCenter[1]]
const offsetR2 = [pixel2R[0] - pixelCenter[0], pixel2R[1] - pixelCenter[1]]
const offsetL2 = [pixel2L[0] - pixelCenter[0], pixel2L[1] - pixelCenter[1]]
const offsetR3 = [pixel3R[0] - pixelCenter[0], pixel3R[1] - pixelCenter[1]]
const offsetL3 = [pixel3L[0] - pixelCenter[0], pixel3L[1] - pixelCenter[1]]
return [offsetL1, offsetR1, offsetL2, offsetR2, offsetL3, offsetR3]
}
效果图:

感谢
Openlayers 距离环绘制的更多相关文章
- OpenLayers学习笔记(八)— 类似比例尺的距离环(二)
openlayers 3 地图上创建一个距离环,始终以地图中心为中心,每个环之间的距离类似比例尺,随地图缩放而变化. 添加具有覆盖整个范围的特征的虚拟层,其可以被设置为围绕地图中心的环. 这篇是上一篇 ...
- OpenLayers学习笔记(七)— 类似比例尺的距离环(一)
openlayers 3 地图上创建一个距离环,始终以地图中心为中心,每个环之间的距离类似比例尺,随地图缩放而变化. 添加具有覆盖整个范围的特征的虚拟层,其可以被设置为围绕地图中心的环.注意,根据地图 ...
- Floyd判圈算法(判断是否有环)
介意转吗博主~~http://blog.csdn.net/thestoryofsnow/article/details/6822576,我知道不介意啦~ 问题:如何检测一个链表是否有环,如果有,那么如 ...
- WebGL绘制有宽度的线
WebGL中有宽度的线一直都是初学者的一道门槛,因为在windows系统中底层的渲染接口都是D3D提供的,所以无论你的lineWidth设置为多少,最终绘制出来的只有一像素.即使在移动端可以设置有宽度 ...
- 通过openlayers加载dwg格式的CAD图并与互联网地图叠加
Openlayers介绍 Openlayers是一个基于Javacript开发,免费.开源的前端地图开发库,使用它,可以很容易的开发出WebGIS系统.目前Openlayers支持地图瓦片.矢量数 ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- Programming Assignment 5: Kd-Trees
用2d-tree数据结构实现在2维矩形区域内的高效的range search 和 nearest neighbor search.2d-tree有许多的应用,在天体分类.计算机动画.神经网络加速.数据 ...
- 学习记录 java 链表知识
01.import java.util.HashMap; 02.import java.util.Scanner; 03.import java.util.Stack; 04. 05./** 06. ...
- 一个完整openlayer的例子,包括marker,popup等
整理转自:http://www.blogjava.net/siriusfx/archive/2007/11/26/163104.html openlayers提供了几十个示例,虽然每个示例都很简单,但 ...
- 浅谈canvas绘画王者荣耀--雷达图
背景: 一日晚上下班的我静静的靠在角落上听着歌,这时"滴!滴!"手机上传来一阵qq消息.原来我人在问王者荣耀的雷达图在页面上如何做出来的,有人回答用canvas绘画.那么问题来了, ...
随机推荐
- 关于Popup的小坑坑
在做一个自定义的输入搜索框,用textbox+popup来实现.其中有一个小需求,当textbox激活并且没有文本输入的时候,也要显示popup.很自然的想到了使用IsKeyboardFocusedC ...
- st包无效
本机正常安装了 oracle11g 和 ArcSDE10, 想要查询某个空间图层表的shape字段值,所以写了如下sql语句在PL/SQL里执行,select sde.st_astext(shape ...
- webpack-dev-server配置https
前情 最近在做一个浏览器通知的交互需求,但是查阅官方文挡,浏览器通知需要在https环境下才能工作,于是就研究怎么在开发环境下配置一个https服务器 STEP1 安装Chocolatey Choco ...
- 某开源ERP最新版SQL与RCE的审计过程
文章首发于 https://forum.butian.net/share/134 前言 代码路径 https://gitee.com/jishenghua/JSH_ERP 软件版本 华夏ERP_v2. ...
- FineReport模板性能问题排查方法
1. 概述 模板的加载速度受到很多因素影响,如果一个模板预览的时候,加载较慢,该如何去分析问题原因呢? 2. 排查步骤 2.1 查看数据集查询速度 大部分模板加载慢,都是因为 sql 执行速度比较慢. ...
- Python中所有子图标签Legend显示详解
在数据可视化中,图例(legend)是一个非常重要的元素,它能够帮助读者理解图表中不同元素的含义.特别是在使用Python进行可视化时,matplotlib库是一个非常强大的工具,能够轻松创建包含多个 ...
- 利用GmSSL制作SM2国密证书
Part 0前言 GmSSL是一个开源的密码工具箱,支持SM2/SM3/SM4/SM9/ZUC等国密(国家商用密码)算法.SM2国密数字证书及基于SM2证书的SSL/TLS安全通信协议,支持国密硬件密 ...
- @EnableWebMvc 注解会让Swagger无效访问的问题
在工作中,通过Swagger2对项目的controller进行配置,以便于用户测试restful服务接口提高开发效率. 但是今天却出现了一个让我匪夷所思的问题就是在配置类里面加上@EnableWebM ...
- Qt音视频开发34-不同库版本不同位数的库和头文件的引用
一.前言 做开发过程中难免遇到需要引入第三方库的时候,而且需要在不同库版本.不同系统.不同位数下都需要.第三方的库版本众多,一般在大版本中的小版本都是兼容的,但是大版本不兼容,比如ffmpeg目前就有 ...
- OpenMMLab AI实战营 第三课笔记
OpenMMLab AI实战营 第三课笔记 目录 OpenMMLab AI实战营 第三课笔记 进入 mmclassification 目录 导入工具包 下载数据集 数据集目录结构 下载 config ...