超图服务打印Tainted canvases may not be exported错误解决方案

319 阅读1分钟

最近做了一个项目,使用超图地图打印功能时,始终提示Failed to executed 'toBlob' on 'HTMLCanvasElement':Tainted canvases may not be exported

打印报错代码:

let tile = new ol.layer.Tile({
    source:new ol.source.TileSuperMapRest({
        url:'超图服务地址'wrapX:true
    })
    projection:"EPSG:4326"
})
map.addLayer(tile)

打印时报错是因为跨域导致地图画布已经被污染,导致报错,只需在初始化地图切片时,所指向的源设置为允许跨域即可。

解决方案:

let tile = new ol.layer.Tile({
    source:new ol.source.TileSuperMapRest({
        crossOrigin:'anonymous',  //解决问题关键点
        url:'超图服务地址'wrapX:true
    })
    projection:"EPSG:4326"
})
map.addLayer(tile)