@tuoyuan/map-adapter-tmap
Version:
天地图适配器
149 lines (148 loc) • 5.84 kB
JavaScript
var g = Object.defineProperty;
var _ = (o, i, t) => i in o ? g(o, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[i] = t;
var a = (o, i, t) => _(o, typeof i != "symbol" ? i + "" : i, t);
import { BaseMarker as f, Position as u, Pixel as x, pxToPt as w, getDPI as v } from "@tuoyuan/map-adapter-lib";
class T extends f {
constructor(t, e) {
super(t._top, e, {
basePlacement: "right-bottom",
labelBasePlacement: "right-bottom"
});
a(this, "_ctx");
a(this, "currentLngLat");
/** 天地图marker实例 */
a(this, "iconInstance");
a(this, "labelInstance");
a(this, "busInstance");
// 组件实例 信息窗口内
a(this, "app");
a(this, "container");
this._ctx = t, this.currentLngLat = new t.TMap.LngLat(e == null ? void 0 : e.position[0], e == null ? void 0 : e.position[1]), this.iconInstance = new this._ctx.TMap.Marker(this.currentLngLat), this.renderIcon(), this.renderLabel(), this.addListener((n, s) => {
this._ctx._top.options.dev && (console.groupCollapsed("[Marker.event]", n), console.log("payload: ", s), console.groupEnd());
});
}
initEvent() {
["click", "dblclick", "rightclick"].forEach((e) => {
this.iconInstance.on(e, (n) => {
this.emit(e, {
e: n.originalEvent,
position: new u(n.lnglat.lng, n.lnglat.lat),
pixel: new x(n.containerPoint.x, n.containerPoint.y),
data: this.data
});
});
});
}
renderIcon() {
var n, s, c, l;
this.iconInstance && this._ctx._map.removeOverLay(this.iconInstance);
const t = (n = this._icon) == null ? void 0 : n.getRealOffset(), e = new this._ctx.TMap.Icon({
iconUrl: (s = this._icon) == null ? void 0 : s.image,
iconSize: new this._ctx.TMap.Point((c = this._icon) == null ? void 0 : c.size.width, (l = this._icon) == null ? void 0 : l.size.height),
iconAnchor: new this._ctx.TMap.Point(-((t == null ? void 0 : t.x) ?? 0), -((t == null ? void 0 : t.y) ?? 0))
});
this.iconInstance = new this._ctx.TMap.Marker(this.currentLngLat, {
icon: e
}), this._ctx._map.addOverLay(this.iconInstance), this.initEvent();
}
renderLabel() {
var s, c, l, h, r, d, p, I, L, b;
if (this.labelInstance && this._ctx._map.removeOverLay(this.labelInstance), !((s = this._label) != null && s.text)) return;
const t = (r = (h = (l = (c = this._label).getRealOffset) == null ? void 0 : l.call(c, {
// 天地图label容器左右内边距10px,自定义容器上下容器内边距2px
padding: [2, 10, 2, 10]
})) == null ? void 0 : h.toArray) == null ? void 0 : r.call(h), e = new this._ctx.TMap.Point(
t[0] - 12,
// 天地图label 横向偏移量
t[1] + 12
// 天地图label 纵向偏移量
), n = {
text: `<div style="line-height: 1.2; padding: 2px 0; font-size: ${(d = this._label) == null ? void 0 : d.fontSize}px">${((p = this._label) == null ? void 0 : p.text) || ""}</div>`,
position: this.currentLngLat,
offset: e
};
this.labelInstance = new this._ctx.TMap.Label(n), this.labelInstance.setBorderLine(0), this.labelInstance.setFontColor((I = this._label) == null ? void 0 : I.color), this.labelInstance.setBackgroundColor((L = this._label) == null ? void 0 : L.backgroundColor), ((b = this._label) == null ? void 0 : b.visible) === !1 && this.labelInstance.hide(), this._ctx._map.addOverLay(this.labelInstance);
}
setName(t) {
super.setName(t);
}
setIcon(t) {
super.setIcon(t), this.iconInstance.getIcon().setIconUrl(t);
}
// 更改icon 位置
setPosition(t) {
super.setPosition(t), this.currentLngLat = this._ctx.TMap.LngLat(t[0], t[1]), this.iconInstance.setLngLat(this.currentLngLat), this.setLabelPosition(t);
}
// 更改label 位置
setLabelPosition(t) {
this.currentLngLat = this._ctx.TMap.LngLat(t[0], t[1]), this.labelInstance.setLngLat(this.currentLngLat);
}
setLabel(t) {
var e;
super.setLabel(t), (e = this.labelInstance) == null || e.setLabel(t);
}
setFontSize(t) {
var e;
(e = this.labelInstance) == null || e.setFontSize(w(t, v()));
}
setBackgroundColor(t) {
var e;
(e = this.labelInstance) == null || e.setBackgroundColor(t);
}
openInfoWindow(t) {
if (t.url) {
const 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"
scrolling="auto">
</iframe>
</div>
`, n = new this._ctx.TMap.InfoWindow({});
n.setContent(e), this.iconInstance.openInfoWindow(n);
} else {
const e = `
<div :closeButton="false" style="width:${t.size.width}px; height:${t.size.height}px; overflow: hidden; pointer-events: none;">
${t.html}
</div>
`, n = new this._ctx.TMap.InfoWindow();
n.setContent(e);
const s = new this._ctx.TMap.Point(10, 0);
n.setOffset(s), this.iconInstance.openInfoWindow(n);
}
}
closeInfoWindow() {
this.iconInstance.closeInfoWindow();
}
setStyle(t) {
super.setStyle(t), this.renderIcon(), this.renderLabel();
}
hidden() {
super.hidden(), this.iconInstance.setOpacity(0);
}
show() {
super.show(), this.iconInstance.setOpacity(1);
}
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.closeInfoWindow(), this.getInstances().forEach((t) => this._ctx._map.removeOverLay(t));
}
panTo() {
this._ctx._map.panTo(this.currentLngLat);
}
}
export {
T as TMapMarker
};