更改标签在特定缩放inOpenlayers上的不透明度

时间:2015-11-26 14:23:28

标签: javascript openlayers-3

我在地图上显示了~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 个答案:

答案 0 :(得分: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];
  };
};