AmapBasemap.ts 1.81 KB
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();
  }
}