【问题标题】:Change label's opacity on a specific zoom inOpenlayers在 Openlayers 的特定缩放中更改标签的不透明度
【发布时间】:2015-11-26 14:23:28
【问题描述】:

我在地图上显示了大约 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. 我怎样才能做到这一点?

【问题讨论】:

    标签: javascript openlayers-3


    【解决方案1】:

    是的。

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

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

    这是该示例中的一个 sn-p。文本设置为'',这足以让它不被渲染。搜索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];
      };
    };
    

    【讨论】:

    • 哦,非常感谢你,这太棒了!刚刚检查了缩放级别以渲染或不渲染文本,这很完美:)
    猜你喜欢
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-03-04
    • 2018-10-01
    • 2021-08-25
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多