0

I have a map with some layers, and the layers have different features on them. I have a function that iterates through each layer and creates an extent from all the features' geometry extent. Then I fit the map to this extent. I want to have a different center for the map and still fit all the features in the view of the map.

Is there a function that can enlarge/extend a given extent to have a new center?

I'm using OpenLayers 4.6.4 Example of code, below, so in essence I want to give a center parameter and have a function that extends/enlarges the extent already calculated.

 /**
 * Fits all the features in a layer on the map.
 * @param {*Layer on map which should get all the features from} layer 
 */
function fitLayersFeatureOnMap(layers, center) {
    var featuresOnLayers = [];
    _.each(layers, function(layer) {
        featuresOnLayers.push(mapLayers[layer].getSource().getFeatures());
    });

    var extentOfAllFeatures = ol.extent.createEmpty();

    _.each(_.flatten(featuresOnLayers), function (feature) {
        ol.extent.extend(extentOfAllFeatures, feature.getGeometry().getExtent());
    });

    map.getView().fit(extentOfAllFeatures);
}
0

I've been adapting the answer of @lambertj to #openlayers-5

/**
 * Fits all the features of all layers on the map.
 * @author massimoi
 * @since 4/1/2019
 * @param map on which we will center
 * @param layers set of layers which will be analyzed 
 */
function centraTuttiLayer(map, layers)
{
  var featuresOnLayers = [];
  layers.forEach(function(layer) {
      featuresOnLayers.push(layer.getSource().getFeatures());
  });

  var extentOfAllFeatures = createEmpty();
  var flatFeatures = featuresOnLayers.flat();

  flatFeatures.forEach(function (feature) {
      extend(extentOfAllFeatures, feature.getGeometry().getExtent());
  });

  map.getView().fit(extentOfAllFeatures);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.