UNPKG

@tuoyuan/map-adapter-tmap

Version:

天地图适配器

130 lines (129 loc) 5.36 kB
var w = Object.defineProperty; var v = (l, i, t) => i in l ? w(l, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[i] = t; var o = (l, i, t) => v(l, typeof i != "symbol" ? i + "" : i, t); import { BasePolygon as L, Position as m, Pixel as P, getDPI as u, pxToPt as x } from "@tuoyuan/map-adapter-lib"; class T extends L { constructor(t, e) { super(t._top, e, { labelBasePlacement: "right-bottom" }); o(this, "_ctx"); o(this, "polygonData"); o(this, "currentLngLat"); /** 天地图polygon实例 */ o(this, "polygonInstance"); o(this, "labelInstance"); this._ctx = t, this.polygonData = e, this.renderPolygon(), this.renderLabel(), this.addListener((n, s) => { this._ctx._top.options.dev && (console.groupCollapsed("[Polygon.event]", n), console.log("payload: ", s), console.groupEnd()); }); } initEvent() { ["click", "dblclick", "rightclick"].forEach((e) => { this.polygonInstance.on(e, (n) => { this.emit(e, { e: n.originalEvent, position: new m(n.lnglat.lng, n.lnglat.lat), pixel: new P(n.containerPoint.x, n.containerPoint.y), data: this.data }); }); }); } renderPolygon() { var e, n, s, d, a, h, c; this.polygonInstance && this._ctx._map.removeOverLay(this.polygonInstance); const t = []; (e = this._path) == null || e.map((r) => { t.push(this._ctx.TMap.LngLat(r.longitude, r.latitude)); }), this.polygonInstance = new this._ctx.TMap.Polygon(t, { lineStyle: (n = this._style.line) == null ? void 0 : n.style, color: (s = this._style.line) == null ? void 0 : s.color, weight: (d = this._style.line) == null ? void 0 : d.width, opacity: (a = this._style.line) == null ? void 0 : a.opacity, fillColor: (h = this._style.fill) == null ? void 0 : h.color, fillOpacity: (c = this._style.fill) == null ? void 0 : c.opacity }), this._ctx._map.addOverLay(this.polygonInstance), this.initEvent(); } renderLabel() { var a, h, c, r, p, g, y, b, _, I, f; if (this.labelInstance && this._ctx._map.removeOverLay(this.labelInstance), !((a = this._label) != null && a.text)) return; const t = u(), e = (h = this.polygonInstance.getBounds()) == null ? void 0 : h.getCenter(), n = (g = (p = (r = (c = this._label).getRealOffset) == null ? void 0 : r.call(c, { // 天地图label容器左右内边距10px,自定义容器上下容器内边距2px padding: [2, 10, 2, 10] })) == null ? void 0 : p.toArray) == null ? void 0 : g.call(p); console.log(this._style, "this._label"); const s = new this._ctx.TMap.Point( n[0] - 12, // 天地图label 横向偏移量 n[1] + 15 // 天地图label 纵向偏移量 ); console.log(this._label, "this._label"); const d = { text: `<div style="line-height: 1.2; padding: 2px 0;">${((y = this._label) == null ? void 0 : y.text) || ""}</div>`, position: new this._ctx.TMap.LngLat(e.lng, e.lat), offset: s }; this.labelInstance = new this._ctx.TMap.Label(d), this.labelInstance.setBorderLine(0), this.labelInstance.setFontColor((b = this._style.label) == null ? void 0 : b.color), this.labelInstance.setFontSize(x((_ = this._style.label) == null ? void 0 : _.fontSize, t)), this.labelInstance.setBackgroundColor((I = this._style.label) == null ? void 0 : I.backgroundColor), ((f = this._label) == null ? void 0 : f.visible) === !1 && this.labelInstance.hide(), this._ctx._map.addOverLay(this.labelInstance); } setFontSize(t) { var e; (e = this.labelInstance) == null || e.setFontSize(x(t, u())); } setBackgroundColor(t) { var e; (e = this.labelInstance) == null || e.setBackgroundColor(t); } setPath(t) { var n; super.setPath(t); const e = []; (n = this.path) == null || n.map((s) => { e.push(this._ctx.TMap.LngLat(s.longitude, s.latitude)); }), this.polygonInstance.setLngLats(e); } setStyle(t) { super.setStyle(t), this.renderPolygon(), this.renderLabel(); } openInfoWindow(t) { 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" scrolling="auto"> </iframe> </div>` : e = `<div style="width:${t.size.width}px; height:${t.size.height}px; overflow: hidden; pointer-events: none;"> ${t.html} </div>`; const n = new this._ctx.TMap.InfoWindow(); n.setContent(e), this.polygonInstance.openInfoWindow(n); } closeInfoWindow() { this.polygonInstance.closeInfoWindow(); } hidden() { super.hidden(), this.polygonInstance.setOpacity(0); } show() { super.show(), this.polygonInstance.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.polygonInstance, this.labelInstance].filter((e) => !!e); } remove() { this.closeInfoWindow(), this.getInstances().forEach((t) => this._ctx._map.removeOverLay(t)); } } export { T as TMapPolygon };