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绘画.那么问题来了, ...
随机推荐
- window.open打开网址被拦截
window.open打开网址被拦截 标签: js 坑位 通过window.open打开一个网址,在火狐和IE系列浏览器下会拦截掉,除非用户主动点击允许才会成功,这样用户体验基本是恶心到产品的,而产品 ...
- 【杂谈】如何选择:Session 还是 JWT?
服务端如何验证客户端已经登录? 在用户成功登录后,服务端会发放一个凭证.之后,客户端的每次请求都需要携带该凭证,服务端通过验证凭证的有效性来判断用户是否已登录,并处理请求. 以下是 Session 和 ...
- Vue开启Gzip
Vue配置 1.安装 npm install --save-dev compression-webpack-plugin@5.0.0 const CompressionWebpackPlugin = ...
- 关于 Span 的一切:探索新的 .NET 明星: 1 Span<T> 是什么?
关于 Span 的一切:探索新的 .NET 明星 https://docs.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp- ...
- 【C#】【报错解决】分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。
服务器:Windows Server 数据中心 2016 问题描述: 在本地测试正常运行,但是上传到服务器却出现该错误 报错: 分析器错误消息: 无法识别的属性"targetFramewor ...
- 【软件】【逆向】StarUML regist
// 安装npm brew install node //安装打包工具 npm install asar -g C:\Program Files\StarUML\resources StarUML\r ...
- Ubuntu 的网络图标不见了,怎么解决
1. 问题 Ubuntu 的网络图标不见了 2. 解决 service network-manager status # 此时,你会发现状态是 active(running),不用管 service ...
- Qt编写的项目作品18-数据导入导出(xls/pdf)及打印示例
一.功能特点 组件同时集成了导出数据到csv.xls.pdf和打印数据. 所有操作全部提供静态方法无需new,数据和属性等各种参数设置采用结构体数据,极为方便. 同时支持QTableView.QTab ...
- [转]C# SerialPort串口通信发送接收,处理接收数据完整
废话少说,直接上干货.感兴趣的读者自己去研究代码吧.请见谅. using System; using System.Collections.Generic; using System.IO.Ports ...
- 【转载】hacker术语
1.肉鸡 所谓"肉鸡"是一种很形象的比喻,比喻那些可以随意被我们控制的电脑,对方可以是WINDOWS系统,也可以是UNIX/LINUX系统,可以是普通的个人电脑,也可以是大型的服务 ...