@tuoyuan/map-adapter-amap
Version:
高德地图适配器
271 lines (270 loc) • 8.77 kB
JavaScript
var T = Object.defineProperty;
var B = (m, i, t) => i in m ? T(m, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : m[i] = t;
var P = (m, i, t) => B(m, typeof i != "symbol" ? i + "" : i, t);
import { BaseMap as L, loadScript as x, deepAssign as N, Position as a, Pixel as h, ERouteType as R, ERoutePolicy as S } from "@tuoyuan/map-adapter-lib";
import { AMapMarker as k } from "./Marker.js";
import { AMapPolyline as b } from "./Polyline.js";
import { AMapPolygon as W } from "./Polygon.js";
class f extends L {
constructor() {
super();
P(this, "AMap");
P(this, "mapInstance");
P(this, "coordinateSystem", "GCJ02");
}
/** 加载依赖 */
load(t, e) {
return new Promise(async (o, l) => {
try {
if (typeof t.secret == "string" && t.secret !== "") {
const c = `
window._AMapSecurityConfig = {
securityJsCode: '${t.secret}'
}
`;
await x({ content: c });
}
const n = `https://webapi.amap.com/maps?v=2.0&key=${t.key}&plugin=AMap.Driving`;
if (await x({ url: n }), e != null && e.loca) {
const c = `https://webapi.amap.com/loca?v=2.0.0&key=${t.key}`;
await x({ url: c });
}
this.AMap = window.AMap;
} catch (n) {
l(n);
}
o();
});
}
/** 初始化地图 */
init(t, e) {
var l, n, c, y, w, I, v, p, A, _, g, u, E;
this._dom = t.dom, t.zoom && (this._zoom = t.zoom);
const o = new this.AMap.Map(t.dom, {
zoom: t.zoom,
center: t.position
});
if (e != null && e.isRoadNet) {
const s = new this.AMap.TileLayer.RoadNet();
o.addLayer(s);
}
if ((e == null ? void 0 : e.layer) === "satellite") {
const s = new this.AMap.TileLayer.Satellite();
o.addLayer(s);
}
if ((n = (l = t.boundary) == null ? void 0 : l.path) != null && n.length) {
const s = t.boundary, r = N({}, (w = (y = (c = this._ctx) == null ? void 0 : c.options) == null ? void 0 : y.style) == null ? void 0 : w.polygon, s.style), M = new this.AMap.Polygon({
path: s.path,
strokeColor: (I = r.line) == null ? void 0 : I.color,
strokeWeight: (v = r.line) == null ? void 0 : v.width,
fillColor: (p = r.fill) == null ? void 0 : p.color,
fillOpacity: (A = r.fill) == null ? void 0 : A.opacity
});
if ((_ = e == null ? void 0 : e.beyondTheBoundary) != null && _.hide) {
const d = new this.AMap.Polygon({
path: [
[
[-360, 90],
[-360, -90],
[360, -90],
[360, 90]
],
s.path
],
strokeWeight: 0,
fillColor: ((g = e.beyondTheBoundary) == null ? void 0 : g.color) ?? "#000",
fillOpacity: ((u = e.beyondTheBoundary) == null ? void 0 : u.opacity) ?? 1
});
o.add(d);
}
(E = e == null ? void 0 : e.beyondTheBoundary) != null && E.limit && o.setLimitBounds(M.getBounds()), o.add(M);
}
return this.mapInstance = o, this.initEvent(), Promise.resolve(o);
}
initEvent() {
this.mapInstance.on("complete", () => {
this._rotate = this.mapInstance.getRotation(), this._zoom = this.mapInstance.getZoom(), this.emit("complete");
}), this.mapInstance.on("zoomchange", () => {
const t = this.mapInstance.getZoom();
this._zoom = t, this.emit("zoom", t);
}), this.mapInstance.on("click", (t) => {
this.emit("click", {
e: t.originEvent,
position: new a(t.lnglat.lng, t.lnglat.lat),
pixel: new h(t.pixel.x, t.pixel.y),
pos: new h(t.pos[0], t.pos[1])
});
}), this.mapInstance.on("dblclick", (t) => {
this.emit("dblclick", {
e: t.originEvent,
position: new a(t.lnglat.lng, t.lnglat.lat),
pixel: new h(t.pixel.x, t.pixel.y),
pos: new h(t.pos[0], t.pos[1])
});
}), this.mapInstance.on("rightclick", (t) => {
this.emit("rightclick", {
e: t.originEvent,
position: new a(t.lnglat.lng, t.lnglat.lat),
pixel: new h(t.pixel.x, t.pixel.y),
pos: new h(t.pos[0], t.pos[1])
});
}), this.mapInstance.on("mapmove", (t) => {
const e = this.mapInstance.getCenter();
this.emit("move", {
center: new a(e.lng, e.lat),
pos: new h(e.pos[0], e.pos[1])
});
}), this.mapInstance.on("rotatechange", (t) => {
this._rotate = this.mapInstance.getRotation(), this.emit("rotate", this._rotate);
});
}
/** 添加点 */
addMarker(t) {
const e = new k(
{
_map: this.mapInstance,
_top: this._ctx,
AMap: this.AMap
},
t
);
return Promise.resolve(e);
}
/** 删除点 */
removeMarker(t) {
return Array.isArray(t) ? t.forEach((e) => e.remove()) : t.remove(), Promise.resolve();
}
/** 添加折线 */
addPolyline(t) {
const e = new b(
{
_map: this.mapInstance,
_top: this._ctx,
AMap: this.AMap
},
t
);
return Promise.resolve(e);
}
/** 删除折线 */
removePolyline(t) {
return Array.isArray(t) ? t.forEach((e) => e.remove()) : t.remove(), Promise.resolve();
}
/** 添加多边形 */
addPolygon(t) {
const e = new W(
{
_map: this.mapInstance,
_top: this._ctx,
AMap: this.AMap
},
t
);
return Promise.resolve(e);
}
/** 删除多边形 */
removePolygon(t) {
return Array.isArray(t) ? t.forEach((e) => e.remove()) : t.remove(), Promise.resolve();
}
/** 飞行到某个坐标 */
panTo(t, e) {
const o = new a(...t);
this.mapInstance.panTo(o.toArray());
}
/** 设置中心点 */
setCenter(t) {
this.mapInstance.setCenter(t);
}
/** 获取中心点 */
getCenter() {
const t = this.mapInstance.getCenter();
return Promise.resolve(new a(t.lng, t.lat));
}
/** 设置地图显示的缩放级别 */
setZoom(t) {
this.mapInstance.setZoom(t);
}
/** 获取东北角 */
getNorthEast() {
const t = this.mapInstance.getBounds().getNorthWest(), e = this.mapInstance.getBounds().getSouthEast();
return Promise.resolve(new a(e.lng, t.lat));
}
/** 获取西北角 */
getNorthWest() {
const t = this.mapInstance.getBounds().getNorthWest();
return Promise.resolve(new a(t.lng, t.lat));
}
/** 获取东南角 */
getSouthEast() {
const t = this.mapInstance.getBounds().getSouthEast();
return Promise.resolve(new a(t.lng, t.lat));
}
/** 获取西南角 */
getSouthWest() {
const t = this.mapInstance.getBounds().getNorthWest(), e = this.mapInstance.getBounds().getSouthEast();
return Promise.resolve(new a(t.lng, e.lat));
}
/** 获取地图边角点位 */
getViewBounds() {
const t = this.mapInstance.getBounds().getNorthWest(), e = this.mapInstance.getBounds().getSouthEast();
return Promise.resolve({
NorthEast: new a(e.lng, t.lat),
NorthWest: new a(t.lng, t.lat),
SouthEast: new a(e.lng, e.lat),
SouthWest: new a(t.lng, e.lat)
});
}
getNavigationPlan(t, e, o, l, n) {
if (o !== R.DRIVING)
throw new Error("[AMap.getNavigationPlan] 目前仅支持驾车导航");
return new Promise((c, y) => {
var g, u;
const w = new this.AMap.LngLat(t.longitude, t.latitude), I = new this.AMap.LngLat(e.longitude, e.latitude), v = /* @__PURE__ */ new Map([
[S.LEAST_TIME, 0],
[S.LEAST_DISTANCE, 2],
[S.AVOID_HIGHWAYS, 1]
]), p = [];
function A(E, s) {
if (E === "complete") {
const r = s.routes[0];
r.steps.forEach((d) => {
d.path.forEach((C) => {
p.push(new a(C.lng, C.lat));
});
});
const M = {
time: r.time,
distance: r.distance,
instruction: r.steps.map((d) => d.instruction).join(""),
path: p
};
return c(M);
} else
return y("获取驾车数据失败:" + s);
}
new this.AMap.Driving({
// map: this.mapInstance,
policy: v.get(l),
// 策略
panel: (g = n == null ? void 0 : n.panel) != null && g.isShow ? (u = n == null ? void 0 : n.panel) == null ? void 0 : u.panelDom : void 0
// 面板
}).search(w, I, A);
});
}
/** 销毁地图 */
destroy() {
this.mapInstance.destroy();
}
/** 自适应地图范围 */
setFitView(t) {
return this.mapInstance.setFitView(t), Promise.resolve();
}
/** 根据像素和缩放级别计算距离 */
getDistanceFromPixels(t, e) {
const n = 4007501668557849e-8 / (256 * Math.pow(2, e ?? 1));
return Promise.resolve(t * n);
}
}
export {
f as AMap
};