UNPKG

load-map

Version:

地图SDK加载器,支持腾讯地图和百度地图,可用于vue和react项目。

203 lines (164 loc) 5.37 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.loadMap = factory()); }(this, (function () { 'use strict'; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } var LoadMap = /*#__PURE__*/function () { function LoadMap(version, url) { _classCallCheck(this, LoadMap); this.version = version; this.url = url; this.query = {}; this.randomNum = this.__getRandomNum(); this.callbackKey = 'loadMapCb' + this.randomNum; this.scriptId = 'loadMap' + this.randomNum; this.__getType(); this.__getWinKey(); } _createClass(LoadMap, [{ key: "__init", value: function __init(_ref) { var version = _ref.version, mapKey = _ref.mapKey, url = _ref.url, query = _ref.query; if (!mapKey) { throw new Error('必须配置mapKey'); } this.mapKey = mapKey; if (version) { this.version = version; } if (url) { this.url = url; } if (query && Object.keys(query).length) { this.query = query; } this.__getType(); this.__getWinKey(); } }, { key: "__getRandomNum", value: function __getRandomNum() { return Math.ceil(Math.random() * 10000000000).toString().substr(0, 6); } }, { key: "__getType", value: function __getType() { var url = this.url; if (/qq/.test(url)) { this.type = 'qq'; } else if (/baidu/.test(url)) { this.type = 'baidu'; } else { throw new Error('不支持此类型地图'); } } // 获取绑定到window对象的key,需要先执行__getType }, { key: "__getWinKey", value: function __getWinKey() { var type = this.type, url = this.url, query = this.query; if (!type) return; var winKey; switch (type) { case 'qq': if (/\/gljs/.test(url)) { winKey = 'TMap'; } else { winKey = 'qq'; } break; case 'baidu': if (query && query.type === 'webgl') { winKey = 'BMapGL'; } else { winKey = 'BMap'; } break; default: winKey = 'qq'; } this.winKey = winKey; } }, { key: "__createScriptElement", value: function __createScriptElement() { var script = document.createElement('script'); script.type = 'text/javascript'; script.id = this.scriptId; script.src = this.__generateURL(); var head = document.getElementsByTagName('head')[0] || document.body; head.appendChild(script); } }, { key: "__generateURL", value: function __generateURL() { var query = this.query; var queryStr = ''; if (query && Object.keys(query).length) { Object.keys(query).map(function (key) { queryStr += '&' + key + '=' + encodeURIComponent(query[key]); }); } if (this.type === 'qq') { return this.url + '?v=' + this.version + '&key=' + this.mapKey + queryStr + '&callback=' + this.callbackKey; } return this.url + '?v=' + this.version + '&ak=' + this.mapKey + queryStr + '&callback=' + this.callbackKey; } }, { key: "__isLoaded", value: function __isLoaded() { var map = window[this.winKey]; if (map && map.__loadMap__) { return map; } return false; } }, { key: "load", value: function load() { var _this = this; var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; this.__init(config); var isLoaded = this.__isLoaded(); if (isLoaded) { return new Promise(function (resolve) { resolve(isLoaded); }); } this.__createScriptElement(); return new Promise(function (resolve) { window[_this.callbackKey] = function () { window[_this.winKey].__loadMap__ = true; resolve(window[_this.winKey]); }; }); } }]); return LoadMap; }(); var loadMap = new LoadMap('2.exp', 'https://map.qq.com/api/js'); return loadMap; })));