z-amap-jsapi-loader 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,114 @@
1
+ # z-amap-jsapi-loader
2
+ z-amap-jsapi-loader 是基于高德开放平台官网提供的地图 JSAPI 的加载器amap-jsapi-loader,可帮助开发者快速定位、有效避免加载引用地图 JSAPI 各种错误用法。
3
+ 如果你已使用@amap/amap-jsapi-loader,只需将安装的包名替换为z-amap-jsapi-loader
4
+ ```
5
+ 原安装:npm i @amap/amap-jsapi-loader
6
+ 原使用:import AMapLoader from '@amap/amap-jsapi-loader';
7
+
8
+ 改为
9
+ 现安装:npm i z-amap-jsapi-loader
10
+ 现使用:import AMapLoader from 'z-amap-jsapi-loader';
11
+ ```
12
+
13
+
14
+ 该加载器具有以下特性:
15
+ * 支持以 普通JS 和 npm包 两种方式使用;
16
+ * 有效避免错误异步加载导致的 JSAPI 资源加载不完整问题;
17
+ * 对于加载混用多个版本 JSAPI 的错误用法给予报错处理;
18
+ * 对于不合法加载引用 JSAPI 给予报错处理;
19
+ * 支持指定 JSAPI 版本;
20
+ * 支持插件加载;
21
+ * 允许多次执行加载操作,网络资源不会重复请求,便于大型工程模块管理;
22
+
23
+
24
+ # USAGE
25
+
26
+ ## AMapLoader.load方法参数说明
27
+ ```js
28
+ AMapLoader.load({
29
+ "key": "", // 申请好的Web端开发者Key,首次调用 load 时必填
30
+ "version": "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
31
+ "plugins": [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
32
+ "AMapUI": { // 是否加载 AMapUI,缺省不加载
33
+ "version": '1.1', // AMapUI 缺省 1.1
34
+ "plugins":[], // 需要加载的 AMapUI ui插件
35
+ },
36
+ "Loca":{ // 是否加载 Loca, 缺省不加载
37
+ "version": '1.3.2' // Loca 版本,缺省 1.3.2
38
+ },
39
+ }).then(()=>{
40
+ window.AMap.xx;
41
+ window.AMapUI.xx;
42
+ window.Loca.xx
43
+ });
44
+
45
+ ```
46
+
47
+ ## AMapUI
48
+
49
+ AMapUI 用法和官网有一点点区别。通过 AMapUI.xx 来获取组件
50
+ ```js
51
+ AMapLoader.load({
52
+ key: '',//首次load必填
53
+ version: '2.0',
54
+ AMapUI: {
55
+ version: '1.1',
56
+ plugins: ['overlay/SimpleMarker'],
57
+ }
58
+ }).then((AMap) => {
59
+ map = new AMap.Map('container');
60
+
61
+ // !!! 通过 AMap.SimpleMarker 获取组件
62
+ new AMapUI.SimpleMarker({
63
+ //前景文字
64
+ iconLabel: 'A',
65
+ //图标主题
66
+ iconTheme: 'default',
67
+ //背景图标样式
68
+ iconStyle: 'red',
69
+ map: map,
70
+ position: map.getCenter()
71
+ });
72
+ }).catch((e) => {
73
+ console.error(e);
74
+ });
75
+ ```
76
+
77
+
78
+ # 使用
79
+ #### 以普通 JS 方式使用 Loader
80
+ 尚未发布在线Loader,可将 dist/index.js 复制到项目下
81
+ ```
82
+ <script src="../dist/index.js"></script>
83
+ <script>
84
+ AMapLoader.load({
85
+ key:'',//首次load必填
86
+ version:'2.0',
87
+ plugins:['AMap.Scale']
88
+ }).then((AMap)=>{
89
+ map = new AMap.Map('container');
90
+ map.addControl(new AMap.Scale())
91
+ }).catch((e)=>{
92
+ console.error(e);
93
+ });
94
+ </script>
95
+ ```
96
+ #### 以 NPM 包方式使用 Loader
97
+ 安装
98
+ ```
99
+ tnpm i z-amap-jsapi-loader --save-dev
100
+ ```
101
+ 使用
102
+ ```
103
+ import AMapLoader from 'z-amap-jsapi-loader';
104
+
105
+ AMapLoader.load().then((AMap)=>{
106
+ map = new AMap.Map('container');
107
+ }).catch(e=>{
108
+ console.log(e);
109
+ })
110
+
111
+ ```
112
+
113
+
114
+
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ 'use strict';(function(m,p){"object"===typeof exports&&"undefined"!==typeof module?module.exports=p():"function"===typeof define&&define.amd?define(p):(m=m||self,m.AMapLoader=p())})(this,function(){function m(a){var b=[];a.AMapUI&&b.push(p(a.AMapUI));a.Loca&&b.push(r(a.Loca));return Promise.all(b)}function p(a){return new Promise(function(h,c){var f=[];if(a.plugins)for(var e=0;e<a.plugins.length;e+=1)-1==d.AMapUI.plugins.indexOf(a.plugins[e])&&f.push(a.plugins[e]);if(g.AMapUI===b.failed)c("\u524d\u6b21\u8bf7\u6c42 AMapUI \u5931\u8d25");
2
+ else if(g.AMapUI===b.notload){g.AMapUI=b.loading;d.AMapUI.version=a.version||d.AMapUI.version;e=d.AMapUI.version;var l=document.body||document.head,k=document.createElement("script");k.type="text/javascript";k.src="https://webapi.amap.com/ui/"+e+"/main.js";k.onerror=function(a){g.AMapUI=b.failed;c("\u8bf7\u6c42 AMapUI \u5931\u8d25")};k.onload=function(){g.AMapUI=b.loaded;if(f.length)window.AMapUI.loadUI(f,function(){for(var a=0,b=f.length;a<b;a++){var c=f[a].split("/").slice(-1)[0];window.AMapUI[c]=
3
+ arguments[a]}for(h();n.AMapUI.length;)n.AMapUI.splice(0,1)[0]()});else for(h();n.AMapUI.length;)n.AMapUI.splice(0,1)[0]()};l.appendChild(k)}else g.AMapUI===b.loaded?a.version&&a.version!==d.AMapUI.version?c("\u4e0d\u5141\u8bb8\u591a\u4e2a\u7248\u672c AMapUI \u6df7\u7528"):f.length?window.AMapUI.loadUI(f,function(){for(var a=0,b=f.length;a<b;a++){var c=f[a].split("/").slice(-1)[0];window.AMapUI[c]=arguments[a]}h()}):h():a.version&&a.version!==d.AMapUI.version?c("\u4e0d\u5141\u8bb8\u591a\u4e2a\u7248\u672c AMapUI \u6df7\u7528"):
4
+ n.AMapUI.push(function(a){a?c(a):f.length?window.AMapUI.loadUI(f,function(){for(var a=0,b=f.length;a<b;a++){var c=f[a].split("/").slice(-1)[0];window.AMapUI[c]=arguments[a]}h()}):h()})})}function r(a){return new Promise(function(h,c){if(g.Loca===b.failed)c("\u524d\u6b21\u8bf7\u6c42 Loca \u5931\u8d25");else if(g.Loca===b.notload){g.Loca=b.loading;d.Loca.version=a.version||d.Loca.version;var f=d.Loca.version,e=d.AMap.version.startsWith("2"),l=f.startsWith("2");if(e&&!l||!e&&l)c("JSAPI \u4e0e Loca \u7248\u672c\u4e0d\u5bf9\u5e94\uff01\uff01");
5
+ else { e = d.key; l = document.body || document.head; var k = document.createElement("script"); k.type = "text/javascript"; k.src ="https://map.zlink.org.cn/a/loca?v="+f+"&key="+e;k.onerror=function(a){g.Loca=b.failed;c("\u8bf7\u6c42 AMapUI \u5931\u8d25")};k.onload=function(){g.Loca=b.loaded;for(h();n.Loca.length;)n.Loca.splice(0,1)[0]()};l.appendChild(k)}}else g.Loca===b.loaded?a.version&&a.version!==d.Loca.version?c("\u4e0d\u5141\u8bb8\u591a\u4e2a\u7248\u672c Loca \u6df7\u7528"):h():a.version&&a.version!==d.Loca.version?
6
+ c("\u4e0d\u5141\u8bb8\u591a\u4e2a\u7248\u672c Loca \u6df7\u7528"):n.Loca.push(function(a){a?c(a):c()})})}if(!window)throw Error("AMap JSAPI can only be used in Browser.");var b;(function(a){a.notload="notload";a.loading="loading";a.loaded="loaded";a.failed="failed"})(b||(b={}));var d={key:"",AMap:{version:"1.4.15",plugins:[]},AMapUI:{version:"1.1",plugins:[]},Loca:{version:"1.3.2"}},g={AMap:b.notload,AMapUI:b.notload,Loca:b.notload},n={AMap:[],AMapUI:[],Loca:[]},q=[],t=function(a){"function"==typeof a&&
7
+ (g.AMap===b.loaded?a(window.AMap):q.push(a))};return{load:function(a){return new Promise(function(h,c){if(g.AMap==b.failed)c("");else if(g.AMap==b.notload){var f=a.key,e=a.version,l=a.plugins;f?(window.AMap&&"lbs.amap.com"!==location.host&&c("\u7981\u6b62\u591a\u79cdAPI\u52a0\u8f7d\u65b9\u5f0f\u6df7\u7528"),d.key=f,d.AMap.version=e||d.AMap.version,d.AMap.plugins=l||d.AMap.plugins,g.AMap=b.loading,e=document.body||document.head,window.___onAPILoaded=function(d){delete window.___onAPILoaded;if(d)g.AMap=
8
+ b.failed,c(d);else for(g.AMap=b.loaded,m(a).then(function(){h(window.AMap)})["catch"](c);q.length;)q.splice(0,1)[0]()},l=document.createElement("script"),l.type="text/javascript",l.src="https://map.zlink.org.cn/a/webapi/maps?callback=___onAPILoaded&v="+d.AMap.version+"&key="+f+"&plugin="+d.AMap.plugins.join(","),l.onerror=function(a){g.AMap=b.failed;c(a)},e.appendChild(l)):c("\u8bf7\u586b\u5199key")}else if(g.AMap==b.loaded)if(a.key&&a.key!==d.key)c("\u591a\u4e2a\u4e0d\u4e00\u81f4\u7684 key");else if(a.version&&
9
+ a.version!==d.AMap.version)c("\u4e0d\u5141\u8bb8\u591a\u4e2a\u7248\u672c JSAPI \u6df7\u7528");else{f=[];if(a.plugins)for(e=0;e<a.plugins.length;e+=1)-1==d.AMap.plugins.indexOf(a.plugins[e])&&f.push(a.plugins[e]);if(f.length)window.AMap.plugin(f,function(){m(a).then(function(){h(window.AMap)})["catch"](c)});else m(a).then(function(){h(window.AMap)})["catch"](c)}else if(a.key&&a.key!==d.key)c("\u591a\u4e2a\u4e0d\u4e00\u81f4\u7684 key");else if(a.version&&a.version!==d.AMap.version)c("\u4e0d\u5141\u8bb8\u591a\u4e2a\u7248\u672c JSAPI \u6df7\u7528");
10
+ else{var k=[];if(a.plugins)for(e=0;e<a.plugins.length;e+=1)-1==d.AMap.plugins.indexOf(a.plugins[e])&&k.push(a.plugins[e]);t(function(){if(k.length)window.AMap.plugin(k,function(){m(a).then(function(){h(window.AMap)})["catch"](c)});else m(a).then(function(){h(window.AMap)})["catch"](c)})}})},reset:function(){delete window.AMap;delete window.AMapUI;delete window.Loca;d={key:"",AMap:{version:"1.4.15",plugins:[]},AMapUI:{version:"1.1",plugins:[]},Loca:{version:"1.3.2"}};g={AMap:b.notload,AMapUI:b.notload,
11
+ Loca:b.notload};n={AMap:[],AMapUI:[],Loca:[]}}}})
package/index.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ declare var load: (options: {
2
+ key: string; // 申请好的Web端开发者Key,首次调用 load 时必填
3
+ version: string; // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
4
+ plugins?: string[]; //插件列表
5
+ // 是否加载 AMapUI,缺省不加载
6
+ AMapUI?: {
7
+ version?: string; // AMapUI 缺省 1.1
8
+ plugins?: string[]; // 需要加载的 AMapUI ui插件
9
+ };
10
+ // 是否加载 Loca, 缺省不加载
11
+ Loca?: {
12
+ version?: string; // Loca 版本,缺省 1.3.2
13
+ };
14
+ }) => Promise<any>;
15
+ export { load };
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "z-amap-jsapi-loader",
3
+ "version": "1.0.4",
4
+ "description": "amap jsapi Loader",
5
+ "main": "dist/index.js",
6
+ "types": "index.d.ts",
7
+ "author": "zlink.org.cn",
8
+ "license": "MIT",
9
+ "devDependencies": {
10
+ "@ampproject/rollup-plugin-closure-compiler": "^0.23.0",
11
+ "@babel/core": "^7.8.7",
12
+ "@babel/preset-env": "^7.8.7",
13
+ "rollup": "^1.32.0",
14
+ "rollup-plugin-babel": "^4.3.3",
15
+ "rollup-plugin-server": "^0.7.0",
16
+ "rollup-plugin-typescript2": "^0.27.1",
17
+ "typescript": "^3.9.7"
18
+ }
19
+ }
@@ -0,0 +1,25 @@
1
+ import server from "rollup-plugin-server";
2
+ import babel from "rollup-plugin-babel";
3
+ import compiler from "@ampproject/rollup-plugin-closure-compiler";
4
+ import rollupTypescript from 'rollup-plugin-typescript2';
5
+
6
+ export default {
7
+ input: "src/index.ts",
8
+ output: {
9
+ file: "dist/index.js",
10
+ format: "umd",
11
+ name: "AMapLoader",
12
+ },
13
+ plugins: [
14
+ rollupTypescript(),
15
+ babel({
16
+ presets: [["@babel/env", { targets: { ie: 9 } }]],
17
+ }),
18
+ compiler(),
19
+ server({
20
+ contentBase: "./",
21
+ host: "127.0.0.1",
22
+ port: 3601,
23
+ }),
24
+ ],
25
+ };
@@ -0,0 +1,5 @@
1
+ declare var AMap: any;
2
+ declare var AMapUI: any;
3
+ declare var Loca: any;
4
+
5
+ declare var ___onAPILoaded: any;
package/src/index.ts ADDED
@@ -0,0 +1,367 @@
1
+ if (!window) {
2
+ throw Error("AMap JSAPI can only be used in Browser.");
3
+ }
4
+ enum LoadStatus {
5
+ notload = "notload",
6
+ loading = "loading",
7
+ loaded = "loaded",
8
+ failed = "failed",
9
+ }
10
+ let config = {
11
+ key: "",
12
+ AMap: {
13
+ version: "1.4.15",
14
+ plugins: [],
15
+ },
16
+ AMapUI: {
17
+ version: "1.1",
18
+ plugins: [],
19
+ },
20
+ Loca: {
21
+ version: "1.3.2",
22
+ },
23
+ };
24
+
25
+ let Status = {
26
+ AMap: LoadStatus.notload,
27
+ AMapUI: LoadStatus.notload,
28
+ Loca: LoadStatus.notload,
29
+ };
30
+ let Callback = {
31
+ AMap: [],
32
+ AMapUI: [],
33
+ Loca: [],
34
+ };
35
+
36
+ let onloadCBKs = [];
37
+ const onload = function (callback) {
38
+ if (typeof callback == "function") {
39
+ if (Status.AMap === LoadStatus.loaded) {
40
+ callback(window.AMap);
41
+ return;
42
+ }
43
+ onloadCBKs.push(callback);
44
+ }
45
+ };
46
+
47
+ interface LoadOption {
48
+ key: string;
49
+ version?: string;
50
+ plugins?: string[];
51
+ AMapUI?: {
52
+ version?: string;
53
+ plugins?: string[];
54
+ };
55
+ Loca?: {
56
+ version?: string;
57
+ };
58
+ }
59
+ function appendOther(option: LoadOption): Promise<any> {
60
+ let pros: Promise<void>[] = [];
61
+ if (option.AMapUI) {
62
+ pros.push(loadAMapUI(option.AMapUI));
63
+ }
64
+ if (option.Loca) {
65
+ pros.push(loadLoca(option.Loca));
66
+ }
67
+ return Promise.all(pros);
68
+ }
69
+ function loadAMapUI(params: { version?: string; plugins?: string[] }): Promise<void> {
70
+ return new Promise((res, rej) => {
71
+ const newPlugins: string[] = [];
72
+ if (params.plugins) {
73
+ for (var i = 0; i < params.plugins.length; i += 1) {
74
+ if (config.AMapUI.plugins.indexOf(params.plugins[i]) == -1) {
75
+ newPlugins.push(params.plugins[i]);
76
+ }
77
+ }
78
+ }
79
+ if (Status.AMapUI === LoadStatus.failed) {
80
+ rej("前次请求 AMapUI 失败");
81
+ } else if (Status.AMapUI === LoadStatus.notload) {
82
+ Status.AMapUI = LoadStatus.loading;
83
+ config.AMapUI.version = params.version || config.AMapUI.version;
84
+ const version = config.AMapUI.version;
85
+ const parentNode = document.body || document.head;
86
+ const script = document.createElement("script");
87
+ script.type = "text/javascript";
88
+ script.src = `https://webapi.amap.com/ui/${version}/main.js`;
89
+
90
+ script.onerror = (e) => {
91
+ Status.AMapUI = LoadStatus.failed;
92
+ rej("请求 AMapUI 失败");
93
+ };
94
+ script.onload = () => {
95
+ Status.AMapUI = LoadStatus.loaded;
96
+ if (newPlugins.length) {
97
+ window.AMapUI.loadUI(newPlugins, function () {
98
+ for (let i = 0, len = newPlugins.length; i < len; i++) {
99
+ const path = newPlugins[i];
100
+ const name = path.split("/").slice(-1)[0];
101
+ window.AMapUI[name] = arguments[i];
102
+ }
103
+ res();
104
+ while (Callback.AMapUI.length) {
105
+ Callback.AMapUI.splice(0, 1)[0]();
106
+ }
107
+ });
108
+ } else {
109
+ res();
110
+ while (Callback.AMapUI.length) {
111
+ Callback.AMapUI.splice(0, 1)[0]();
112
+ }
113
+ }
114
+ };
115
+ parentNode.appendChild(script);
116
+ } else if (Status.AMapUI === LoadStatus.loaded) {
117
+ if (params.version && params.version !== config.AMapUI.version) {
118
+ rej("不允许多个版本 AMapUI 混用");
119
+ } else {
120
+ if (newPlugins.length) {
121
+ window.AMapUI.loadUI(newPlugins, function () {
122
+ for (let i = 0, len = newPlugins.length; i < len; i++) {
123
+ const path = newPlugins[i];
124
+ const name = path.split("/").slice(-1)[0];
125
+ window.AMapUI[name] = arguments[i];
126
+ }
127
+ res();
128
+ });
129
+ } else {
130
+ res();
131
+ }
132
+ }
133
+ } else {
134
+ if (params.version && params.version !== config.AMapUI.version) {
135
+ rej("不允许多个版本 AMapUI 混用");
136
+ } else {
137
+ Callback.AMapUI.push((err) => {
138
+ if (err) {
139
+ rej(err);
140
+ } else {
141
+ if (newPlugins.length) {
142
+ window.AMapUI.loadUI(newPlugins, function () {
143
+ for (let i = 0, len = newPlugins.length; i < len; i++) {
144
+ const path = newPlugins[i];
145
+ const name = path.split("/").slice(-1)[0];
146
+ window.AMapUI[name] = arguments[i];
147
+ }
148
+ res();
149
+ });
150
+ } else {
151
+ res();
152
+ }
153
+ }
154
+ });
155
+ }
156
+ }
157
+ });
158
+ }
159
+
160
+ function loadLoca(params: { version?: string }): Promise<void> {
161
+ return new Promise((res, rej) => {
162
+ if (Status.Loca === LoadStatus.failed) {
163
+ rej("前次请求 Loca 失败");
164
+ } else if (Status.Loca === LoadStatus.notload) {
165
+ Status.Loca = LoadStatus.loading;
166
+ config.Loca.version = params.version || config.Loca.version;
167
+ const version = config.Loca.version;
168
+ const isApiV2 = config.AMap.version.startsWith("2");
169
+ const isLocaV2 = version.startsWith("2");
170
+ if ((isApiV2 && !isLocaV2) || (!isApiV2 && isLocaV2)) {
171
+ rej("JSAPI 与 Loca 版本不对应!!");
172
+ return;
173
+ }
174
+ const key = config.key;
175
+ const parentNode = document.body || document.head;
176
+ const script = document.createElement("script");
177
+ script.type = "text/javascript";
178
+ script.src = `https://map.zlink.org.cn/a/loca?v=${version}&key=${key}`;
179
+
180
+ script.onerror = (e) => {
181
+ Status.Loca = LoadStatus.failed;
182
+ rej("请求 AMapUI 失败");
183
+ };
184
+ script.onload = () => {
185
+ Status.Loca = LoadStatus.loaded;
186
+ res();
187
+ while (Callback.Loca.length) {
188
+ Callback.Loca.splice(0, 1)[0]();
189
+ }
190
+ };
191
+ parentNode.appendChild(script);
192
+ } else if (Status.Loca === LoadStatus.loaded) {
193
+ if (params.version && params.version !== config.Loca.version) {
194
+ rej("不允许多个版本 Loca 混用");
195
+ } else {
196
+ res();
197
+ }
198
+ } else {
199
+ if (params.version && params.version !== config.Loca.version) {
200
+ rej("不允许多个版本 Loca 混用");
201
+ } else {
202
+ Callback.Loca.push((err) => {
203
+ if (err) {
204
+ rej(err);
205
+ } else {
206
+ rej();
207
+ }
208
+ });
209
+ }
210
+ }
211
+ });
212
+ }
213
+
214
+ const load = function (options: LoadOption) {
215
+ return new Promise((resolve, reject) => {
216
+ if (Status.AMap == LoadStatus.failed) {
217
+ reject("");
218
+ } else if (Status.AMap == LoadStatus.notload) {
219
+ //初次加载
220
+ let { key, version, plugins } = options;
221
+ if (!key) {
222
+ reject("请填写key");
223
+ return;
224
+ }
225
+ if (window.AMap && location.host !== "lbs.amap.com") {
226
+ reject("禁止多种API加载方式混用");
227
+ }
228
+ config.key = key;
229
+ config.AMap.version = version || config.AMap.version;
230
+ config.AMap.plugins = plugins || config.AMap.plugins;
231
+ Status.AMap = LoadStatus.loading;
232
+
233
+ const parentNode = document.body || document.head;
234
+
235
+ window.___onAPILoaded = function (err) {
236
+ delete window.___onAPILoaded;
237
+ if (err) {
238
+ Status.AMap = LoadStatus.failed;
239
+ reject(err);
240
+ } else {
241
+ Status.AMap = LoadStatus.loaded;
242
+ appendOther(options)
243
+ .then(() => {
244
+ resolve(window.AMap);
245
+ })
246
+ .catch(reject);
247
+ while (onloadCBKs.length) {
248
+ onloadCBKs.splice(0, 1)[0]();
249
+ }
250
+ }
251
+ };
252
+ const script = document.createElement("script");
253
+ script.type = "text/javascript";
254
+
255
+ script.src =
256
+ "https://map.zlink.org.cn/a/webapi/maps?callback=___onAPILoaded&v=" +
257
+ config.AMap.version +
258
+ "&key=" +
259
+ key +
260
+ "&plugin=" +
261
+ config.AMap.plugins.join(",");
262
+ script.onerror = (e) => {
263
+ Status.AMap = LoadStatus.failed;
264
+ reject(e);
265
+ };
266
+ parentNode.appendChild(script);
267
+ } else if (Status.AMap == LoadStatus.loaded) {
268
+ //deal multi load
269
+ if (options.key && options.key !== config.key) {
270
+ reject("多个不一致的 key");
271
+ return;
272
+ }
273
+ if (options.version && options.version !== config.AMap.version) {
274
+ reject("不允许多个版本 JSAPI 混用");
275
+ return;
276
+ }
277
+ const newPlugins = [];
278
+ if (options.plugins) {
279
+ for (var i = 0; i < options.plugins.length; i += 1) {
280
+ if (config.AMap.plugins.indexOf(options.plugins[i]) == -1) {
281
+ newPlugins.push(options.plugins[i]);
282
+ }
283
+ }
284
+ }
285
+ if (newPlugins.length) {
286
+ window.AMap.plugin(newPlugins, () => {
287
+ appendOther(options)
288
+ .then(() => {
289
+ resolve(window.AMap);
290
+ })
291
+ .catch(reject);
292
+ });
293
+ } else {
294
+ appendOther(options)
295
+ .then(() => {
296
+ resolve(window.AMap);
297
+ })
298
+ .catch(reject);
299
+ }
300
+ } else {
301
+ // loading
302
+ if (options.key && options.key !== config.key) {
303
+ reject("多个不一致的 key");
304
+ return;
305
+ }
306
+ if (options.version && options.version !== config.AMap.version) {
307
+ reject("不允许多个版本 JSAPI 混用");
308
+ return;
309
+ }
310
+ const newPlugins = [];
311
+ if (options.plugins) {
312
+ for (var i = 0; i < options.plugins.length; i += 1) {
313
+ if (config.AMap.plugins.indexOf(options.plugins[i]) == -1) {
314
+ newPlugins.push(options.plugins[i]);
315
+ }
316
+ }
317
+ }
318
+ onload(() => {
319
+ if (newPlugins.length) {
320
+ window.AMap.plugin(newPlugins, () => {
321
+ appendOther(options)
322
+ .then(() => {
323
+ resolve(window.AMap);
324
+ })
325
+ .catch(reject);
326
+ });
327
+ } else {
328
+ appendOther(options)
329
+ .then(() => {
330
+ resolve(window.AMap);
331
+ })
332
+ .catch(reject);
333
+ }
334
+ });
335
+ }
336
+ });
337
+ };
338
+ function reset() {
339
+ delete window.AMap;
340
+ delete window.AMapUI;
341
+ delete window.Loca;
342
+ config = {
343
+ key: "",
344
+ AMap: {
345
+ version: "1.4.15",
346
+ plugins: [],
347
+ },
348
+ AMapUI: {
349
+ version: "1.1",
350
+ plugins: [],
351
+ },
352
+ Loca: {
353
+ version: "1.3.2",
354
+ },
355
+ };
356
+ Status = {
357
+ AMap: LoadStatus.notload,
358
+ AMapUI: LoadStatus.notload,
359
+ Loca: LoadStatus.notload,
360
+ };
361
+ Callback = {
362
+ AMap: [],
363
+ AMapUI: [],
364
+ Loca: [],
365
+ };
366
+ }
367
+ export default { load, reset };