当前位置:网站首页>Draw arrows with openlayer
Draw arrows with openlayer
2022-07-26 09:52:00 【Zhou Xiaotiao】
Project scenario && Problem description :
demand : Got a line from the background geojson data , Now you need to draw it on the map and add arrows ;
Baidu ( Official website example ): Get the picture below 1, Every inflection point has an arrow 
But the demand is : Here's the picture 2, Just need an arrow at the end , No arrow is needed in the middle 
Cause analysis :
Analyze the official website source code :
So what I need to change here is the traversal , After modification, my code is as follows :
arrowFunction(feature){
var geometry = feature.getGeometry();
var styles = [
new Style({
// String style
stroke: new Stroke({
color: '#2EB745', // Color
width: 2 // Line thickness
})
})
];
console.log("geometry",geometry)
// Get the coordinates of the two ends of the last line segment , Generally speaking, the background returns geojson The data is sequential , Just take the last coordinate
// After printing out here, I found that the easiest way to get it is to get flatCoordinates The last four digits of the attribute can , So intercept
let sta = geometry.flatCoordinates.slice(-4)
let dx = sta[2] - sta[0];
let dy = sta[3] - sta[1];
let rotation = Math.atan2(dy, dx); // Get the angle of the line segment ( radian )
styles.push(new Style({
geometry: new Point([sta[2],sta[3]]),
image: new Icon({
// src: 'data/arrow.png',
src:require("../assets/arrow.png"),
anchor: [0.75, 0.5], // Icon anchor
rotateWithView: true, // Rotate with map view
rotation: rotation // Because the angle is positive with clockwise rotation , So add a minus sign in front
})
}));
return styles;
}

summary :
Get access to styles Basically, there is no big problem if the sending method is changed to this , But when I was writing this thing, I still stepped on two pits , Let me share with you :
One 、 hold arrowFunction When a method , To find the corresponding feature Parameters of the incoming :
This is what I wrote at first :( error )
let styles=this.arrowFunction(features)
let vectorLayer = new VectorLayer({
id: name,
visible: true,
source: vectorSource,
style: styles,
});
this.addLayer(vectorLayer);
Later, I found that nothing was right , After consulting colleagues, I learned , This feature You don't have to pass it , Put it in VectorLayer Object can be used directly , So the code is changed to :( correct )
let vectorLayer = new VectorLayer({
id: name,
visible: true,
source: vectorSource,
style: this.arrowFunction,
});
this.addLayer(vectorLayer);
Two 、 The image src problem :
At the beginning, directly copy the code of the official website for use , stay new Icon Medium src Directly use the... On the official website src: 'data/arrow.png', And check the coordinate angle, there is no problem , But the arrow doesn't come out , Later, I found that I didn't find it arrow.png This picture resource , So we'd better download the pictures from the official website and put them locally , Then carry out the corresponding introduction ( Of course , It's OK to use local pictures directly , Note that the background is transparent png picture , It's better to keep the size consistent with the official website example ).
边栏推荐
- Interpretation of the standard of software programming level examination for teenagers_ second level
- Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
- Gauss elimination
- 在Blazor 中自定义权限验证
- POJ 1012 Joseph
- Set view dynamic picture
- 服务器内存故障预测居然可以这样做!
- 官方颁发的SSL证书与自签名证书结合实现网站双向认证
- [datawhale] [machine learning] Diabetes genetic risk detection challenge
- 2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
猜你喜欢

spolicy请求案例

挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破

2021年山东省中职组“网络空间安全”B模块windows渗透(解析)

服务发现原理分析与源码解读

服务器内存故障预测居然可以这样做!

Login module use case writing

The problem of accessing certsrv after configuring ADCs

面试突击68:为什么 TCP 需要 3 次握手?

Matlab Simulink realizes fuzzy PID control of time-delay temperature control system of central air conditioning

Installation and use of cocoapods
随机推荐
Sqoop [environment setup 01] CentOS Linux release 7.5 installation configuration sqoop-1.4.7 resolve warnings and verify (attach sqoop 1 + sqoop 2 Latest installation package +mysql driver package res
高斯消元的应用
高斯消元
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
Learning notes: what are the common array APIs that change the original array or do not change the original array?
Docker configuring MySQL Cluster
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
网络流学习笔记
Redis sentinel mode setup under Windows
Write a script that can run in Bash / shell and PowerShell
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
在Blazor 中自定义权限验证
Development to testing: a six-year road to automation starting from 0
新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
MFC handy notes
JS 连等赋值操作
IIS error prompt after installing Serv-U: hresult:0x80070020
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
网站设计需要的基本知识