UNPKG

@orca-fe/x-map

Version:
204 lines (203 loc) 5.4 kB
import { EventEmitter } from 'events'; import ResizeObserver from 'resize-observer-polyfill'; /* eslint-disable no-console,no-unused-vars */ const notImplementError = (method = '') => { console.warn(`MapInstance Error: You should implement method "${method}"`); }; class BaseInstance extends EventEmitter { constructor() { super(...arguments); this.currentViewport = { lat: 0, lng: 0, pitch: 0, rotate: 0, zoom: 0, }; } /** * 初始化地图 * @param id 初始化容器ID */ init(id, options) { notImplementError('init(id, options)'); } /** * 销毁地图 */ destroy() { this.removeAllListeners(); this.unObsDom(); } /** * 经纬度转像素 * @param point 坐标点 */ lnglatToPixel(point) { notImplementError('lnglatToPixel(lng, lat)'); return [0, 0]; } /** * 像素转经纬度 * @param pixel 像素点 */ pixelToLnglat(pixel) { notImplementError('pixelToLnglat(x, y)'); return [0, 0]; } /** * 地图自动调整容器大小 */ resize() { } /** * 查询当前缩放级别 */ getZoom() { notImplementError('getZoom()'); return 0; } /** * 设置地图缩放级别 * @param zoom */ setZoom(zoom) { notImplementError('setZoom(...)'); } /** * 设置地图缩放级别以及中心位置 * @param zoom * @param center */ setZoomAndCenter(zoom, center) { this.setCenter(center); this.setZoom(zoom); } /** * 放大地图 */ zoomIn() { notImplementError('zoomIn()'); } /** * 缩小地图 */ zoomOut() { notImplementError('zoomOut()'); } /** * 将地图中心平移到指定点 * @param point 坐标点 */ panTo(point) { notImplementError('panTo(lng, lat)'); } /** * 获取地图中心坐标 */ getCenter() { notImplementError('getCenter()'); return [0, 0]; } /** * 设置地图中心点(无动画) */ setCenter(point) { } /** * 获取俯仰角度 */ getPitch() { notImplementError('getPitch()'); return 0; } /** * 获取俯仰角度 */ setPitch(pitch) { notImplementError('setPitch()'); } /** * 设置旋转角度 */ setRotate(deg) { notImplementError('setRotate(deg)'); } /** * 获取旋转角度 */ getRotate() { notImplementError('getRotate()'); return 0; } getMatrix() { return undefined; } obsDom() { if (this.dom) { if (this.observer) return; this.observer = new ResizeObserver((e) => { var _a, _b; this.domWidth = (_a = this.dom) === null || _a === void 0 ? void 0 : _a.clientWidth; this.domHeight = (_b = this.dom) === null || _b === void 0 ? void 0 : _b.clientHeight; }); this.observer.observe(this.dom); } } unObsDom() { if (this.observer) { this.observer.disconnect(); this.observer = undefined; } } getViewport() { var _a, _b; let width = (_a = this.domWidth) !== null && _a !== void 0 ? _a : 0; let height = (_b = this.domHeight) !== null && _b !== void 0 ? _b : 0; if (this.dom && (!width || !height)) { if (!this.observer) this.obsDom(); const { dom } = this; if (dom) { width = dom.clientWidth; height = dom.clientHeight; } } const [lngO, latO] = this.getCenter(); const lng = Number(lngO.toFixed(6)); const lat = Number(latO.toFixed(6)); const zoom = this.getZoom(); const pitch = this.getPitch(); const rotate = this.getRotate(); const viewport = { lng, lat, zoom, pitch, rotate, width, height }; return viewport; } setViewport(viewport) { if (!this.map) { console.error('Map not init or destroyed'); return; } const { currentViewport: cv } = this; const { lng, lat, zoom, pitch, rotate } = viewport; this.currentViewport = viewport; const centerChanged = lng != null && lat != null && (lng !== cv.lng || lat !== cv.lat); const zoomChanged = zoom != null && zoom !== cv.zoom; if (centerChanged && zoomChanged) { this.setZoomAndCenter(zoom, [lng, lat]); } else { if (centerChanged) { this.setCenter([lng, lat]); } if (zoomChanged) { this.setZoom(zoom); } } if (pitch != null && pitch !== cv.pitch) { this.setPitch(pitch); } if (rotate != null && rotate !== cv.rotate) { this.setRotate(rotate); } } } export default BaseInstance;