Box Selection——盒形方式选择

来源:互联网 发布:角色数据库设计 编辑:程序博客网 时间:2024/06/06 01:21

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>


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 原地转圈头晕恶心想吐怎么办 孩子吃凉的呕吐头还晕怎么办 孩子转晕了想吐怎么办 转圈晕了想吐怎么办 我爸总是骂我妈怎么办 转圈转的想吐怎么办 大便干燥拉不出来怎么办 吹完头发很干燥怎么办 腿摔伤了结痂疼怎么办 蹭wifi被禁止后怎么办 电脑wifi给拉黑怎么办 电脑想用无线网怎么办 中路被对方打崩了怎么办 英雄联盟队友太坑怎么办 匹配被王者虐了怎么办 lol碰到嘴臭的怎么办 小婴儿脾气大怎么办呢? 玩游戏输入法会跳出出来怎么办 逆水寒fps太低怎么办 我dcj没地速怎么办 电焊看久眼睛疼怎么办 装修忘了窗帘盒怎么办? 纹眉导致眼肿了怎么办 哭泣引起的眼肿怎么办 在酒店忘记拉窗帘了怎么办 湿气重喉咙有痰怎么办 眼睛上火了肿了怎么办 陌陌直播没人看怎么办 陌陌直播没人气怎么办 我真的爱上你了怎么办 弯腰时间久了腰疼怎么办 斗鱼pk输的怎么办 领导当着人骂我怎么办 被老板骂了应该怎么办 三星s7关机键掉了怎么办 主播遇到黑粉怎么办 在工作单位突然死亡怎么办 孕7月半夜脚抽筋怎么办 上单对上两个射手怎么办 游戏本玩游戏掉帧怎么办 手机开直播很卡怎么办