openlayer实现图层点击响应功能

本文介绍了如何在OpenLayers中实现图层的点击响应功能。通过监听地图的Hover事件,结合GeoJSON数据创建矢量图层,并展示了一个具体的代码示例,包括读取GeoJSON数据、创建Vector源及图层。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法:

//地图Hover事件监听

function ListMapHoverkFunc()
{
    var selectHover = new ol.interaction.Select({
        condition: ol.events.condition.pointerMove
    });
    map.addInteraction(selectHover);
}




//地图点击事件监听
function ListMapClickFunc()
{
    var selectClick = new ol.interaction.Select({
        condition: ol.events.condition.click
    });
    //鼠标点击地图叠加要素监听函数
    map.addInteraction(selectClick);



    map.on('click', function (evt) {
        var pixel = map.getEventPixel(evt.originalEvent);
        var feature = map.forEachFeatureAtPixel(pixel, function (feature, layer) {
            return feature;
        });



        if (feature != undefined && feature.get("is_Gq")=="true") {
            alert("名称:" + feature.get("name")+"编码:"+feature.get("id"));
        }


    });

}


图层数据:

var cg;
function getCgLyr() {
    var geojsonObject = {
        "type": "FeatureCollection", "features": [
        {
        "type": "Feature", "geometry": { "type": "LineString", "coordinates": [[13573970.118557936, 3633242.5444948766],...,[13239428.843400298, 3787373.647233305]] },
        "properties": { "name": "cg", "queryargs": "taihu_js", "id": "F01", "is_Gq": "true" }
        }
        ]
    };


    var vectorSource = new ol.source.Vector({
        features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
    });


    changjiang = new ol.layer.Vector({
        source: vectorSource
    });


    return changjiang;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值