glsk-mapapi-sdk.js
5.37 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* 地图 API 封装:从配置中心拉取脚本地址并加载,与历史 UMD 导出保持一致。
* - 修复:head 中已存在地图 script 时也必须实例化 GLMap(不再依赖构造函数是否传入回调)。
* - 新增:ready() 返回 Promise,在底层 Map 可用时 resolve(失败则 reject)。
*/
;(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.GlMap = factory()))
})(this, function () {
'use strict'
var envNameByCode = {
1: 'develop',
2: 'test',
3: 'production',
4: 'beta'
}
function typeOf(v) {
return typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol'
? function (t) {
return typeof t
}
: function (t) {
return t &&
typeof Symbol === 'function' &&
t.constructor === Symbol &&
t !== Symbol.prototype
? 'symbol'
: typeof t
}
}
function GLMapVendorSdk() {
var self = this
this.GLMap = undefined
var _resolveReady
var _rejectReady
var settled = false
function settleResolve() {
if (settled) return
settled = true
_resolveReady(self)
}
function settleReject(err) {
if (settled) return
settled = true
_rejectReady(err)
}
this._readyPromise = new Promise(function (resolve, reject) {
_resolveReady = resolve
_rejectReady = reject
})
var o = { env: 3, version: 3 }
var i
if (arguments.length === 0) {
i = undefined
} else if (arguments.length === 1) {
var a0 = arguments[0]
if (typeOf(a0) === 'object' && a0 !== null) {
o = a0
if (!o.env) o.env = 3
if (!o.version) o.version = 3
i = undefined
} else {
i = a0
}
} else {
o = typeOf(arguments[0]) === 'object' && arguments[0] !== null ? arguments[0] : o
i = arguments[1]
if (!o.env) o.env = 3
if (!o.version) o.version = 3
}
if (typeof window === 'undefined') {
settleReject(new Error('GLMapSdk requires window'))
return
}
function attachGlMapInstance() {
if (typeof window.$GlMapApi !== 'function') {
var err = new Error('$GlMapApi is not available after script load')
console.error(err)
settleReject(err)
return
}
try {
self.GLMap = new window.$GlMapApi(o, function () {
if (i && typeof i === 'function') i(self)
settleResolve()
})
} catch (e) {
settleReject(e)
}
}
;(async function () {
try {
// map-api-js:不配 env 时接口返回 code 与非对象 data,无 apiUrl(见配置中心「不存在该环境配置」)
var envCode = o.env != null ? o.env : 3
var envName = envNameByCode[envCode] || 'production'
var url =
'https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=map-api-js&env=' +
envName
var resp = await fetch(url)
var raw = await resp.json()
var payload = raw && typeof raw === 'object' ? raw : {}
var cfg = payload
if (
payload.data != null &&
typeof payload.data === 'object' &&
!Array.isArray(payload.data)
) {
cfg = payload.data
} else if (typeof payload.data === 'string' && payload.data.trim()) {
try {
var parsed = JSON.parse(payload.data)
if (parsed && typeof parsed === 'object') cfg = parsed
} catch (err) {
/* ignore */
}
}
var apiUrl = cfg.apiUrl || cfg.apiURL || cfg.scriptUrl
if (!apiUrl) {
var hint =
(payload && payload.message) ||
(cfg && cfg.message) ||
'map config missing apiUrl'
console.error('Map config response (no apiUrl):', raw)
throw new Error(hint)
}
var scriptAlreadyLoaded = Array.from(
document.head.getElementsByTagName('script')
).some(function (s) {
return s.src === apiUrl
})
if (scriptAlreadyLoaded) {
attachGlMapInstance()
} else {
var sc = document.createElement('script')
sc.setAttribute('ignore', '')
sc.src = apiUrl
sc.onload = function () {
attachGlMapInstance()
}
sc.onerror = function () {
settleReject(new Error('Failed to load map script: ' + apiUrl))
}
document.head.appendChild(sc)
}
} catch (e) {
console.error('Failed to fetch map configuration:', e)
settleReject(e)
}
})()
}
GLMapVendorSdk.prototype.ready = function () {
return this._readyPromise
}
GLMapVendorSdk.prototype.invoke = function (method) {
var args = Array.prototype.slice.call(arguments, 1)
var g = this.GLMap
return g[method].apply(g, args)
}
Object.defineProperty(GLMapVendorSdk.prototype, 'map', {
get: function () {
return this.GLMap._BMAP
}
})
Object.defineProperty(GLMapVendorSdk.prototype, 'mapGL', {
get: function () {
return this.GLMap._BMapGL
}
})
return GLMapVendorSdk
})