当前位置:网站首页>How to control the display and hiding of layergroup through transparency in leaflet
How to control the display and hiding of layergroup through transparency in leaflet
2022-06-12 23:48:00 【GIS weapon warehouse】
Recently leaflet Development process , The map data needs to be played according to the sequence , The idea is to add the elements of each time node to layerGroup in , And then by switching layerGroup Display and hide to achieve the effect . Look over leaflet Of API file , Find out leaflet There is no direct control in layerGroup Show hidden methods , How to realize it layerGroup Show and hide ?
There are usually two ways of thinking :
The first one is , adopt map.addLayer()、map.removeLayer() add to 、 Remove the layer group , When the amount of data is small , And you don't need to switch layers frequently to show or hide , It is more convenient to use this method . however , When the amount of data is large , Or when you need to switch layer display and hiding frequently , Using this method will increase the pressure on the browser , There's a jam .
The second kind , Traverse all the elements inside the layer , By controlling the transparency of features , Achieve the purpose of controlling layer display and hiding . This method can solve the problem of large amount of data , Or when you need to switch layer display and hiding frequently , There's a Caton situation , The effect is as follows :

The core code is as follows :

We can see from the above code that , because maker elements and vector Feature styles are controlled differently , It needs to be placed in two layer groups , It still feels a little cumbersome to write like this , And the style of layer initialization is not considered .
Through to leaflet Source code research , come to know leaflet have access to include Method to rewrite the method to modify the source code .
include The way
Let's learn about : such as leaflet Source code Polygon.toGeoJSON() The method is not in Polygon.js It's written in the document , It's about using include The way is written in GeoJSON.js In file .Polygon Class doesn't have toGeoJSON() Methodical , This adds to the method . If Polygon Class already has toGeoJSON() Method , This will be written according to the order of execution , The later one will rewrite the first one .
Next , Just use include Way to layerGroup Add a show and hide method . ad locum , We not only control layerGroup Show hidden , It also recorded layerGroup Transparency of elements in the default state , To ensure consistent style when switching to display .
The code is as follows :

For ease of use , We encapsulate the above code into a plug-in , Just reference this plug-in , call showLayer()、hideLayer() That's right layerGroup Display and hide control of all features in .
See if the code after using the plug-in is very refreshing .

summary
- control layerGroup There are two ways to show and hide : add to 、 How to remove ; And traverse the internal elements , How to control the transparency of each element .
- Controlling transparency is more efficient , Experience better , but leaflet There is no ready-made method in , You need to write your own code .
- Attention should be paid to ,
makerelements andvectorFeature styles are controlled differently . - Encapsulate the control transparency mode into plug-ins , adopt
showLayer()、hideLayer()Method directly use .
Online example
ShowHideLayerGroup.js plug-in unit
Original address :http://gisarmory.xyz/blog/index.html?blog=LeafletShowHideLayerGroup.
Focus on 《GIS Zeughaus 》 official account , The first time to get more high quality GIS article .

This article adopts Creative Commons signature - Noncommercial use - Share in the same way 4.0 International licensing agreement Licensing . Welcome to reprint 、 Use 、 Re release , But be sure to keep the signature of the article 《GIS Zeughaus 》( Include links : http://gisarmory.xyz/blog/), Not for commercial purposes , Based on this revised work must be released with the same license .
边栏推荐
- CS for mobile security [nethunter]
- Matlab【路径规划】—— 无人机药品配送路线最优化
- Start of u-boot_ Armboot analysis (I)
- Don't write about the full screen explosion, try the decorator mode, this is the elegant way!!
- CST learning: four element array design of circular patch antenna (II) array formation and combination results
- 【Matlab】基础运算
- Redis实现短信验证码登录
- 华为云会议初体验【华为云至简致远】
- 【Matlab】基础知识
- 基于Three.js海上风电数字孪生三维效果
猜你喜欢

So, what is the difference between e.target and e.currenttarget?

TCP与UDP

数组

2022 electrician (elementary) operation certificate examination question bank and online simulation examination
![[redis sentinel] failed listening on port 26379 (TCP) & sentinel mode no response problem solved](/img/0e/d734cd835d44361d6d93cc1be81c98.jpg)
[redis sentinel] failed listening on port 26379 (TCP) & sentinel mode no response problem solved

Basic operations of dict and set

CST learning: four element array design of circular patch antenna (III) array formation and parallel excitation

Introduction to message oriented middleware (message queue)

Enterprise wechat H5_ Authentication, PC website, enterprise wechat scanning code, authorized login

Based on three JS offshore wind power digital twin 3D effect
随机推荐
SAP QM qp03 displays an inspection plan with multiple specs inspection features
Enterprise wechat H5_ Authentication, H5 application web page authorization login to obtain identity
Theory + practice will help you master the dynamic programming method
妙才周刊 - 5
[redis sentinel] failed listening on port 26379 (TCP) & sentinel mode no response problem solved
Preparing for the Blue Bridge Cup Day11__ Basic operation of serial port communication
2022 questions d'examen pour le personnel de gestion de la sécurité de l'unit é de gestion des produits chimiques dangereux et examen de simulation en ligne
Initial experience of Huawei cloud Conference [Huawei cloud to jianzhiyuan]
Redis realizes SMS verification code login
Industry reshuffle, a large number of programmers are going to lose their jobs? How can we break the current workplace dilemma
Novice must see! How rust beginners write gear smart contracts (1)
Introduction to business rules service on SAP Business Technology Platform (BTP)
【Matlab】基础运算
你真的会用PostGIS中的buffer缓冲吗?
Examination questions and online simulation examination for safety management personnel of hazardous chemical business units in 2022
SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍
Redis实现短信验证码登录
M_8:设计消息队列存储消息数据的 MySQL 表格
Tsinghua University image source will cause tensorflow GPU installation failure
〖Kubernetes指南⑤〗Label快速入门
