Box Selection——盒形方式选择


This example shows how to use a DragBox interaction to select features. Selected features are added to the feature overlay of a select interaction (ol.interaction.Select) for highlighting.

Use Ctrl+Drag (Command+Drag on Mac) to draw boxes.

这个例子用来展示如何使用DragBox交互控件来选择要素,选中的要素被添加到选择交互控件(ol.interaction.Select)
的要素叠加层中来进行高亮显示。

使用Ctrl+Drag (Mac系统使用Command+Drag)来绘制盒形。

代码:

<!DOCTYPE html>
<html>
  <head>
    <title>Box Selection</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.2.0/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.2.0/build/ol.js"></script>
    <style>
      .ol-dragbox {
        background-color: rgba(255,255,255,0.4);
        border-color: rgba(100,150,0,1);
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <div id="info">No countries selected</div>
    <script>
      //加载GeoJson格式的矢量数据
     var vectorSource = new ol.source.Vector({
        url: 'https://openlayers.org/en/v4.2.0/examples/data/geojson/countries.geojson',
        format: new ol.format.GeoJSON()
      });


      var map = new ol.Map({
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          }),
          new ol.layer.Vector({
            source: vectorSource
          })
        ],
        target: 'map',
        view: new ol.View({
          center: [0, 0],
          zoom: 2
        })
      });

      //创建选择交互控件
      var select = new ol.interaction.Select();
      map.addInteraction(select);

      //获取所有选择的要素
      var selectedFeatures = select.getFeatures();

      //创建DragBox交互控件
      var dragBox = new ol.interaction.DragBox({
         //ol.events.condition.platformModifierKeyOnly
         //Return true if only the platform-modifier-key (the meta-key on Mac, ctrl-key otherwise) is pressed,
         //如果只按下platform-modifier-key(Mac平台是meta-key,其他平台是ctrl-key)则返回true
         //false otherwise
         //其他都返回false
        condition: ol.events.condition.platformModifierKeyOnly
      });

      map.addInteraction(dragBox);

      dragBox.on('boxend', function() {
        //获取当前绘制的范围
        var extent = dragBox.getGeometry().getExtent();
        //遍历当前范围内的每一个要素,并把它添加到selectedFeatures集合中
       vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {
          selectedFeatures.push(feature);
        });
      });

      //每次绘制之前清除selectedFeatures中的所有要素
      dragBox.on('boxstart', function() {
        selectedFeatures.clear();
      });

      var infoBox = document.getElementById('info');

       selectedFeatures.on(['add', 'remove'], function() {
        //遍历selectedFeatures中的每一个要素,并返回每一个要素的名称
       var names = selectedFeatures.getArray().map(function(feature) {
          return feature.get('name');
        });
        //如果names数组的长度大于0,则把它用逗号分隔添加到id为info的DIV标签中
       if (names.length > 0) {
          infoBox.innerHTML = names.join(', ');
        } else {
          infoBox.innerHTML = 'No countries selected';
        }
      });
    </script>
  </body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSS(层叠样式表)是一种用于描述网页上元素样式的语言。在CSS中,盒模型是一个重要的概念,它定义了HTML元素在页面中的布局和样式。 盒模型由四个主要部分组成:内容区域、内边距、边框和外边距。下面对每个部分进行详细说明: 1. 内容区域(content):内容区域是盒模型中实际显示内容的部分,例如文本、图像等。它的大小由内容的宽度和高度决定。 2. 内边距(padding):内边距是位于内容区域和边框之间的空白区域。可以使用padding属性来设置内边距的大小。内边距可以为正值或负值,它会影响元素的尺寸和位置。 3. 边框(border):边框是包围内容区域和内边距的线条或样式。可以使用border属性来设置边框的样式、宽度和颜色。 4. 外边距(margin):外边距是位于元素边框外部的空白区域。可以使用margin属性来设置外边距的大小。外边距也可以为正值或负值,它会影响元素与其他元素之间的间距。 在CSS中,可以使用box-sizing属性来控制盒模型的计算方式。默认情况下,box-sizing属性的值为content-box,即宽度和高度只包括内容区域。如果将box-sizing属性的值设置为border-box,那么宽度和高度将包括内容区域、内边距和边框。 了解盒模型的概念对于理解和掌握CSS布局和样式非常重要。可以通过设置元素的宽度、高度、内边距、边框和外边距来实现不同的布局效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值