Search code examples
javascriptopenlayersopenlayers-3

How to hide and show features in OpenLayers 3? (Redraw?)


I'm updating a project from OL2 to OL3, but I'm stuck on how to redraw features after changing the feature style.

In OL2, this worked:

hidePoints: function(id) {
    if (! this.getMap().center) {
        return;
    }

    var i,
    feature,    
    len = this.points.features.length;

   if (id !== null) {
    for( i = 0; i < len; i++ ) {         
      feature = this.points.features[i];
      if (feature.attributes.aces_id == id) {
          feature.style = null;
        } else {
            feature.style = { display: 'none' };
        }
      }
   } else {
      for( i = 0; i < len; i++ ) {         
        feature = this.points.features[i];
        feature.style = { display: 'none' };
      }
   }
 this.points.redraw();
},

In OL3, I tried updating the function to hide the points layer, but redraw() no longer exists and since the layer I am working with is an ol.layer.Vector, I can't find any updateParams options like other sources besides vectors have. Dispatch Event and changed also did not work. I was hoping changed would, but nothing happens.

hidePoints: function(id) {
    if (! this.getMap().getView().getCenter()) {
        return;
    }

    var i,
        feature,
        layerSourceFeatures = this.pointsLayer.getSource().getFeatures(),
        len = layerSourceFeatures.length;

    if (id !== null) {
        for( i = 0; i < len; i++ ) {
            feature = this.pointsLayer.getSource().getFeatures()[i];

            if (feature.get('aces_id') == id) {
                feature.style = null;
            } else {
                feature.style = { display: 'none' };
            }
        }
    } else {
        for( i = 0; i < len; i++ ) {
            feature = this.pointsLayer.getSource().getFeatures()[i];
            feature.style = { display: 'none' };
        }
    }
    //this.pointsLayer.redraw();
    //this.pointsLayer.dispatchEvent(goog.events.EventType.CHANGE);
    this.pointsLayer.changed();
},

I'm also wondering if changing the style is done this way (fetching each feature to another var) or if that won't just change that feature and leave the original untouched. Plus always fetching getSource().getFeatures() seems abusive on the performance... but I can't seem to find another way.

Regardless, how is redraw performed in OL3 now to render features whose styles have been altered? A layer can be set as visible, but I don't want to hide/show all the features all the time. Sometimes I just want to hide/show a few according to their given id.


Solution

  • So while looking at the documentation over and over, I finally found what would fire the change event, much like seto suggested after.

    This is the converted function from OL2 to OL3 that works for me. Redraw is no longer needed since setStyle does it all.

    hidePoints: function(id) {
        if (! this.getMap().getView().getCenter()) {
            return;
        }
    
        var i,
            feature,
            layerSourceFeatures = this.pointsLayer.getSource().getFeatures(),
            len = layerSourceFeatures.length;
    
        var emptyImgStyle = new ol.style.Style({ image: '' });
    
        // If an aces id was provided
        if (id !== undefined) {
            for( i = 0; i < len; i++ ) {
                feature = layerSourceFeatures[i];
    
                feature.setStyle(emptyImgStyle);
    
                // Resetting feature style back to default function given in defaultPointStyleFunction()
                if (feature.get('aces_id') == id) {
                    feature.setStyle(null);
                }
                // Hiding marker by removing its associated image
                else {
                    feature.setStyle(emptyImgStyle);
                }
            }
        }
        // No id was provided - all points are hidden
        else {
            for( i = 0; i < len; i++ ) {
                feature = layerSourceFeatures[i];
                feature.setStyle(emptyImgStyle);
            }
        }
    },