1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| export const fileTool = { loadBMap: function (ak) { return new Promise(function (resolve, reject) { try { console.log('BMap is defined:', BMapGL === undefined || BMapGL) resolve(BMapGL) } catch (err) { window.initBMapGL = function () { resolve(BMapGL) } const BMapUrl = `http://api.map.baidu.com/api?v=1.0&type=webgl&ak=${ak}&callback=initBMapGL`; fileTool.loadScript(BMapUrl, null, reject) } }) }, loadBMapGLLib: function (ak) { return new Promise(function (resolve, reject) { fileTool.loadBMap(ak).then(BMapGL => { let loaded = false; try { loaded = (BMapGLLib && BMapGLLib.DrawingManager); } catch (err) { loaded = false; } if (!loaded) { const GLLibFile = { js: 'https://mapopen.bj.bcebos.com/github/BMapGLLib/DrawingManager/src/DrawingManager.min.js', css: 'https://mapopen.bj.bcebos.com/github/BMapGLLib/DrawingManager/src/DrawingManager.min.css', } fileTool.loadCSS(GLLibFile.css); fileTool.loadScript(GLLibFile.js, function() { return resolve({BMapGL, BMapGLLib}) }, function() { return resolve({BMapGL}) }); } }) }) }, loadScript: function (src, callback, reject) { let script = document.createElement('script'); script.type = 'text/javascript'; script.src = src; if (script.addEventListener) { script.addEventListener('load', function () { callback && callback(); }, false); script.addEventListener('error', function () { reject && reject(); }); } else if (script.attachEvent) { script.attachEvent('onreadystatechange', function () { let target = window.event.srcElement; if (target.readyState == 'loaded') { callback && callback(); } }); } document.getElementsByTagName('head')[0].appendChild(script); }, loadCSS: function (url) { let link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.url = url; document.getElementsByTagName('head')[0].appendChild(link); }, }
|