ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

openlayers 框选(画图)得到在选款内的要素,并标注出这些要素的名称 Demo (可直接运行)

2020-11-24 12:00:52  阅读:434  来源: 互联网

标签:要素 style ol 选款 Demo feature source var new


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/v5.3.0/build/ol.js"></script>
  <title>Document</title>
</head>
<body>
  <div id="map"></div>
  <div id="selectedFeatures"></div>
  <script>
      var beijing = ol.proj.fromLonLat([116.28, 39.54]);
      var map = new ol.Map({
          target: 'map',
          layers: [
          new ol.layer.Tile({  
            source: new ol.source.OSM()
          })  
          ],
          view: new ol.View({
              center: beijing,
              zoom: 4
          })
      });

      //实例化矢量点要素,通过矢量图层添加到地图容器中
      //这样就实现了预先加载图文标注
      var iconFeature = new ol.Feature({
          geometry: new ol.geom.Point(beijing),
          name: '北京市',                         //名称属性
          population: 2115                       //人口数(万)
      });
      //设置点要素样式
      iconFeature.setStyle(createLabelStyle(iconFeature));
      //矢量标注的数据源
      var vectorSource = new ol.source.Vector({
          features: [iconFeature]
      });
      //矢量标注图层
      var vectorLayer = new ol.layer.Vector({
          source: vectorSource
      });
      map.addLayer(vectorLayer);

      //矢量标注样式设置函数,设置image为图标ol.style.Icon
      function createLabelStyle(feature){
        //   console.log(feature);
          return new ol.style.Style({
              image: new ol.style.Icon({
                  anchor: [0.5, 60],              //锚点
                  anchorOrigin:'top-right',       //锚点源
                  anchorXUnits: 'fraction',       //锚点X值单位
                  anchorYUnits: 'pixels',         //锚点Y值单位
                  offsetOrigin: 'top-right',      //偏移原点
                  opacity: 0.75,
                  scale: 0.05,
                  src: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1594359193211&di=1d6bb819a5daa724ff65cc4d011d4d42&imgtype=0&src=http%3A%2F%2Fku.90sjimg.com%2Felement_pic%2F17%2F10%2F27%2F05dc60e54e3aa5d093cdc32611303643.jpg'  //图标的URL
              }),
              text: new ol.style.Text({
                  textAlign: 'center',            //位置
                  textBaseline: 'bottom',         //基准线
                  font: 'normal 12px 微软雅黑',    //文字样式
                  text: feature.get('name'),      //文本内容
                  fill: new ol.style.Fill({       //文本填充样式(即文字颜色)
                      color: '#000'
                  }),
                  stroke: new ol.style.Stroke({
                      color: '#F00', 
                      width: 2
                  })
              })
          });
      }
    var coordinate1 = [10806361.310845079, 3942927.667062532];        //鼠标单击点的坐标
          var coordinate2 =  [11540156.782382771, 4539747.983913189]      //鼠标单击点的坐标
          var coordinate3 =  [12225032.55581795, 3982063.4255445423]      //鼠标单击点的坐标
          var arr =[coordinate1,coordinate2,coordinate3]
          //新建一个要素ol.Feature
          arr.forEach((ar,index) => {
              var newFeature = new ol.Feature({
                  geometry: new ol.geom.Point(ar),  //几何信息
                  name: '标注点'+index
              });
              newFeature.setStyle(createLabelStyle(newFeature));      //设置要素样式
              vectorSource.addFeature(newFeature);  
          });
      
var draw = new ol.interaction.Draw({
    source: vectorLayer.getSource(),
    type:"Circle",
    style:new ol.style.Style({
                // 将点设置成圆形样式
                image: new ol.style.Circle({
                    // 点的颜色
                    fill: new ol.style.Fill({
                        color: '#F00'
                    }),
                    // 圆形半径
                    radius: 5
                }),
                // 线样式
                stroke: new ol.style.Stroke({
                    color: '#0F0',
                    lineCap: 'round',       // 设置线的两端为圆头
                    width: 5                
                })
            }),
            geometryFunction: ol.interaction.Draw.createBox() // 使画出来的现状为矩形

});
    map.addInteraction(draw);
draw.on('drawend',function(evt){
    var polygon = evt.feature.getGeometry();
    setTimeout(function(){              //如果不设置延迟,范围内要素选中后自动取消选中,具体原因不知道
        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 += "<div class=\"selectedItem\" οnclick='showDeviceOnMap(\""+features[i].getId()+"\");'>"+features[i].get("name")+"</div>";
             }   
        }
        document.getElementById('selectedFeatures').innerHTML = str
    },300)
})
  </script>
</body>
</html>

标签:要素,style,ol,选款,Demo,feature,source,var,new
来源: https://www.cnblogs.com/wwj007/p/14029494.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有