地图上的矢量图层hover时,高亮显示并提示图层信息

效果动图

结构,地图+提示弹窗

<div id="homeMap">
	<!--提示弹框-->
	<div id="popBoxId" v-html="tipContent"></div>
</div>

初始化地图时,定义高亮图层以及弹出框

/**
 * 初始化地图
 */
initMap() {
	//初始化地图操作,此处省略。。。
	...
	
    // 提示框
    this.popup = new Overlay({
        element: document.getElementById('popBoxId')
    })
    this.map.addOverlay(this.popup)
    this.map.on('pointermove', this.showTipBox)

    //高亮显示图层
    this.highlightSource = new VectorSource()
    let highlightLayer = new VectorLayer({
        name: '高亮图层',
        source: this.highlightSource,
        style: new Style({
            stroke: new Stroke({
                color: '#c0c0c0',
                width: 5
            }),
            fill: new Fill({
                color: 'rgba(255,255,0,1)'
            })
        }),
        zIndex: 100
    })
    this.map.addLayer(highlightLayer)
}

展示高亮图层以及弹出框

/**
 * 展示高亮图层及弹出框
 */
showTipBox(event) {
    let pixel = this.map.getEventPixel(event.originalEvent)
    let feature = this.map.forEachFeatureAtPixel(pixel, function (feature) {
        return feature
    })
    // 在图层外面的时候,feature和highlight都是null,不执行
    // 如果有feature/没有hightlight,就添加feature到图层,并且给highlight赋值。
    // 如果feature发生改变,就移除highlight并添加新的feature到图层,然后给highlight改变值。
    if (feature !== this.highlightFeature) {
        //清空高亮图层样式
        if (this.highlightFeature) {
            this.highlightSource.removeFeature(this.highlightFeature)
        }
        //添加新的高亮图层,显示弹窗
        if (feature) {
            this.highlightSource.addFeature(feature)
            let coordinate = event.coordinate
            let patchId = feature.getId()
            if (patchId !== undefined) {
                this.tipContent = '<p>斑块: ' + patchId + '</p>'
                this.popup.setPosition(coordinate)
            } else {
                this.popup.setPosition(undefined)
            }
        } else {
            this.popup.setPosition(undefined)
        }
        this.highlightFeature = feature
    }
}
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值