In your code, you are using getExtent
for layer1
. According to the API docs, it "returns the extent of the layer".
Then
Then if you look at what is an extent
for an ol.layer.Tile
(e.g layer1layer1
)
It's. It's "the bounding extent for layer rendering. The layer will not be rendered outside of this extent."
SoConclusion: you need to setget the extent manually...
Iffrom somewhere but as you wereare not using an ol.layer.Vector
, you couldcan't get geometries and deduce the extent (butas a WMS is not a vector layer)
To avoid settingThe way to get extent & center manually, you need"magically" is to get the "capabilities" from the WMS layer considering you already set in GeoServer the extent using the layer data (see http://docs.geoserver.org/latest/en/user/data/webadmin/layers.html#bounding-boxes)
What are "capabilities"? It's informations about available layers using OGC web Services (here WMS). You can get more detailed informations about each layer including bounds.
You can find a prototype showing how ityou can be doneuse them. As I don't own the remote demo GeoServer, I suffered some limitations in the prototype. The layer center seems to be the CRS center instead of the layer data center.