Commit 57e6f3fa authored by 郭铭瑶's avatar 郭铭瑶 🤘

点击事件回调参数统一化

parent 38273348
...@@ -18,27 +18,27 @@ let map: MyMap ...@@ -18,27 +18,27 @@ let map: MyMap
onMounted(async () => { onMounted(async () => {
await nextTick() await nextTick()
// map = useMap('SMap').with({ map = useMap('SMap').with({
// el: 'container',
// center: [0, 0],
// zoom: 5,
// style: 'smap://styles/dark',
// appKey: 'ACF69EDK17LON63GHPF081',
// netType: 'internet',
// })
map = useMap('AIMap').with({
el: 'container', el: 'container',
center: [121.612846, 31.205494], center: [0, 0],
zoom: 13, zoom: 5,
style: 'aimap://styles/aimap/darkblue-v4', style: 'smap://styles/dark',
appKey: 'gt8XidVe5upHf7cirkJwwXTCWj20zfu3', appKey: 'ACF69EDK17LON63GHPF081',
baseApiUrl: 'https://location-dev.newayz.com', netType: 'internet',
}) })
// map = useMap('AIMap').with({
// el: 'container',
// center: [121.612846, 31.205494],
// zoom: 13,
// style: 'aimap://styles/aimap/darkblue-v4',
// appKey: 'gt8XidVe5upHf7cirkJwwXTCWj20zfu3',
// baseApiUrl: 'https://location-dev.newayz.com',
// })
map map
.on('load', addControls) .on('load', addControls)
.on('click', (a, b) => console.log('click', a, b, map)) .on('click', (a, b) => console.log('click', a, b))
.on('zoom', (e) => console.log('zoom: ', e)) .on('zoom', (e) => console.log('zoom: ', e))
.on('move', (e) => console.log('move: ', e)) .on('move', (e) => console.log('move: ', e))
.on('blur', (e) => console.log('blur: ', e)) .on('blur', (e) => console.log('blur: ', e))
......
...@@ -7,6 +7,7 @@ import { ...@@ -7,6 +7,7 @@ import {
ZoomOptions, ZoomOptions,
FocusOptions, FocusOptions,
Location, Location,
ClickCallBack,
} from './types' } from './types'
declare const aimap: { declare const aimap: {
...@@ -41,14 +42,13 @@ export default class AI_Map extends MyMap { ...@@ -41,14 +42,13 @@ export default class AI_Map extends MyMap {
bearing: config.bearing || 0, bearing: config.bearing || 0,
style: config.style, style: config.style,
localIdeographFontFamily: config.family, localIdeographFontFamily: config.family,
spatialReference: 'cgcs2000',
}) })
super(instance) super(instance)
this.setListeners() this._setListeners()
this.setControls() this._setControls()
} }
private setListeners() { private _setListeners() {
const _listeners: Partial<Listeners> = { const _listeners: Partial<Listeners> = {
load: (cb: CallBack) => { load: (cb: CallBack) => {
this.map.on('load', cb) this.map.on('load', cb)
...@@ -71,14 +71,24 @@ export default class AI_Map extends MyMap { ...@@ -71,14 +71,24 @@ export default class AI_Map extends MyMap {
resize: (cb: CallBack) => { resize: (cb: CallBack) => {
this.map.on('resize', cb) this.map.on('resize', cb)
}, },
click: (cb: CallBack) => { click: (cb: ClickCallBack) => {
this.map.on('click', cb) this.map.on('click', (e: any) => {
const arg = {
type: e.type,
center: [e.lngLat.lng, e.lngLat.lat],
event: e.originalEvent,
clientX: e.point.x,
clientY: e.point.y,
originData: e,
}
cb(arg)
})
}, },
} }
this._listeners = Object.assign(this._listeners, _listeners) this._listeners = Object.assign(this._listeners, _listeners)
} }
private setControls() { private _setControls() {
const _controls = { const _controls = {
compass: (options: ControlOptions) => { compass: (options: ControlOptions) => {
this.map.addControl(new aimap.CompassControl(), options.position) this.map.addControl(new aimap.CompassControl(), options.position)
......
...@@ -51,19 +51,19 @@ export default class S_Map extends MyMap { ...@@ -51,19 +51,19 @@ export default class S_Map extends MyMap {
rotateEnable: config.rotateEnable, rotateEnable: config.rotateEnable,
}) })
super(instance) super(instance)
this.setListeners() this._setListeners()
this.setControls() this._setControls()
this.on('load', this.clearFooter) this.on('load', this._clearFooter)
} }
private clearFooter() { private _clearFooter() {
const footer = document.querySelector( const footer = document.querySelector(
'.esri-ui-manual-container>.esri-component', '.esri-ui-manual-container>.esri-component',
) )
footer && ((footer as HTMLElement).style.display = 'none') footer && ((footer as HTMLElement).style.display = 'none')
} }
private setListeners() { private _setListeners() {
const _listeners: Partial<Listeners> = { const _listeners: Partial<Listeners> = {
load: (cb: CallBack) => { load: (cb: CallBack) => {
this.map.on(SMap.MapEvent.maploaded, cb) this.map.on(SMap.MapEvent.maploaded, cb)
...@@ -81,33 +81,41 @@ export default class S_Map extends MyMap { ...@@ -81,33 +81,41 @@ export default class S_Map extends MyMap {
this.map.on(SMap.MapEvent.focus, cb) this.map.on(SMap.MapEvent.focus, cb)
}, },
drag: (cb: CallBack) => { drag: (cb: CallBack) => {
this.map.on(SMap.MapEvent.drag, cb) this._addEvent(SMap.MapEvent.drag, cb)
}, },
resize: (cb: CallBack) => { resize: (cb: CallBack) => {
this.map.on(SMap.MapEvent.resize, cb) this.map.on(SMap.MapEvent.resize, cb)
}, },
click: (cb: CallBack) => { click: (cb: CallBack) => {
this.event(SMap.MapEvent.click, cb) this._addEvent(SMap.MapEvent.click, cb)
}, },
dblclick: (cb: CallBack) => { dblclick: (cb: CallBack) => {
this.event(SMap.MapEvent.doubleclick, cb) this._addEvent(SMap.MapEvent.doubleclick, cb)
}, },
mousewheel: (cb: CallBack) => { mousewheel: (cb: CallBack) => {
this.event(SMap.MapEvent.mousewheel, cb) this._addEvent(SMap.MapEvent.mousewheel, cb)
}, },
} }
this._listeners = Object.assign(this._listeners, _listeners) this._listeners = Object.assign(this._listeners, _listeners)
} }
private event(event: unknown, cb: CallBack) { private _addEvent(event: unknown, cb: CallBack) {
this.map.on(event, (view: any, eventParamter: any) => { this.map.on(event, (view: any, e: any) => {
view.hitTest(eventParamter).then((res: any) => { const arg = {
cb(res.results, eventParamter.mapPoint) type: e.type,
center: [e.mapPoint?.longitude, e.mapPoint?.latitude],
event: e.native,
clientX: e.x,
clientY: e.y,
originData: e,
}
view.hitTest(e).then((res: any) => {
cb(arg, res.results)
}) })
}) })
} }
private setControls() { private _setControls() {
const _controls = { const _controls = {
home: (options: ControlOptions) => { home: (options: ControlOptions) => {
this.map.addControl( this.map.addControl(
......
...@@ -47,6 +47,25 @@ export type Location = [number, number] | [number, number, number] ...@@ -47,6 +47,25 @@ export type Location = [number, number] | [number, number, number]
*/ */
export type CallBack = (arg: any, oth?: any) => unknown export type CallBack = (arg: any, oth?: any) => unknown
/** 经过封装的返回统一参数的点击回调 */
export type ClickCallBack = (
arg: {
/** 事件类型 */
type: string
/** 地图初始中心点位 */
center: number[]
/** 鼠标事件 */
event: PointerEvent
/** 触发时鼠标x位置 */
clientX: number
/** 触发时鼠标y位置 */
clientY: number
/** 地图触发事件的原始数据(即未经封装前的数据) */
originData: any
},
oth?: any,
) => unknown
/** /**
* 监听事件 * 监听事件
*/ */
...@@ -66,7 +85,7 @@ export interface Listeners { ...@@ -66,7 +85,7 @@ export interface Listeners {
/** 视图大小变化触发 */ /** 视图大小变化触发 */
resize: (cb: CallBack) => unknown resize: (cb: CallBack) => unknown
/** 点击触发 */ /** 点击触发 */
click: (cb: CallBack) => unknown click: (cb: ClickCallBack) => unknown
/** 双击触发 */ /** 双击触发 */
dblclick: (cb: CallBack) => unknown dblclick: (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