因为项目需要,需要将ZXY转换为BBox来获取瓦片,经过一番折腾,终于给实现了。网上关于它的资料也不是很多,所以做以下分享!
- 首先要得到ZXY数据,就需要通过source的tileUrlFunction来获取
let self = this;
const TileLayer = new Tile({
source: new XYZ({
const proj3857 = new olProj.get('EPSG:3857');
const proj3857Extent = proj3857.getExtent();
tileUrlFunction: function(tileCoord) {
const z = tileCoord[0];
const x = tileCoord[1];
const y = Math.abs(tileCoord[2]);
const origin = getTopLeft(proj3857Extent); //地图左上角坐标
const bbox = self.zxyToBbox(z, x, y, origin); //此处返回的就是算好的bbox值
return `瓦片的url`;
}
})
- 将ZXY转换为BBox的函数
zxyToBbox(z, x, y, origin) {
let minX, minY, maxX, maxY;
let size = 256 * this.zoomReses[z];
minX = origin[0] + x * size;
maxX = origin[0] + x * size + size;
minY = origin[1] - y * size - size;
maxY = origin[1] - y * size;
return [minX, minY, maxX, maxY].join(',');
},
- 最后将tile添加到map中就可以了