@tuoyuan/map-adapter-amap
Version:
高德地图适配器
147 lines (146 loc) • 5.49 kB
JavaScript
var u = Object.defineProperty;
var w = (a, l, t) => l in a ? u(a, l, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[l] = t;
var r = (a, l, t) => w(a, typeof l != "symbol" ? l + "" : l, t);
import { BasePolyline as I, Position as p, Pixel as b, Bounds as g } from "@tuoyuan/map-adapter-lib";
class k extends I {
constructor(t, e) {
super(t._top, e, {});
r(this, "_ctx");
r(this, "polylineInstance");
r(this, "labelInstance");
r(this, "infoWindowInstance");
this._ctx = t, this.renderPolyline(), this.renderLabel(), this.addListener((n, i) => {
this._ctx._top.options.dev && (console.groupCollapsed("[Polyline.event]", n), console.log("payload: ", i), console.groupEnd());
});
}
initEvent() {
["click", "dblclick", "rightclick"].forEach((e) => {
this.polylineInstance.on(e, (n) => {
this.emit(e, {
e: n.originEvent,
position: new p(n.lnglat.lng, n.lnglat.lat),
pixel: new b(n.pixel.x, n.pixel.y),
pos: new b(n.pos[0], n.pos[1]),
data: this.data
});
});
});
}
renderPolyline() {
var e, n, i, s;
this.polylineInstance && this.polylineInstance.remove();
const t = {
path: this._path.map((o) => o.toArray()),
strokeColor: (e = this._style.line) == null ? void 0 : e.color,
strokeOpacity: (n = this._style.line) == null ? void 0 : n.opacity,
strokeWeight: (i = this._style.line) == null ? void 0 : i.width,
strokeStyle: (s = this._style.line) == null ? void 0 : s.style
};
this.polylineInstance = new this._ctx.AMap.Polyline(t), this._ctx._map.add(this.polylineInstance), this.initEvent();
}
renderLabel() {
var n, i, s, o, h, c, d, y, f, m, _;
if (this.labelInstance && this.labelInstance.remove(), !((n = this._label) != null && n.text)) return;
const t = super.findClosestPointToCentroid(this._path), e = {
name: this._name,
position: t == null ? void 0 : t.toArray(),
text: (i = this._label) == null ? void 0 : i.text,
visible: (s = this._label) == null ? void 0 : s.visible,
// 动态计算位置和偏移量
anchor: "bottom-center",
offset: ((d = (c = (h = (o = this._label) == null ? void 0 : o.getRealOffset) == null ? void 0 : h.call(o, {
padding: [2, 3, 2, 3]
})) == null ? void 0 : c.toArray) == null ? void 0 : d.call(c)) ?? [0, (y = this._icon) == null ? void 0 : y.getRealOffset().y],
style: {
border: "none",
"background-color": (f = this._label) == null ? void 0 : f.backgroundColor,
"font-size": ((m = this._label) == null ? void 0 : m.fontSize) + "px",
color: (_ = this._label) == null ? void 0 : _.color
}
};
this.labelInstance = new this._ctx.AMap.Text(e), this._ctx._map.add(this.labelInstance);
}
setPath(t) {
super.setPath(t), this.polylineInstance.setPath(this._path.map((e) => e.toArray()));
}
setStyle(t) {
var n, i, s, o;
super.setStyle(t);
const e = {
strokeColor: (n = this._style.line) == null ? void 0 : n.color,
strokeOpacity: (i = this._style.line) == null ? void 0 : i.opacity,
strokeWeight: (s = this._style.line) == null ? void 0 : s.width,
strokeStyle: (o = this._style.line) == null ? void 0 : o.style
};
this.polylineInstance.setOptions(e);
}
hidden() {
super.hidden(), this.polylineInstance.hide();
}
show() {
super.show(), this.polylineInstance.show();
}
showLabel() {
var t, e;
super.showLabel(), (e = (t = this.labelInstance) == null ? void 0 : t.show) == null || e.call(t);
}
hiddenLabel() {
var t, e;
super.hiddenLabel(), (e = (t = this.labelInstance) == null ? void 0 : t.hide) == null || e.call(t);
}
getInstances() {
return [this.polylineInstance, this.labelInstance].filter((e) => !!e);
}
remove() {
this.closeInfoWindow(), this.getInstances().forEach((t) => t.remove());
}
panTo() {
this._ctx._map.setFitView(this.getInstances());
}
getBounds() {
const { northEast: t, southWest: e } = this.polylineInstance.getBounds();
return g.getBounds([new p(t.lng, t.lat), new p(e.lng, e.lat)]);
}
openInfoWindow(t) {
this.closeInfoWindow();
let e = "";
t.url ? e = `<div style="width:${t.size.width}px; height:${t.size.height}px; padding:10px;">
<iframe src=${t.url}
width="100%"
height="100%"
frameborder="0">
</iframe>
</div>` : e = `<div style="width:${t.size.width}px; height:${t.size.height}px; overflow: hidden; pointer-events: auto;">
${t.html}
</div>`;
const i = {
isCustom: !0,
autoMove: !1,
closeWhenClickMap: !1,
content: e,
anchor: {
bottom: "top-center",
top: "bottom-center",
right: "middle-left",
left: "middle-right",
"right-bottom": "top-left",
"right-top": "bottom-left",
"left-bottom": "top-right",
"left-top": "bottom-right"
}[t.placement ?? "top"],
position: g.getCenter(this.getBounds()).toArray()
}, s = new this._ctx.AMap.InfoWindow(i);
s.open(this._ctx._map), s.dom.style.height = "0";
const o = s.dom.querySelector(".amap-info-contentContainer");
o && (o.style.pointerEvents = "none"), s.on("close", () => {
const h = s.dom;
h.parentNode.removeChild(h);
});
}
closeInfoWindow() {
this._ctx._map.clearInfoWindow();
}
}
export {
k as AMapPolyline
};