@tuoyuan/map-adapter-amap
Version:
高德地图适配器
150 lines (149 loc) • 5.16 kB
JavaScript
var _ = Object.defineProperty;
var g = (l, o, t) => o in l ? _(l, o, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[o] = t;
var c = (l, o, t) => g(l, typeof o != "symbol" ? o + "" : o, t);
import { BaseMarker as I, Position as u, Pixel as f } from "@tuoyuan/map-adapter-lib";
class v extends I {
constructor(t, e) {
super(t._top, e, { basePlacement: "right-bottom", labelBasePlacement: "top" });
c(this, "_ctx");
/** 高德地图marker实例 */
c(this, "iconInstance");
c(this, "labelInstance");
this._ctx = t, this.renderIcon(), this.renderLabel(), this.addListener((i, s) => {
this._ctx._top.options.dev && (console.groupCollapsed("[Marker.event]", i), console.log("payload: ", s), console.groupEnd());
});
}
initEvent() {
["click", "dblclick", "rightclick"].forEach((e) => {
this.iconInstance.on(e, (i) => {
this.emit(e, {
e: i.originEvent,
position: new u(i.lnglat.lng, i.lnglat.lat),
pixel: new f(i.pixel.x, i.pixel.y),
pos: new f(i.pos[0], i.pos[1]),
data: this.data
});
});
});
}
renderIcon() {
var e, i, s;
this.iconInstance && this.iconInstance.remove();
const t = {
title: this._name,
position: this._position.toArray(),
icon: new this._ctx.AMap.Icon({
image: (e = this._icon) == null ? void 0 : e.image,
imageSize: (i = this._icon) == null ? void 0 : i.size.toArray()
}),
visible: this._visible,
offset: (s = this._icon) == null ? void 0 : s.getRealOffset().toArray()
};
this.iconInstance = new this._ctx.AMap.Marker(t), this._ctx._map.add(this.iconInstance), this.initEvent();
}
renderLabel() {
var e, i, s, n, r, a, h, p, d, m, b;
if (this.labelInstance && this.labelInstance.remove(), !((e = this._label) != null && e.text)) return;
const t = {
name: this._name,
position: this._position.toArray(),
text: (i = this._label) == null ? void 0 : i.text,
visible: (s = this._label) == null ? void 0 : s.visible,
// 动态计算位置和偏移量
anchor: "bottom-center",
offset: ((h = (a = (r = (n = this._label) == null ? void 0 : n.getRealOffset) == null ? void 0 : r.call(n, {
padding: [2, 3, 2, 3]
})) == null ? void 0 : a.toArray) == null ? void 0 : h.call(a)) ?? [0, (p = this._icon) == null ? void 0 : p.getRealOffset().y],
style: {
border: "none",
"background-color": (d = this._label) == null ? void 0 : d.backgroundColor,
"font-size": ((m = this._label) == null ? void 0 : m.fontSize) + "px",
color: (b = this._label) == null ? void 0 : b.color
}
};
this.labelInstance = new this._ctx.AMap.Text(t), this._ctx._map.add(this.labelInstance);
}
get map() {
return this.iconInstance.getMap();
}
setName(t) {
super.setName(t), this.iconInstance.name = this._name;
}
setPosition(t) {
super.setPosition(t), this.iconInstance.setPosition(this._position.toArray());
}
setIcon(t) {
super.setIcon(t), this.renderIcon();
}
setLabel(t) {
super.setLabel(t), this.renderLabel();
}
hidden() {
super.hidden(), this.iconInstance.hide();
}
show() {
super.show(), this.iconInstance.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.iconInstance, this.labelInstance].filter((e) => !!e);
}
remove() {
this.getInstances().forEach((t) => t.remove());
}
panTo() {
this.map.panTo(this._position.toArray());
}
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 s = {
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: this._position.toArray()
}, n = new this._ctx.AMap.InfoWindow(s);
n.open(this._ctx._map), n.dom.style.height = "0";
const r = n.dom.querySelector(".amap-info-contentContainer");
r && (r.style.pointerEvents = "none"), n.on("close", () => {
const a = n.dom;
a.parentNode.removeChild(a);
});
}
closeInfoWindow() {
this._ctx._map.clearInfoWindow();
}
setStyle(t) {
super.setStyle(t), this.renderIcon(), this.renderLabel();
}
}
export {
v as AMapMarker
};