- Using OpenLayers
^7.4.0
- Trying to get tiles from 2 different providers one gives 256 tileSize images and the other 512
- After zoom level 18 or user event, i shift my provider
- Using new XYZ to create the source of each tileLayer
Code for reference:
- Tile Layer 1 from first source with 512 tilesize
const extent = [12445171.1973, -5518141.946, 17346924.9472, -939258.2036];
const startResolution = getWidth(extent) / 512;
const resolutions = new Array(20 + 1);
resolutions[0] = startResolution;
for (let i = 1, ii = 20; i <= ii; ++i) {
resolutions[i] = resolutions[i - 1] / 2;
}
const mabBox512Source = new XYZ({
tilePixelRatio: hiDPI ? 2 : 1,
tileSize: [512, 512],
tileGrid: new TileGrid({
extent: extent,
resolutions: resolutions,
tileSize: [512, 512],
}),
maxZoom: 20,
tileUrlFunction: function (tileCoord) {
const z = tileCoord[0];
const x = tileCoord[1];
const y = tileCoord[2];
const mapBoxURl = `https://api.mapbox.com/style...............`;
return mapBoxURl;
},
});
const tileLayer1 = new OLTileLayer({
source: mabBox512Source,
properties: {
name: 'BASE_TILE_LAYER_1',
},
});
- Tile Layer 2 from other source with 256 tilesize
const startResolution2 = getWidth(extent) / 256;
const resolutions2 = new Array(20 + 1);
resolutions2[0] = startResolution2;
for (let i = 1, ii = 20; i <= ii; ++i) {
resolutions2[i] = resolutions2[i - 1] / 2;
}
const newMap256Source = new XYZ({
tilePixelRatio: hiDPI ? 2 : 1,
tileSize: [256, 256],
tileGrid: new TileGrid({
extent: extent,
resolutions: resolutions2,
tileSize: [256, 256],
}),
maxZoom: 20,
tileUrlFunction: function (tileCoord) {
const z = tileCoord[0];
const x = tileCoord[1];
const y = tileCoord[2];
const nearMapUrl = `https://api.nearmap.com/tiles/.....`;
return nearMapUrl;
},
})
const tileLayer2 = new OLTileLayer({
source: newMap256Source,
properties: {
name: 'BASE_TILE_LAYER_1',
},
visible: false,
});
And on some condition/userAction in a callback function i am doing the below
console.log("-----------Switch Layer---------");
tileLayer1.setVisible(false);
tileLayer3.setVisible(true);
What is going wrong
I suspect this is not the right way to do it, as the map stops supporting some configuration rules. Like it is not respecting the maxZoom that i had applied. This is a visible breakage. There is a possibility of other breakages which i might not know.
Also, i had tried a different way, where i kept a single tileLayer but i switched the source on user Action/conndition(zoom>18), using setSource.
With help from @Mike , the solution for using two different tileSizes at the same time in the same map instance in openlayers is,
Read the code snippet below.