@xuehongbo/map-craft-js
Version:
MapCraftJS 是一个功能强大且灵活的开源 JavaScript 库,旨在简化互动地图的创建和操作。使用 MapCraftJS,开发者可以轻松地将动态地图功能集成到应用程序中,为用户提供根据自定义配置查看、注释和互动的地图体验。(开发中!!!)
64 lines (59 loc) • 1.9 kB
JavaScript
// 引入地图适配器和事件管理器
import BMapAdapter from '../adaptor/BMapAdaptor.js';
import EGisAdapter from '../adaptor/EGisAdaptor.js';
import EventEmitter from '../utils/EventEmitter.js';
import AMapAdapter from '../adaptor/AMapAdaptor.js';
class MapAdapter extends EventEmitter {
constructor(mapConfig, options) {
super(); // 调用父类构造函数初始化事件管理器
if (!mapConfig.security) {
throw new Error('Security configuration is required');
}
switch (mapConfig.platform) {
case 'bmap':
if (!mapConfig.security.BMap || !mapConfig.security.BMap.ak) {
throw new Error('BMap ak is required');
}
this.adapter = new BMapAdapter(mapConfig.security.BMap, options, () => {
this.emit('loaded');
});
break;
case 'amap':
if (!mapConfig.security.AMap || !mapConfig.security.AMap.key || !mapConfig.security.AMap.code) {
throw new Error('AMap key and code are required');
}
this.adapter = new AMapAdapter(mapConfig.security.AMap, options, () => {
this.emit('loaded');
});
break;
case 'egis':
if (!mapConfig.security.EGis || !mapConfig.security.EGis.key || !mapConfig.security.EGis.secret) {
throw new Error('EGis key and secret are required');
}
this.adapter = new EGisAdapter(mapConfig.security.EGis, options, () => {
this.emit('loaded');
});
break;
default:
throw new Error('Unsupported map platform');
}
}
/**
* 打点
* @param {*} options
* @returns
*/
addMarker(options) {
let marker = this.adapter.addMarker(options);
return marker;
}
/**
* 画线
* @param {*} points
* @returns
*/
drawPolyline(points) {
return this.adapter.drawPolyline(points);
}
}
export default MapAdapter;