index.js (2522B)
1 import L from 'leaflet'; 2 3 import {fetch} from '~/lib/xhr-promise'; 4 5 function tile2quad(x, y, z) { 6 let quad = ''; 7 for (let i = z; i > 0; i--) { 8 let digit = 0; 9 const mask = 1 << (i - 1); 10 if ((x & mask) !== 0) { 11 digit += 1; 12 } 13 if ((y & mask) !== 0) { 14 digit += 2; 15 } 16 quad += digit; 17 } 18 return quad; 19 } 20 21 const BingBaseLayer = L.TileLayer.extend({ 22 getTileUrl: function (tilePoint) { 23 const data = { 24 quadkey: tile2quad(tilePoint.x, tilePoint.y, this._getZoomForUrl()), 25 }; 26 return L.Util.template(this._url, L.extend(data, this.options)); 27 }, 28 }); 29 30 const BingBaseLayerWithDynamicUrl = BingBaseLayer.extend({ 31 initialize: function (options) { 32 BingBaseLayer.prototype.initialize.call(this, null, options); 33 this.layerInfoRequested = false; 34 }, 35 36 onAdd: function (map) { 37 this.loadLayerInfo(); 38 L.TileLayer.prototype.onAdd.apply(this, [map]); 39 }, 40 41 _update: function () { 42 if (this._url === null || !this._map) { 43 return; 44 } 45 L.TileLayer.prototype._update.apply(this); 46 }, 47 48 getLayerUrl: async function () { 49 throw new Error('Not implemented'); 50 }, 51 52 loadLayerInfo: async function () { 53 if (this.layerInfoRequested) { 54 return; 55 } 56 this.layerInfoRequested = true; 57 this._url = await this.getLayerUrl(); 58 this._update(); 59 }, 60 }); 61 62 const BingSatLayer = BingBaseLayerWithDynamicUrl.extend({ 63 getLayerUrl: async function () { 64 const xhr = await fetch('https://www.bing.com/maps/style?styleid=aerial', { 65 responseType: 'json', 66 timeout: 5000, 67 }); 68 return xhr.response['sources']['bing-aerial']['tiles'][0].replace(/^raster/u, 'https'); 69 }, 70 }); 71 72 const BingOrdnanceSurveyLayer = BingBaseLayerWithDynamicUrl.extend({ 73 options: { 74 credentials: 'Auy875gcaw3RCFzVQSxi8Ytzw_K67r4Dw8DpGHavRZW_ciCBHLhQJAhCiXSdnzwH', 75 }, 76 77 getLayerUrl: async function () { 78 const xhr = await fetch('https://www.bing.com/maps/style?styleid=ordnancesurvey', { 79 responseType: 'json', 80 headers: [['accept-language', 'en-GB']], 81 timeout: 5000, 82 }); 83 return xhr.response['sources']['osMaps1']['tiles'][0].replace(/^raster/u, 'https'); 84 }, 85 }); 86 87 // eslint-disable-next-line import/no-unused-modules 88 export {BingSatLayer, BingOrdnanceSurveyLayer, BingBaseLayerWithDynamicUrl};