###请求地图瓦片的时候,在请求头是加token再去请求
initMapFn () { this.$nextTick(() => { if (!this.mapInstance) { this.mapInstance = new Map({ controls: defaultControls({ zoom: false, rotate: false, }).extend([new ScaleLine()]), target: this.$refs.map, layers: [ new TileLayer({ source: this.sourceMaoHeaderFn() }), ], loadTilesWhileAnimating: true, interactions: new defaults({ doubleClickZoom: false, }), view: new View({ projection: "EPSG:3857", center: this.gisPosTransform1(this.centerCoodinate), zoom: 4, minZoom: 2, maxZoom: 8 }), }); } }) },
sourceMaoHeaderFn () { let source = new XYZ({ url: process.env.VUE_APP_BASE_API + '/underwater-sound/maps/{z}/{x}/{y}.png', }) source.setTileLoadFunction((tile, src) => { const xhr = new XMLHttpRequest(); xhr.responseType = 'blob'; xhr.addEventListener('loadend', function (evt) { const data = this.response; if (data !== undefined) { tile.getImage().src = URL.createObjectURL(data); } else { tile.setState(TileState.ERROR); } }); xhr.addEventListener('error', function () { tile.setState(TileState.ERROR); }); xhr.open('GET', src); xhr.setRequestHeader('Authorization', `Bearer ${this.$store.state.token}`); xhr.send(); }); return source },
|