在Openlayers中特定缩放级别下更改标签的不透明度

4

我有大约100,000个特征在地图上显示。在特定的缩放级别下,我想在特征上方添加标签。这是没有标签的样式:

    style: function(feature, resolution) {
         var style = [new ol.style.Style({
               image: new ol.style.Icon({
               src: myImg,
                rotation: myRotation
           })
   })]};

我尝试创建一个函数来创建新的样式,以便添加标签。
labelStyleFunction: function(name){
         return new ol.style.Style({
            text: new ol.style.Text({
                text: name,
                font: ' 10px Arial',
                fill: new ol.style.Fill({
                    color: 'black'
                }),
                offsetY: -10,
                offsetX: 30
            })
        });
    }

当我达到所需的特定级别时,我尝试使用layer.forEachFeature和layer.forEachFeatureInExtent。

if(zoom >= 15 && status){
            me.getData('clusters100').getSource().getSource().forEachFeatureInExtent(extent,function(feature){
                feature.setStyle([
                    //me.getData('selectedStyle'),
                    me.labelStyleFunction(feature.get('name'))

                ]);
            });

        }

但我猜这是因为功能太多导致我的应用程序崩溃了...所以我想在标签上设置不透明度为0,然后在缩放到15时设置不透明度为1,但是:

  1. 这是否可行?
  2. 我该如何实现?
1个回答

3

是的,是这样的。

看看这个例子:http://openlayers.org/en/v3.11.2/examples/vector-labels.html。稍微缩小一点。你会看到标签消失了。在该示例中,这由'MaxReso'属性控制。以下是它的工作原理。

样式函数在每次渲染要素时都会被调用,并接收当前地图分辨率。使用最大分辨率设置,您可以控制是否为该特定分辨率添加文本样式。您还可以基于此进行任何其他类型的自定义。

以下是该示例中包含此功能的摘录。文本设置为'',足以使其不被渲染。搜索LOOK HERE部分:

var getText = function(feature, resolution, dom) {
  var type = dom.text.value;
  var maxResolution = dom.maxreso.value;
  var text = feature.get('name');

  // == LOOK HERE, this is where the text is set to '' ==
  if (resolution > maxResolution) {
    text = '';
  } else if (type == 'hide') {
    text = '';
  } else if (type == 'shorten') {
    text = text.trunc(12);
  } else if (type == 'wrap') {
    text = stringDivider(text, 16, '\n');
  }

  return text;
};

var createTextStyle = function(feature, resolution, dom) {
  var align = dom.align.value;
  var baseline = dom.baseline.value;
  var size = dom.size.value;
  var offsetX = parseInt(dom.offsetX.value, 10);
  var offsetY = parseInt(dom.offsetY.value, 10);
  var weight = dom.weight.value;
  var rotation = parseFloat(dom.rotation.value);
  var font = weight + ' ' + size + ' ' + dom.font.value;
  var fillColor = dom.color.value;
  var outlineColor = dom.outline.value;
  var outlineWidth = parseInt(dom.outlineWidth.value, 10);

  return new ol.style.Text({
    textAlign: align,
    textBaseline: baseline,
    font: font,
    text: getText(feature, resolution, dom),
    fill: new ol.style.Fill({color: fillColor}),
    stroke: new ol.style.Stroke({color: outlineColor, width: outlineWidth}),
    offsetX: offsetX,
    offsetY: offsetY,
    rotation: rotation
  });
};

// Points
// == LOOK HERE - this is the style function definition, which ==
// == receives the 'resolution' property                       ==
var createPointStyleFunction = function() {
  return function(feature, resolution) {
    var style = new ol.style.Style({
      image: new ol.style.Circle({
        radius: 10,
        fill: new ol.style.Fill({color: 'rgba(255, 0, 0, 0.1)'}),
        stroke: new ol.style.Stroke({color: 'red', width: 1})
      }),
      text: createTextStyle(feature, resolution, myDom.points)
    });
    return [style];
  };
};

哦,非常感谢,太棒了!刚刚检查了渲染文本的缩放级别,效果完美 :) - So4ne

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接