5

In an openlayers 3 app, I am able to retrieve the bounding extent and fit the view. However I now want to create a feature/polygon by using the bounding extent.

    let boundingExtent = ol.extent.boundingExtent([[left, bottom], [right, top]]);

    //??/let polygon = ol.geom.Polygon.fromExtent(boundingExtent);

    var view = this.map.getView();
    view.fit(boundingExtent, null);

    //let source = this.vectorSource.getSource();
    //source.clear(); 
    //feature.setStyle(this.VectorAltStyles);
    //source.addFeatures(feature);

Using ol.geom.Polygon.fromExtent and adding the result to the vector source doesn't seem to work. Please can someone shed some light on how to accomplish this?

Kalamarico
  • 5,466
  • 22
  • 53
  • 70
vicgoyso
  • 636
  • 1
  • 14
  • 35

1 Answers1

3

Finally found a way after a lot of trial and error...

let boundingExtent = ol.extent.boundingExtent([[left, bottom], [right, top]]),
            polygon = ol.geom.Polygon.fromExtent(boundingExtent),
            feature = new ol.Feature(polygon);

        let source = this.vectorSource.getSource();
        source.clear();
        feature.setStyle(this.VectorStyles);
        source.addFeatures([feature]);
vicgoyso
  • 636
  • 1
  • 14
  • 35