javascript - Openlayers3 : How to add a colored border around a marker icon?

标签 javascript gis openlayers openlayers-3

OpenLayers 3 将其标记图标渲染为 Canvas 上的图像。标记可以具有带有非常规图像图标的透明背景。是否可以使用 ol.interaction.Select 添加特定厚度和颜色的彩色边框?

最佳答案

所以我能够通过修改 Icon Pixel Operations 来实现这一点OpenLayers 网站上的示例。可以在 ol.interaction.select 提供的 styleFunction 中使用图标图像 Canvas ,首先创建与图标形状相同的彩色填充,然后将图标写回其上,实质上为图标提供了彩色边框。看起来像这样:

非事件状态:

enter image description here

enter image description here

工作 CodePen .

这是修改后的样式函数:

style: function(feature) {
    var image = feature.get('style').getImage().getImage();

      var canvas = document.createElement('canvas');
      var ctx = canvas.getContext('2d');
      var activeColor = "red"; //set border color
      var dArr = [-1,-1, 0,-1, 1,-1, -1,0, 1,0, -1,1, 0,1, 1,1], // offset array
          s = 2,  // thickness scale
          i = 0,  // iterator
          x = 2,  // final x position
          y = 2;  // final y position

      //set new canvas dimentions adjusted for border
      canvas.width = image.width + s + s;
      canvas.height = image.height + s + s;

      // draw images at offsets from the array scaled by s
      for(; i < dArr.length; i += 2)
      ctx.drawImage(image, x + dArr[i]*s, y + dArr[i+1]*s);

      // fill with color
      ctx.globalCompositeOperation = "source-in";
      ctx.fillStyle = activeColor;
      ctx.fillRect(0,0,canvas.width, canvas.height);

      // draw original image in normal mode
      ctx.globalCompositeOperation = "source-over";
      ctx.drawImage(image, x, y,image.width, image.height);

      //create new openlayers icon style from canvas
      return new ol.style.Style({
          image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
          crossOrigin: 'anonymous',
          src: undefined,
          img: canvas,
          imgSize: canvas ? [canvas.width, canvas.height] : undefined
        }))
      });

  }

完整代码:

function createStyle(src, img) {
  return new ol.style.Style({
    image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
      crossOrigin: 'anonymous',
      src: src,
      img: img,
      imgSize: img ? [img.width, img.height] : undefined
    }))
  });
}

var iconFeature = new ol.Feature(new ol.geom.Point([0, 0]));
iconFeature.set('style', createStyle('https://openlayers.org/en/v4.2.0/examples/data/icon.png', undefined));

var map = new ol.Map({
  layers: [
     new ol.layer.Tile({
      source: new ol.source.OSM()
    }),
    new ol.layer.Vector({
      style: function(feature) {
        return feature.get('style');
      },
      source: new ol.source.Vector({features: [iconFeature]})
    })
  ],
  target: document.getElementById('map'),
  view: new ol.View({
    center: [0, 0],
    zoom: 3
  })
});


var select = new ol.interaction.Select({
  style: function(feature) {
    var image = feature.get('style').getImage().getImage();

      var canvas = document.createElement('canvas');
      var ctx = canvas.getContext('2d');
      var activeColor = "red"; //set border color
      var dArr = [-1,-1, 0,-1, 1,-1, -1,0, 1,0, -1,1, 0,1, 1,1], // offset array
          s = 2,  // thickness scale
          i = 0,  // iterator
          x = 2,  // final x position
          y = 2;  // final y position
      
      //set new canvas dimentions adjusted for border
      canvas.width = image.width + s + s;
      canvas.height = image.height + s + s;
    
      // draw images at offsets from the array scaled by s
      for(; i < dArr.length; i += 2)
      ctx.drawImage(image, x + dArr[i]*s, y + dArr[i+1]*s);
    
      // fill with color
      ctx.globalCompositeOperation = "source-in";
      ctx.fillStyle = activeColor;
      ctx.fillRect(0,0,canvas.width, canvas.height);
    
      // draw original image in normal mode
      ctx.globalCompositeOperation = "source-over";
      ctx.drawImage(image, x, y,image.width, image.height);
    
      //create new openlayers icon style from canvas
      return new ol.style.Style({
          image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
          crossOrigin: 'anonymous',
          src: undefined,
          img: canvas,
          imgSize: canvas ? [canvas.width, canvas.height] : undefined
        }))
      });
    
  }
});
map.addInteraction(select);
<link href="https://openlayers.org/en/v4.2.0/css/ol.css" rel="stylesheet"/>
<script src="https://openlayers.org/en/v4.2.0/build/ol.js"></script>

<div id="map" class="map"></div>

关于javascript - Openlayers3 : How to add a colored border around a marker icon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44738247/

相关文章:

javascript - OpenLayers 不允许我添加 Popup

kubernetes - 使用Kubernetes调整资源以进行GIS应用

database - 我在哪里可以获得所有国家/地区的邮政编码?

javascript - 在 React 中使用 map 返回一个等于最后一个元素的数组

javascript - 从 Firefox 插件访问文件系统/目录路径

snowflake-cloud-data-platform - 如何在 Snowflake 中将 MultiPoint 转换为 LineString?

javascript - OpenLayers/OpenStreetMap 远足自行车 map

javascript - OpenLayers 中的混合缩放?

javascript - 使用 useEffect 上的方法时遇到困难、缺少依赖项和 useCallback 错误?

PHP 数组转换为 javascript 数组