質問

Is there an easy way (other than getting layer extents separately and doing the calculation) to group the layers and zoom to the extent that is best for displaying shapes on all of the grouped layers?

役に立ちましたか?

解決

Solution:

bounds = @get('siblingsLayer').getDataExtent()
bounds.extend(@get('vectorLayer').getDataExtent())
bounds.extend(@get('parentLayer').getDataExtent())

@get('map').zoomToExtent(bounds)

他のヒント

This one worked for me.

Code:

var allLayers = map.getLayers();
var length = allLayers.getLength();
var layerArray = ['Layer1', 'Layer2', ..., 'LayerN'];
var extent = ol.extent.createEmpty();
for (var i = 0; i < length; i++) {
    var existingLayer = allLayers.item(i);
    for (var j = 0; j < layerArray.length; j++) {
        if (existingLayer.get('title') == layerArray[j]) {
            ol.extent.extend(extent,existingLayer.getSource().getExtent());
        }
    }
}
map.getView().fit(extent, map.getSize());

Example:

I have added two layers and pointed to one layer among them.

Map snapshot with one layer in focus

After calling the API, my view updated and focused to both the layers:

Map snapshot with both the layer in focus

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top