openlayers高亮显示和点击(包含去除默认高亮显示样式)

本文介绍了在OpenLayers 4中如何实现图层的高亮显示和点击功能。通过使用ol.interaction.Select交互,设置高亮图层并添加到地图上。同时,针对ol.interaction.Select的默认样式问题,文章提供了在点击事件中清除当前选中特征以去除默认高亮样式的方法。
摘要由CSDN通过智能技术生成

由于项目之前使用的是ol4,所以这里主要是在ol4中如何实现高亮显示和点击

主要是使用ol.interaction.Select

首先要初始化,指定要高亮显示和选择的图层,然后添加到地图上,添加高亮显示和点击事件

//初始化高亮显示和选择工具
	initSelTool : function() {
		this.selTool = new ol.interaction.Select({//选择工具
			condition : ol.events.condition.click,
			style : function(fea, res) {
				return this_.styleFunction(fea, res);
			},
			layers : [ this.clusterLyr ]
		});
		this.hovTool = new ol.interaction.Select({//高亮显示工具
			condition : ol.events.condition.pointerMove,
			style : function(fea, res) {
				return this_.styleFunction(fea, res, true);
			},
			layers : [ this.clusterLyr ]
		});
		var this_ = this;
		//聚集点高亮显示事件
		this.hovTool.on('select', function(e) {//高亮显示回调函数
			var alert = map.getOverlayById(this_.alertId);
			if (e.selected.length > 0) {// 移入,相邻元素之间的滑动
				this_.inHover = true;
				if (alert) {
					var ele = alert.element;
					if (ele) {
						$(ele).addClass("alert_none");//添加预警样式
					}
				}

				var feature = e.selected[0];
				var id = feature.getId();
				this_.hoverFlt(feature);
			}
			if (e.deselected.length > 0) {// 移出,相邻元素之间的滑动
				this_.inHover = false;
				if (alert) {
					var ele = alert.element;
					if (ele) {
						$(ele).removeClass("alert_none");//移除预警样式
					}
				}
				var feature = e.deselected[0];
				var id = feature.getId();
				this_.disHoverFlt(id);
			}
		});
		
		//聚集点选中后列表仅显示该聚集点数据
		this.selTool.on('select', function(e) {
			if (e.selected.length > 0) {
				var feature = e.selected[0];
				var feas = feature.get("features");
				if (feas.length > 0) {
					var data = [];
					for (var i = 0; i < feas.length; i+&#
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值