当前位置:网站首页>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 ).
边栏推荐
- Gauss elimination solves the inverse of matrix (Gauss)
- Interview shock 68: why does TCP need three handshakes?
- Redis sentinel mode setup under Windows
- PHP executes shell script
- Sublime install plug-ins
- Uni app learning summary
- SSG框架Gatsby访问数据库,并显示到页面上
- CSV data file settings of JMeter configuration components
- A new paradigm of distributed deep learning programming: Global tensor
- Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
猜你喜欢
MySQL 5.7.25 source code installation record
QT handy notes (III) use qtcharts to draw a line chart in VS
服务器内存故障预测居然可以这样做!
Xiaobai makes a wave of deep copy and shallow copy
【荧光字效果】
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
开发转测试:从0开始的6年自动化之路...
Login module use case writing
随机推荐
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
图解用户登录验证流程,写得太好了!
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
服务发现原理分析与源码解读
Node 内存溢出及V8垃圾回收机制
asp. Net using redis cache
R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp
[datawhale] [machine learning] Diabetes genetic risk detection challenge
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
反射机制的原理是什么?
QT handy notes (II) edit control and float, qstring conversion
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
POJ 1012 Joseph
新公链Aptos何以拉满市场期待值?
E. Two Small Strings
Explain automatic packing and unpacking?
spolicy请求案例
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
Sublime install plug-ins
Flutter event distribution