67 lines
2.2 KiB
JavaScript
67 lines
2.2 KiB
JavaScript
// 使用OpenStreetMap和Leaflet替代百度地图
|
|
|
|
export function loadMap() {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
// eslint-disable-next-line no-undef
|
|
if (typeof L !== 'undefined') {
|
|
// eslint-disable-next-line no-undef
|
|
resolve(L);
|
|
return true;
|
|
}
|
|
|
|
// 加载Leaflet CSS
|
|
if (!document.querySelector('link[href*="leaflet.css"]')) {
|
|
const link = document.createElement('link');
|
|
link.rel = 'stylesheet';
|
|
link.href = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css';
|
|
link.integrity = 'sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=';
|
|
link.crossOrigin = '';
|
|
document.head.appendChild(link);
|
|
}
|
|
|
|
// 加载Leaflet JS
|
|
const script = document.createElement('script');
|
|
script.type = 'text/javascript';
|
|
script.src = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js';
|
|
script.integrity = 'sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=';
|
|
script.crossOrigin = '';
|
|
script.onload = function() {
|
|
// eslint-disable-next-line no-undef
|
|
resolve(L);
|
|
};
|
|
script.onerror = reject;
|
|
document.head.appendChild(script);
|
|
});
|
|
|
|
}
|
|
|
|
// export function loadBMap() {
|
|
// return new Promise(function(resolve, reject) {
|
|
// if (typeof BMap !== 'undefined') {
|
|
// resolve(BMap)
|
|
// return true
|
|
// }
|
|
// window.onBMapCallback = function() {
|
|
// resolve(BMap)
|
|
// }
|
|
// // 使用https协议需要添加一下meta标签
|
|
// var protocolStr = document.location.protocol;
|
|
// if(protocolStr == "https:")
|
|
// {
|
|
// let meta = document.createElement('meta')
|
|
// meta.httpEquiv = 'Content-Security-Policy'
|
|
// meta.content ='upgrade-insecure-requests'
|
|
// meta.onerror = reject
|
|
// document.head.appendChild(meta)
|
|
// }
|
|
// // 引入百度地图
|
|
// let script = document.createElement('script')
|
|
// script.type = 'text/javascript'
|
|
// script.src =
|
|
// 'http://api.map.baidu.com/api?v=2.0&ak='+ process.env.VUE_APP_BAI_DU_AK +'&s=1&__ec_v__=20190126&callback=onBMapCallback'
|
|
// script.onerror = reject
|
|
// document.head.appendChild(script)
|
|
// })
|
|
// }
|