/** * mars3d地图实体基类 * 提供mars3d地图要素的通用功能,如弹窗管理、事件处理、飞行定位等 */ export default class Mars3dEntity { /** 配置选项 */ protected option: any = null; /** 事件监听器集合 */ private event: Record = {}; /** 图形实例 */ protected graphic: any = null; /** * 构造函数 * @param hnMap 地图实例 */ constructor(hnMap: any) { this.event = {}; } /** * 添加属性弹窗 * 点击要素时显示包含要素属性信息的弹窗 */ addPopupByAttr(): void { this.graphic.bindPopup((event: any) => { const data = event.graphic.attr; return mars3d.Util.getTemplateHtml({ title: "详情", template: "all", attr: data, }); }); } /** * 添加自定义DOM弹窗 * 点击要素时显示自定义DOM结构的弹窗 * @param getCustomDom 自定义DOM生成函数,接收要素数据,返回DOM字符串 */ addCustomPopup(getCustomDom: (data: any) => Promise | string): void { this.graphic.bindPopup( async (event: any) => { if (event.graphic.attr) { const data = event.graphic.attr || {}; return await getCustomDom(data); } }, {offsetY: -20} ); } /** * 飞行定位到要素 * @param option 飞行定位选项 */ flyTo(option: any = {}): void { this.graphic.flyTo(option); } /** * 销毁要素 * 从地图中移除并销毁要素 */ destroy(): void { this.graphic.destroy(); } /** * 监听事件 * @param eventType 事件类型 * @param callback 事件回调函数 */ on(eventType: string, callback: (data: any) => void): void { this.off(eventType); switch (eventType) { case "click": this.event[eventType] = () => { callback(this.option.data); }; break; } this.graphic.on(eventType, this.event[eventType]); } /** * 移除事件监听 * @param eventType 事件类型 */ off(eventType: string): void { if (this.event[eventType]) { this.graphic.off(eventType, this.event[eventType]); delete this.event[eventType]; } } }