When attempting to use the WMTS tile service with Geoserver and openlayers I keep getting error 43 (Expected layers to be an array or an ol.Collection.). I am new to openlayers so the issue is likely trivial but I can't figure it out.

 var projection = ol.proj.get('EPSG:4326');
 var projectionExtent = projection.getExtent();
 var size = ol.extent.getWidth(projectionExtent) / 256;
 var mapExtent =  [-13884991, 2870341, -7455066, 6338219];
 var resolutions = new Array(22);
 var matrixIds = new Array(22);

 for (var z = 0; z < 22; ++z) {
   // generate resolutions and matrixIds arrays for this WMTS
   resolutions[z] = size / Math.pow(2, z);
   matrixIds[z] = z;

 layers = new ol.layer.Tile({
       source: new ol.source.WMTS({
       url: 'http://localhost/geoserver/gwc/service/wmts',
        layer: 'Aberdeen:TAC_ALL',
       matrixSet: 'EPSG:4326',
       format: 'image/png',
       projection: projection,
       tileGrid: new ol.tilegrid.WMTS({
           origin: ol.extent.getTopLeft(projectionExtent),
           resolutions: resolutions,
           matrixIds: matrixIds

 var map = new ol.Map({
   layers: layers,
   target: 'map',
   view: new ol.View({
     center: [-10997148, 4569099],
     zoom: 4
  • your map is in 3857 not 4326 so you either need to change it or the gridset you use – Ian Turton Feb 20 at 9:06
layers = new ol.layer.Tile({ ...

is only one layer so you need to specify

layers: [layers],

to make it an array

  • That is incredibly annoying how easy of a fix that was. Thanks – mconn Feb 20 at 13:36

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.