Skip to content

Commit

Permalink
Zoom to extent fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkThalheim committed Feb 5, 2019
1 parent 3b7018e commit e894d13
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/js/Control/Zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,18 @@ BKGWebMap.Control.createZoom = function () {
// Zoom to extent
function zoomToExtent() {
var extent = ol.extent.createEmpty();
var projection = map.getView().getProjection();

map.getLayers().forEach(function (layer) {
if (layer instanceof ol.layer.Group) {
layer.getLayers().forEach(function (groupLayer) {
if (groupLayer instanceof ol.layer.Vector) {
ol.extent.extend(extent, groupLayer.getSource().getExtent());
} else if (layer.extent && layer.extent instanceof Array) {
ol.extent.extend(extent, layer.extent);
}
});
} else if (layer instanceof ol.layer.Vector) {
ol.extent.extend(extent, layer.getSource().getExtent());
} else if (layer.extent && layer.extent instanceof Array) {
ol.extent.extend(extent, layer.extent);
}
var layerExtent = BKGWebMap.Util.getLayerExtent(layer, projection);
ol.extent.extend(extent, layerExtent);
});
// TODO: doesn't work with different projections
map.getView().fit(extent, map.getSize());

// clip to projection extent?
extent = ol.extent.getIntersection(extent, projection.getExtent());

// TODO: check if configured view extent is respected.
map.getView().fit(extent);
}

// Activate history function
Expand Down
38 changes: 38 additions & 0 deletions src/js/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,41 @@ if (!('remove' in Element.prototype)) {

window.CustomEvent = CustomEvent;
}());


/**
* Returns the extent of the given layer. Depending on the layer type the
* access to the extent may vary.
*
* @param {ol.layer.Layer} layer - the layer from which to retrieve the extent
* @param {ol.proj.Projection} projection - optional target projection for layer
* @returns {ol.Extent}
*/
BKGWebMap.Util.getLayerExtent = function (layer, projection) {
var extent;
if (layer instanceof ol.layer.Group) {
extent = ol.extent.createEmpty();
layer.getLayers().forEach(function (subLayer) {
var subLayerExtent = BKGWebMap.Util.getLayerExtent(subLayer, projection);
ol.extent.extend(extent, subLayerExtent);
});
return extent;
}

// layer has no source. e.g. EMPTY
if(layer.getSource() == null)
return ol.extent.createEmpty();

if (layer instanceof ol.layer.Vector) {
extent = layer.getSource().getExtent();
} else if (layer.extent && layer.extent instanceof Array) {
extent = layer.extent;
}

var srcProjection = layer.getSource().getProjection();
if (projection && srcProjection != null) {
extent = ol.proj.transformExtent(extent, srcProjection, projection);
}

return extent;
};

0 comments on commit e894d13

Please sign in to comment.