Change the opacity of the label at a specific magnification in the windows

I have ~ 100,000 functions displayed on the map. At a certain zoom level, I want to add a shortcut above the functions. Here's the untagged style:

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

I tried to create a function created to create a new style, to add a label

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
            })
        });
    }

And when I reached the desired level, I tried layer.forEachFeature and 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'))

                ]);
            });

        }

But both made my application crash, due to the huge number of functions that I think ... Therefore, I thought about opacity being 0 on the shortcut, then opacity 1 at 15, but:

  • Is it possible?
  • How can I achieve this?
+4
source share
1 answer

Yes it is.

: 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];
  };
};
+3

Source: https://habr.com/ru/post/1617629/


All Articles