zoukankan      html  css  js  c++  java
  • openLayer矩形框选要素,展示要素属性

    实现的效果:

     通过绘制矩形框选要素,然后使用popup以弹窗的形式展示获取的要素属性。

    // 选择要素事件开始
          const btn_select = document.getElementById("btn_select");
          btn_select.addEventListener("click", () => {
            this.map1.removeInteraction(this.draw);
            this.map1.removeInteraction(this.snap);
            this.map1.removeLayer(this.vectorLayer2);
            this.map2.removeInteraction(this.snap);
            this.map2.removeLayer(this.vectorLayer2);
    
            this.source = new VectorSource();
            this.vectorLayer2 = new VectorLayer({
              source: this.source,
            });
            this.map1.addLayer(this.vectorLayer2);
            this.map2.addLayer(this.vectorLayer2);
            this.draw = new Draw({
              source: this.source,
              type: "Circle",
              style: new Style({
                // 将点设置成圆形样式
                image: new CircleStyle({
                  // 点的颜色
                  fill: new Fill({
                    color: "#F00",
                  }),
                  // 圆形半径
                  radius: 5,
                }),
                // 线样式
                stroke: new Stroke({
                  color: "#0F0",
                  lineCap: "round", // 设置线的两端为圆头
                   5,
                }),
              }),
    
              geometryFunction: createBox(), // 使画出来的现状为矩形
            });
            // 使用popup进行要素属性的演示
            this.overlay = new Overlay(
              /** @type {olx.OverlayOptions} */ ({
                element: container,
                autoPan: true,
                autoPanAnimation: {
                  duration: 250, //当Popup超出地图边界时,为了Popup全部可见,地图移动的速度.
                },
              })
            );
    
            this.snap = new Snap({ source: this.source });
            this.map1.addInteraction(this.draw);
            this.draw.on("drawend", (evt) => {
              //  map.removeLayer(vectorLayer2);
    
              var polygon = evt.feature.getGeometry();
              setTimeout(() => {
                var extent = polygon.getExtent();
                var features = vectorLayer.getSource().getFeaturesInExtent(extent); //先缩小feature的范围
                var str = "";
                for (var i = 0; i < features.length; i++) {
                  var newCoords = features[i].getGeometry().getCoordinates();
                  if (features[i].get("name")) {
                    str += "  " + features[i].get("name");
                  }
                }
                // document.getElementById('selectedFeatures').innerHTML = str
                container.innerHTML = "<p>你选择的要素是:</p>" + str + "";
                this.overlay.setPosition(newCoords);
                this.map2.addOverlay(this.overlay); //将选中要素的属性信息在this.overlay图层通过popup进行显示
              }, 300);
            });
          });
        },
        // 选择要素事件结束

    参考资料:https://www.cnblogs.com/wwj007/p/14029494.html

    // 选择要素事件开始
          const btn_select = document.getElementById("btn_select");
          btn_select.addEventListener("click", () => {
            this.map1.removeInteraction(this.draw);
            this.map1.removeInteraction(this.snap);
            this.map1.removeLayer(this.vectorLayer2);
            this.map2.removeInteraction(this.snap);
            this.map2.removeLayer(this.vectorLayer2);

            this.source = new VectorSource();
            this.vectorLayer2 = new VectorLayer({
              source: this.source,
            });
            this.map1.addLayer(this.vectorLayer2);
            this.map2.addLayer(this.vectorLayer2);
            this.draw = new Draw({
              source: this.source,
              type: "Circle",
              style: new Style({
                // 将点设置成圆形样式
                image: new CircleStyle({
                  // 点的颜色
                  fill: new Fill({
                    color: "#F00",
                  }),
                  // 圆形半径
                  radius: 5,
                }),
                // 线样式
                stroke: new Stroke({
                  color: "#0F0",
                  lineCap: "round"// 设置线的两端为圆头
                  width: 5,
                }),
              }),

              geometryFunction: createBox(), // 使画出来的现状为矩形
            });
            // 使用popup进行要素属性的演示
            this.overlay = new Overlay(
              /** @type {olx.OverlayOptions} */ ({
                element: container,
                autoPan: true,
                autoPanAnimation: {
                  duration: 250//当Popup超出地图边界时,为了Popup全部可见,地图移动的速度.
                },
              })
            );

            this.snap = new Snap({ source: this.source });
            this.map1.addInteraction(this.draw);
            this.draw.on("drawend", (evt=> {
              //  map.removeLayer(vectorLayer2);

              var polygon = evt.feature.getGeometry();
              setTimeout(() => {
                var extent = polygon.getExtent();
                var features = vectorLayer.getSource().getFeaturesInExtent(extent); //先缩小feature的范围
                var str = "";
                for (var i = 0i < features.lengthi++) {
                  var newCoords = features[i].getGeometry().getCoordinates();
                  if (features[i].get("name")) {
                    str += "  " + features[i].get("name");
                  }
                }
                // document.getElementById('selectedFeatures').innerHTML = str
                container.innerHTML = "<p>你选择的要素是:</p>" + str + "";
                this.overlay.setPosition(newCoords);
                this.map2.addOverlay(this.overlay); //将选中要素的属性信息在this.overlay图层通过popup进行显示
              }, 300);
            });
          });
        },
        // 选择要素事件结束
  • 相关阅读:
    VMware虚拟机下如何安装一个64位的win7系统
    无人驾驶刚刚开始的未来
    Ruby on Rails开发Web应用的基本概念
    Hibernate学习(1)简单介绍
    Linux pipe 源代码分析
    【知识梳理1】Android触摸事件机制
    【CODEFORCES】 C. Dreamoon and Strings
    LightOJ
    [leetcode]Maximum Subarray
    25个增强iOS应用程序性能的提示和技巧 — 中级篇
  • 原文地址:https://www.cnblogs.com/1gaoyu/p/15273073.html
Copyright © 2011-2022 走看看