window._AMapSecurityConfig = {
|
securityJsCode: '', // 替换为高德平台生成的安全密钥
|
};
|
|
export const loadAMap = () => {
|
return new Promise((resolve, reject) => {
|
if (window.AMap) {
|
resolve(window.AMap);
|
return;
|
}
|
|
const script = document.createElement('script');
|
script.src = `https://webapi.amap.com/maps?v=1.4.11&key=7873666321486813e3eedad751bb9608&plugin=AMap.PlaceSearch,AMap.Geocoder`; // 替换为你的 API Key
|
script.onerror = () => reject(new Error('Failed to load AMap script'));
|
script.onload = () => {
|
if (window.AMap) {
|
resolve(window.AMap);
|
} else {
|
reject(new Error('AMap failed to initialize'));
|
}
|
};
|
|
document.body.appendChild(script);
|
});
|
};
|
|
export const loadAMapUI = () => {
|
return new Promise((resolve, reject) => {
|
if (window.AMapUI) {
|
resolve(window.AMapUI);
|
return;
|
}
|
|
const script = document.createElement('script');
|
script.src = 'https://webapi.amap.com/ui/1.0/main.js?v=1.0.11';
|
script.onerror = () => reject(new Error('Failed to load AMap UI script'));
|
script.onload = () => {
|
if (window.AMapUI) {
|
resolve(window.AMapUI);
|
} else {
|
reject(new Error('AMap UI failed to initialize'));
|
}
|
};
|
|
document.body.appendChild(script);
|
});
|
};
|
|
export const initPoiPicker = () => {
|
return new Promise((resolve, reject) => {
|
loadAMap()
|
.then(() => loadAMapUI())
|
.then((AMapUI) => {
|
AMapUI.loadUI(['misc/PoiPicker'], (PoiPicker) => {
|
if (!PoiPicker) {
|
reject(new Error('PoiPicker failed to load'));
|
return;
|
}
|
const poiPicker = new PoiPicker();
|
resolve(poiPicker);
|
});
|
})
|
.catch((error) => {
|
console.error('Error initializing PoiPicker:', error);
|
reject(error);
|
});
|
});
|
};
|
|
export const initGeocoder = () => {
|
return new Promise((resolve, reject) => {
|
loadAMap()
|
.then((AMap) => {
|
try {
|
const geocoder = new AMap.Geocoder();
|
resolve(geocoder);
|
} catch (error) {
|
reject(new Error('Failed to initialize Geocoder'));
|
}
|
})
|
.catch((error) => {
|
console.error('Error initializing Geocoder:', error);
|
reject(error);
|
});
|
});
|
};
|