AmapBasemap.ts
1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { OverlayRuntime } from '@hm/map-overlay';
import type { Basemap, BasemapBounds, LngLat } from '../types';
/**
* 高德地图门面。与百度的 BaiduBasemap 对称,业务层只见 Basemap 接口。
*/
export class AmapBasemap implements Basemap {
readonly raw: AMap.Map;
readonly overlayRuntime: OverlayRuntime;
constructor(
amap: typeof AMap,
map: AMap.Map,
) {
this.raw = map;
this.overlayRuntime = {
CustomLayer: amap.CustomLayer as unknown as OverlayRuntime['CustomLayer'],
};
}
getSize() {
return this.raw.getSize();
}
getCenter() {
const c = this.raw.getCenter();
return { getLng: () => c.getLng(), getLat: () => c.getLat() };
}
getZoom() {
return this.raw.getZoom();
}
lngLatToContainer(lnglat: LngLat) {
const p = this.raw.lngLatToContainer(lnglat);
return { x: p.x, y: p.y };
}
containerToLngLat(pixel: [number, number]) {
const ll = this.raw.containerToLngLat(pixel);
return { getLng: () => ll.getLng(), getLat: () => ll.getLat() };
}
getBounds(): BasemapBounds {
const b = this.raw.getBounds();
const sw = b.getSouthWest();
const ne = b.getNorthEast();
return {
getSouthWest: () => ({ getLng: () => sw.getLng(), getLat: () => sw.getLat() }),
getNorthEast: () => ({ getLng: () => ne.getLng(), getLat: () => ne.getLat() }),
};
}
setZoomAndCenter(
zoom: number,
center: LngLat,
immediately?: boolean,
duration?: number,
) {
this.raw.setZoomAndCenter(zoom, center, immediately, duration);
}
setMapStyle(style: string) {
this.raw.setMapStyle(style);
}
on(event: string, handler: (e: any) => void) {
this.raw.on(event, handler);
}
off(event: string, handler: (e: any) => void) {
this.raw.off(event, handler);
}
destroy() {
this.raw.destroy();
}
}