html+js+openlayer的框选事件

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./ol.css" type="text/css" />
    <script src="./jquery-1.8.3.min.js"></script>
    <script src="./ol.js"></script>
    <style>
        .map {
            width: 100%;
        }
    </style>
    <title>Document</title>
</head>

<body>
    <div id="map" class="map"></div>
    <script type="text/javascript">
        var colorLayer = []; //选中区域的填色图层

        $("#map").css("height", ($(window).height() - 20) + "px");
        var map_ter = new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: "http://t3.tianditu.com/DataServer?T=vec_w&x={x}&y={y}&l={z}&tk=你的天地图key",
            }),
            zIndex: 0,
        });
        var map_ter2 = new ol.layer.Tile({
            source: new ol.source.XYZ({
                url: "http://t1.tianditu.com/DataServer?T=cva_w&X={x}&Y={y}&L={z}&tk=你的天地图key",
            }),
            zIndex: 0,
        });
        var map = new ol.Map({
            target: 'map',
            layers: [map_ter, map_ter2],
            view: new ol.View({
                center: [119.2, 36.3],
                projection: "EPSG:4326",
                zoom: 7.5,
            }),
        });

        //添加选择和框选控件,按住Ctr键,使用鼠标框选采样点
        let select = new ol.interaction.Select();
        map.addInteraction(select);
        let dragBox = new ol.interaction.DragBox({
            condition: ol.events.condition.platformModifierKeyOnly,
        });
        map.addInteraction(dragBox);
        var data = [{
            V_LON: 119.2,
            V_LAT: 36.3,
            V_STATION_NUM: "测试"
        }];
        console.log(data);
        var WFSVectorSource = new ol.source.Vector();
        for (let i = 0; i < data.length; i++) {
            var feature = new ol.Feature({
                geometry: new ol.geom.Point([data[i].V_LON, data[i].V_LAT]),
                popupData: {
                    V_STATION_NUM: data[i].V_STATION_NUM,
                },
            });
            feature = setFeature(feature);
            WFSVectorSource.addFeature(feature);
        }
        var WFSVectorLayer = new ol.layer.Vector({
            source: WFSVectorSource,
        });
        map.addLayer(WFSVectorLayer);
        dragBox.on("boxend", () => {
                let extent = dragBox.getGeometry().getExtent();
                WFSVectorSource.forEachFeatureIntersectingExtent(
                    extent,
                    (feature) => {

                        let coordinate = [
                            [
                                [extent[0], extent[1]],
                                [extent[0], extent[3]],
                                [extent[2], extent[3]],
                                [extent[2], extent[1]],
                                [extent[1], extent[2]],
                            ],
                        ];
                        var boxFeature = new ol.Feature({
                            geometry: new ol.geom.Polygon(coordinate),
                        });
                        colorLayer = new ol.layer.Vector({
                            source: new ol.source.Vector({
                                features: [boxFeature],
                            }),
                            style: new ol.style.Style({
                                fill: new ol.style.Fill({
                                    color: "rgba(0, 0, 0, 0.3)",
                                }),
                            }),
                        });
                        map.addLayer(colorLayer);



                        console.log(feature.A.popupData);
                    }
                );
            })
            //下一次选中取消上次的区域
        dragBox.on("boxstart", () => {
            map.removeLayer(colorLayer);
        });

        //点击地图的时候让选中区域可以取消
        map.on("click", function(e) {
            // map.removeLayer(feature);
            map.removeLayer(colorLayer);
            //获取当前的feaure
            // let pixel = e.map.getEventPixel(e.originalEvent);
            // let hit = map.hasFeatureAtPixel(pixel);
            // if (hit) {
            //     let feature = e.map.forEachFeatureAtPixel(pixel, function(feature) {
            //         return feature;
            //     });
            //     if (!feature.A.popupData) {
            //         map.removeLayer(feature);
            //         map.removeLayer(colorLayer);
            //         return;
            //     }
            // }
        })

        //填色
        function setFeature(feature) {
            feature.setStyle(
                new ol.style.Style({
                    image: new ol.style.Circle({
                        radius: 6,
                        fill: new ol.style.Fill({
                            color: "#00F"
                        }),
                    }),
                })
            );
            return feature;
        }
    </script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
layui的layer模块layer.open方法是一个用于弹出对话框的功能,它支持丰富的配置选项和自定义事件处理。当你使用`layer.open`打开一个对话框时,你可以为特定的操作或状态变化添加自定义事件,以便在这些事件发生时执行你自己的代码逻辑。 以下是一些关键的自定义事件: 1. `load`(加载完成):当对话框内容(如HTML字符串或加载外部URL的内容)完全加载完毕时触发。 2. `show`(显示完成):当对话框从隐藏状态变为可见状态时触发。 3. `close`(关闭):当对话框被用户点击关闭按钮或通过程序调用`layer.close`关闭时触发。 4. `resize`(尺寸改变):当对话框大小发生变化时触发。 5. `dragend`(拖拽结束):当对话框拖动结束时触发。 6. `form`(表单提交):如果对话框包含表单并且被提交时触发。 要添加自定义事件,你需要在`layer.open`的配置对象指定事件名称和对应的回调函数。例如: ```javascript layer.open({ type: 1, // 1代表对话框类型 title: '自定义事件示例', content: '这是一个弹出框', btn: ['确定', '取消'], // 可选按钮数组 shadeClose: true, // 是否点击遮罩关闭 area: ['auto', 'auto'], // 指定宽高,单位是px,默认是整个浏览器窗口 events: { 'click .custom-btn': function(index, layero, elem) { // 点击特定按钮 // 这里写你的自定义操作 }, 'form.formId.submit': function(index, form) { // 表单提交 // 表单验证并处理数据 }, close: function(index, type) { // 关闭时执行 console.log('对话框被关闭'); } } }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值