6 changed files with 986 additions and 715 deletions
File diff suppressed because it is too large
@ -0,0 +1,78 @@ |
|||||
|
let amapPromise = null; |
||||
|
|
||||
|
export function loadAMap(options = {}) { |
||||
|
const { |
||||
|
key = "", |
||||
|
securityJsCode = "", |
||||
|
version = "2.0", |
||||
|
plugins = [], |
||||
|
} = options; |
||||
|
|
||||
|
if (typeof window === "undefined") { |
||||
|
return Promise.reject(new Error("当前环境不支持加载高德地图")); |
||||
|
} |
||||
|
|
||||
|
if (window.AMap && typeof window.AMap.Map === "function") { |
||||
|
return Promise.resolve(window.AMap); |
||||
|
} |
||||
|
|
||||
|
if (!key) { |
||||
|
return Promise.reject(new Error("未配置高德地图 Web Key")); |
||||
|
} |
||||
|
|
||||
|
if (securityJsCode) { |
||||
|
window._AMapSecurityConfig = Object.assign({}, window._AMapSecurityConfig, { |
||||
|
securityJsCode, |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
if (amapPromise) { |
||||
|
return amapPromise; |
||||
|
} |
||||
|
|
||||
|
amapPromise = new Promise((resolve, reject) => { |
||||
|
const scriptId = "geotag-amap-script"; |
||||
|
const existingScript = document.getElementById(scriptId); |
||||
|
|
||||
|
const handleResolve = () => { |
||||
|
if (window.AMap && typeof window.AMap.Map === "function") { |
||||
|
resolve(window.AMap); |
||||
|
return; |
||||
|
} |
||||
|
amapPromise = null; |
||||
|
reject(new Error("高德地图脚本加载失败")); |
||||
|
}; |
||||
|
|
||||
|
const handleError = () => { |
||||
|
amapPromise = null; |
||||
|
reject(new Error("高德地图脚本加载失败")); |
||||
|
}; |
||||
|
|
||||
|
if (existingScript) { |
||||
|
existingScript.addEventListener("load", handleResolve, { once: true }); |
||||
|
existingScript.addEventListener("error", handleError, { once: true }); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
const script = document.createElement("script"); |
||||
|
script.id = scriptId; |
||||
|
script.async = true; |
||||
|
script.defer = true; |
||||
|
script.onload = handleResolve; |
||||
|
script.onerror = handleError; |
||||
|
|
||||
|
const query = [ |
||||
|
"v=" + encodeURIComponent(version), |
||||
|
"key=" + encodeURIComponent(key), |
||||
|
]; |
||||
|
|
||||
|
if (plugins.length) { |
||||
|
query.push("plugin=" + encodeURIComponent(plugins.join(","))); |
||||
|
} |
||||
|
|
||||
|
script.src = "https://webapi.amap.com/maps?" + query.join("&"); |
||||
|
document.head.appendChild(script); |
||||
|
}); |
||||
|
|
||||
|
return amapPromise; |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
let googleMapsPromise = null; |
||||
|
|
||||
|
export function loadGoogleMaps(apiKey) { |
||||
|
if (typeof window === "undefined") { |
||||
|
return Promise.reject(new Error("当前环境不支持加载谷歌地图")); |
||||
|
} |
||||
|
|
||||
|
if (window.google && window.google.maps) { |
||||
|
return Promise.resolve(window.google.maps); |
||||
|
} |
||||
|
|
||||
|
if (!apiKey) { |
||||
|
return Promise.reject(new Error("未配置谷歌地图 API Key")); |
||||
|
} |
||||
|
|
||||
|
if (googleMapsPromise) { |
||||
|
return googleMapsPromise; |
||||
|
} |
||||
|
|
||||
|
googleMapsPromise = new Promise((resolve, reject) => { |
||||
|
const callbackName = "__geotagGoogleMapsReady__"; |
||||
|
const scriptId = "geotag-google-maps-script"; |
||||
|
const existingScript = document.getElementById(scriptId); |
||||
|
|
||||
|
const cleanup = () => { |
||||
|
if (window[callbackName]) { |
||||
|
delete window[callbackName]; |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
window[callbackName] = () => { |
||||
|
cleanup(); |
||||
|
resolve(window.google.maps); |
||||
|
}; |
||||
|
|
||||
|
const handleError = () => { |
||||
|
cleanup(); |
||||
|
googleMapsPromise = null; |
||||
|
reject(new Error("谷歌地图脚本加载失败")); |
||||
|
}; |
||||
|
|
||||
|
if (existingScript) { |
||||
|
existingScript.addEventListener("error", handleError, { once: true }); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
const script = document.createElement("script"); |
||||
|
script.id = scriptId; |
||||
|
script.async = true; |
||||
|
script.defer = true; |
||||
|
script.src = |
||||
|
"https://maps.googleapis.com/maps/api/js?key=" + |
||||
|
encodeURIComponent(apiKey) + |
||||
|
"&callback=" + |
||||
|
callbackName; |
||||
|
script.onerror = handleError; |
||||
|
document.head.appendChild(script); |
||||
|
}); |
||||
|
|
||||
|
return googleMapsPromise; |
||||
|
} |
||||
Loading…
Reference in new issue