当前位置: 首页 > article >正文

Openlayers高级交互(3/20):动态添加 layer 到 layerGroup,并动态删除

layerGroup 是 OpenLayers 库中的一个类,用于创建图层组。图层组允许您将多个图层组合在一起,并作为一个整体来控制它们的可见性和其他属性。本示例动态添加layer到layerGroup,并动态删除。

效果图

在这里插入图片描述

专栏名称内容介绍
Openlayers基础实战 (72篇)专栏提供73篇文章,为小白群体提供基础知识及示例演示,能解决基础的开发问题
Openlayers高级应用(20篇)专栏提供20篇文章, 为有经验的开发者提供示例参考,对开发指导意义很大
Openlayers全面教程(300+)专栏提供300+篇文章示例, 可以全面的学习Openlayers,适合系统学习及各类开发者

配置说明

1)环境配置参考:https://blog.csdn.net/cuclife/article/details/126195754
2)将示例源代码,粘贴到src/views/Home.vue中,npm run serve 运行即可。

源代码


/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: 2909222303@qq.com
* @First published in CSDN
* @First published time: 2024-10-19
*/

<template>
	<div class="container">
		<h3>vue+openlayers: 动态添加layer到layerGroup,并动态删除</h3>
		<p>大剑师兰特, 还是大剑师兰特</p>
		<h4>
			<span v-for="(item,index) in pointData" :key="index">
				<el-button type="danger" v-show="item.isShow" size="mini" @click='removegpLayer(item)'>删除{{item.myname}}
				</el-button>
				<el-button type="primary" v-show="!item.isShow" size="mini" @click='addgpLayer(item)'>添加{{item.myname}}
				</el-button>
			</span>
		</h4>
		<div id="vue-openlayers"></div>
	</div>
</template>

<script>
	import 'ol/ol.css'
	import {Map,View} from 'ol'
	import Tile from 'ol/layer/Tile'
	import GroupLayer from 'ol/layer/Group'
	import OSM from 'ol/source/OSM'
	import LayerVector from 'ol/layer/Vector'
	import VectorSource from 'ol/source/Vector'
	import Feature from 'ol/Feature'
	import Point from 'ol/geom/Point'
	import Fill from 'ol/style/Fill'
	import Stroke from 'ol/style/Stroke'
	import Style from 'ol/style/Style'
	import Circle from 'ol/style/Circle'

	export default {
		data() {
			return {
				pointData: [{
						myname: 'layer1',
						point: [114.064839, 22.548857],
						isShow: false,
					},
					{
						myname: 'layer2',
						point: [114.074839, 22.548857],
						isShow: false,
					},
				],
				map: null, // 地图
				draw: null,
				geoGroupLayer: new GroupLayer({
					layers: [],
					zIndex: 3,
					myname:"geoGroupLayer",
				}),
			}
		},

		methods: {
			removegpLayer(data) {
				data.isShow = !data.isShow;
				this.geoGroupLayer.getLayers().getArray().forEach((layer, index, array) => {	
					
					if (layer.get('myname') == data.myname) {
						array.splice(index, 1)
					}
				})
			    this.map.removeLayer(this.geoGroupLayer);
				this.map.addLayer(this.geoGroupLayer)
				
			},
			addgpLayer(data) {
				this.map.removeLayer(this.geoGroupLayer);
				let pointFeature = new Feature({
					geometry: new Point(data.point),
				})
				let pointsource = new VectorSource({
					wrapX: false
				});
				pointsource.addFeature(pointFeature)
				let vector = new LayerVector({
					myname: data.myname,
					source: pointsource,
					// Vector层显示的样式
					style: new Style({
						fill: new Fill({
							color: [255, 255, 255, 0.00001]
						}),
						stroke: new Stroke({
							width: 2,
							color: "#00f",
						}),
						image: new Circle({ //点样式
							radius: 10,
							fill: new Fill({
								color: '#ff00ff'
							})
						}),
					})
				});

				this.geoGroupLayer.getLayers().getArray().push(vector);
				 data.isShow = !data.isShow;
				this.map.addLayer(this.geoGroupLayer)
			},

			initMap() {
				let raster = new Tile({
					source: new OSM(),
					myname: "OSM"
				});

				this.map = new Map({
					target: "vue-openlayers",
					layers: [raster],
					view: new View({
						projection: "EPSG:4326",
						center: [114.064839, 22.548857],
						zoom: 13
					})
				})
			},


		},
		mounted() {
			this.initMap()
		}
	}
</script>

<style scoped>
   .container {
        width: 1000px;
        height: 660px;
        margin: 10px auto;
        border: 1px solid #42B983;
    }

    #vue-openlayers {
        width: 960px;
        height: 480px;
        margin: 0 auto;
        border: 1px solid #42B983;
        position: relative;
    }
</style>



http://www.kler.cn/news/355817.html

相关文章:

  • 【2D/3D-Lidar-SLAM】 Cartographer详细解读
  • PCDN 技术如何优化网络延迟(壹)
  • 如何快速学会盲打
  • c++流的异常捕获
  • 使用 surya-ocr 进行文字识别
  • Python 连接和操作 PostgreSQL 数据库的详解
  • Python——判定变量是空还是非空的有效方法
  • 【MySQL 数据库】之--基础知识
  • C++面试速通宝典——27
  • 使用big.js处理js精度缺失的问题
  • Linux发展与基础
  • C++《string的模拟实现》
  • 泛微E-Cology系统 CptInstock1Ajax SQL注入漏洞复现
  • ThreadLocal内存泄漏面试题
  • Java.6--多态-设计模式-抽象父类-抽象方法
  • 【unity小技巧】unity C#对DateTime的常见操作,用于处理日期和时间
  • vue3学习记录-transition
  • 基于Spring Boot的JavaWeb在线考试系统设计与实践
  • 演示:基于WPF的DrawingVisual开发的高刷新率示波器
  • 显示器是如何展示信息的