Commit 09a690bd authored by 郭铭瑶's avatar 郭铭瑶 🤘

地图的事件监听

parent 65b0026b
import MyMap from './my-map' import MyMap from './my-map'
import { CallBack, Listener, MapConfig } from './types' import { CallBack, MapConfig, Listeners } from './types'
declare const aimap: { declare const aimap: {
Map: any Map: any
...@@ -9,7 +9,7 @@ declare const aimap: { ...@@ -9,7 +9,7 @@ declare const aimap: {
export default class AI_Map extends MyMap { export default class AI_Map extends MyMap {
constructor(config: MapConfig) { constructor(config: MapConfig) {
aimap.accessToken = config.appKey aimap.accessToken = config.appKey
aimap.accessToken = config.appKey aimap.baseApiUrl = config.baseApiUrl
const instance = new aimap.Map({ const instance = new aimap.Map({
container: config.el, container: config.el,
...@@ -24,44 +24,36 @@ export default class AI_Map extends MyMap { ...@@ -24,44 +24,36 @@ export default class AI_Map extends MyMap {
style: config.style, style: config.style,
}) })
super(instance) super(instance)
this.setListeners()
} }
on<K extends keyof Listener>(name: K, cb: CallBack) { private setListeners() {
const eventName = `on${name.slice(0, 1).toUpperCase()}${name const listeners: Partial<Listeners> = {
.slice(1) load: (cb: CallBack) => {
.toLowerCase()}` as Listener[K]
;(this[eventName] as Function)(cb)
return this
}
private onClick(cb: CallBack) {
this.map.on('click', cb)
}
private onLoad(cb: CallBack) {
this.map.on('load', cb) this.map.on('load', cb)
} },
private onZoom(cb: CallBack) { zoom: (cb: CallBack) => {
this.map.on('zoom', cb) this.map.on('zoom', cb)
} },
private onMove(cb: CallBack) { move: (cb: CallBack) => {
this.map.on('move', cb) this.map.on('move', cb)
} },
private onBlur(cb: CallBack) { blur: (cb: CallBack) => {
this.map.on('blur', cb) this.map.on('blur', cb)
} },
private onFocus(cb: CallBack) { focus: (cb: CallBack) => {
this.map.on('focus', cb) this.map.on('focus', cb)
} },
private onDrag(cb: CallBack) { drag: (cb: CallBack) => {
this.map.on('drag', cb) this.map.on('drag', cb)
} },
private onResize(cb: CallBack) { resize: (cb: CallBack) => {
this.map.on('resize', cb) this.map.on('resize', cb)
},
click: (cb: CallBack) => {
this.map.on('click', cb)
},
} }
private onDblClick(cb: CallBack) { this.listeners = Object.assign(this.listeners, listeners)
this.map.on('dblclick', cb)
}
private onMouseWheel(cb: CallBack) {
this.map.on('mousewheel', cb)
} }
} }
import { CallBack, Listeners } from './types'
export default class MyMap { export default class MyMap {
protected map protected map
protected listeners: Listeners
constructor(instance: any) { constructor(instance: any) {
this.map = instance this.map = instance
this.listeners = {
load: (cb: CallBack) => {
console.error('on:此地图不存在 load 监听事件!')
},
zoom: (cb: CallBack) => {
console.error('on:此地图不存在 zoom 监听事件!')
},
move: (cb: CallBack) => {
console.error('on:此地图不存在 move 监听事件!')
},
blur: (cb: CallBack) => {
console.error('on:此地图不存在 blur 监听事件!')
},
focus: (cb: CallBack) => {
console.error('on:此地图不存在 focus 监听事件!')
},
drag: (cb: CallBack) => {
console.error('on:此地图不存在 drag 监听事件!')
},
resize: (cb: CallBack) => {
console.error('on:此地图不存在 resize 监听事件!')
},
click: (cb: CallBack) => {
console.error('on:此地图不存在 click 监听事件!')
},
dblclick: (cb: CallBack) => {
console.error('on:此地图不存在 dblclick 监听事件!')
},
mousewheel: (cb: CallBack) => {
console.error('on:此地图不存在 mousewheel 监听事件!')
},
}
} }
/** /**
...@@ -11,4 +46,14 @@ export default class MyMap { ...@@ -11,4 +46,14 @@ export default class MyMap {
get instance(): any { get instance(): any {
return this.map return this.map
} }
/**
* 监听事件
* @param name 事件名称
* @param cb 回调函数
*/
on<K extends keyof Listeners>(name: K, cb: CallBack) {
this.listeners[name](cb)
return this
}
} }
import MyMap from './my-map' import MyMap from './my-map'
import { CallBack, MapConfig, Listener, EventName } from './types' import { CallBack, MapConfig, Listeners } from './types'
declare const SMap: { declare const SMap: {
Map: any Map: any
...@@ -14,8 +14,6 @@ declare const Plugins: { ...@@ -14,8 +14,6 @@ declare const Plugins: {
MaskBoundary: any MaskBoundary: any
} }
type Fn = (cb: CallBack) => void
export default class S_Map extends MyMap { export default class S_Map extends MyMap {
constructor(config: MapConfig) { constructor(config: MapConfig) {
if (!config.netType) { if (!config.netType) {
...@@ -34,6 +32,7 @@ export default class S_Map extends MyMap { ...@@ -34,6 +32,7 @@ export default class S_Map extends MyMap {
rotateEnable: config.rotateEnable, rotateEnable: config.rotateEnable,
}) })
super(instance) super(instance)
this.setListeners()
this.on('load', this.clearFooter) this.on('load', this.clearFooter)
} }
...@@ -43,35 +42,40 @@ export default class S_Map extends MyMap { ...@@ -43,35 +42,40 @@ export default class S_Map extends MyMap {
) )
footer && ((footer as HTMLElement).style.display = 'none') footer && ((footer as HTMLElement).style.display = 'none')
} }
private setListeners() {
on<K extends keyof Listener>(name: K, cb: CallBack) { const listeners: Partial<Listeners> = {
const eventName = `on${name.slice(0, 1).toUpperCase()}${name load: (cb: CallBack) => {
.slice(1)
.toLowerCase()}` as Listener[K]
this[eventName](cb)
return this
}
private onLoad(cb: CallBack) {
this.map.on(SMap.MapEvent.maploaded, cb) this.map.on(SMap.MapEvent.maploaded, cb)
} },
private onZoom(cb: CallBack) { zoom: (cb: CallBack) => {
this.map.on(SMap.MapEvent.zoomchanged, cb) this.map.on(SMap.MapEvent.zoomchanged, cb)
} },
private onMove(cb: CallBack) { move: (cb: CallBack) => {
this.map.on(SMap.MapEvent.centerchanged, cb) this.map.on(SMap.MapEvent.centerchanged, cb)
} },
private onBlur(cb: CallBack) { blur: (cb: CallBack) => {
this.map.on(SMap.MapEvent.blur, cb) this.map.on(SMap.MapEvent.blur, cb)
} },
private onFocus(cb: CallBack) { focus: (cb: CallBack) => {
this.map.on(SMap.MapEvent.focus, cb) this.map.on(SMap.MapEvent.focus, cb)
} },
private onDrag(cb: CallBack) { drag: (cb: CallBack) => {
this.map.on(SMap.MapEvent.drag, cb) this.map.on(SMap.MapEvent.drag, cb)
} },
private onResize(cb: CallBack) { resize: (cb: CallBack) => {
this.map.on(SMap.MapEvent.resize, cb) this.map.on(SMap.MapEvent.resize, cb)
},
click: (cb: CallBack) => {
this.event(SMap.MapEvent.click, cb)
},
dblclick: (cb: CallBack) => {
this.event(SMap.MapEvent.doubleclick, cb)
},
mousewheel: (cb: CallBack) => {
this.event(SMap.MapEvent.mousewheel, cb)
},
}
this.listeners = Object.assign(this.listeners, listeners)
} }
private event(event: unknown, cb: CallBack) { private event(event: unknown, cb: CallBack) {
...@@ -81,14 +85,4 @@ export default class S_Map extends MyMap { ...@@ -81,14 +85,4 @@ export default class S_Map extends MyMap {
}) })
}) })
} }
private onClick(cb: CallBack): void {
this.event(SMap.MapEvent.click, cb)
}
private onDblClick(cb: CallBack): void {
this.event(SMap.MapEvent.doubleclick, cb)
}
private onMouseWheel(cb: CallBack): void {
this.event(SMap.MapEvent.mousewheel, cb)
}
} }
export type MapConfig = Partial<MapOptions> /**
* 地图配置
export interface MapOptions { */
export type MapConfig = Partial<{
/** 地图容器(需要有宽高) */
el: string el: string
/** /**
* SMap 地图专用 * SMap 地图专用
...@@ -11,13 +13,21 @@ export interface MapOptions { ...@@ -11,13 +13,21 @@ export interface MapOptions {
* @param njdl 表示南京东路政务网3D * @param njdl 表示南京东路政务网3D
*/ */
netType: 'internet' | 'affairs' | 'local3D' | 'affairs3D' | 'njdl' netType: 'internet' | 'affairs' | 'local3D' | 'affairs3D' | 'njdl'
/** 地图模式 */
mode: '2D' | '3D' mode: '2D' | '3D'
/** 地图初始中心点位 */
center: [number, number] center: [number, number]
/** 地图初始zoom等级 */
zoom: number zoom: number
/** 地图zoom允许范围 */
zooms: [number, number] zooms: [number, number]
/** 地图初始旋转角度 */
bearing: number bearing: number
/** 地图初始仰角(3D用) */
pitch: number pitch: number
/** 地图仰角允许范围 */
pitchs: [number, number] pitchs: [number, number]
/** 地图样式 */
style: string style: string
/** /**
* 在SMap中作为appKey使用 * 在SMap中作为appKey使用
...@@ -27,19 +37,35 @@ export interface MapOptions { ...@@ -27,19 +37,35 @@ export interface MapOptions {
showBuildingBlock: boolean showBuildingBlock: boolean
rotateEnable: boolean rotateEnable: boolean
baseApiUrl: string baseApiUrl: string
} }>
export type CallBack = (arg: unknown, oth?: unknown) => void /**
* 监听回调函数
*/
export type CallBack = (arg: any, oth?: any) => void
export type Listener = { /**
load: 'onLoad' * 监听事件
zoom: 'onZoom' */
move: 'onMove' export interface Listeners {
blur: 'onBlur' /** 加载完成触发 */
focus: 'onFocus' load: (cb: CallBack) => unknown
drag: 'onDrag' /** zoom变化触发 */
resize: 'onResize' zoom: (cb: CallBack) => unknown
click: 'onClick' /** 地图移动 */
dblclick: 'onDblClick' move: (cb: CallBack) => unknown
mousewheel: 'onMouseWheel' /** 失焦触发 */
blur: (cb: CallBack) => unknown
/** 聚焦触发 */
focus: (cb: CallBack) => unknown
/** 拖动触发 */
drag: (cb: CallBack) => unknown
/** 视图大小变化触发 */
resize: (cb: CallBack) => unknown
/** 点击触发 */
click: (cb: CallBack) => unknown
/** 双击触发 */
dblclick: (cb: CallBack) => unknown
/** 滚轮触发 */
mousewheel: (cb: CallBack) => unknown
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment