My OpenLayers3 map includes an OSM Tile layer and one or more Vector layers. I have a function that allows me to zoom into a single layer successfully:
vector.addEventListener("change", function() {
map.getView().fitExtent(vectorSource.getExtent(), map.getSize());
});
However, when attempting to zoom to cover multiple layers using the code below within the loop that adds layers:
vector.addEventListener("change", function () {
if (bounds == null) {
bounds = vectorSource.getExtent();
} else {
bounds = bounds.extend(vectorSource.getExtent());
}
map.getView().fitExtent(bounds, map.getSize());
});
where 'bounds' is declared outside the loop, the zoom functionality works well for maps with a single layer but gives an error "undefined is not a function" for maps with multiple layers.
As per the documentation, getExtent() returns an extent object and the extent object has an extend method. Despite this, I am encountering errors and unsure of the cause.