@tuoyuan/map-adapter-lib
Version:
地图适配器库
107 lines (106 loc) • 4.18 kB
JavaScript
var u = Object.defineProperty;
var g = (s, t, i) => t in s ? u(s, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : s[t] = i;
var e = (s, t, i) => g(s, typeof t != "symbol" ? t + "" : t, i);
import { Pixel as a } from "./Pixel.js";
import { Size as S } from "./Size.js";
import { measureText as r } from "../utils/measure-text.js";
class b {
constructor(t, i) {
e(this, "_top");
/** 可以显示label */
e(this, "_canShow");
/** 最小显示的缩放级别 */
e(this, "_minShowZoom");
/** 文本内容 */
e(this, "_text");
/** 文字颜色 */
e(this, "_color");
/** 背景颜色 */
e(this, "_backgroundColor");
/** 文字尺寸 */
e(this, "_fontSize");
/** 是否显示 */
e(this, "_visible");
/** 边框大小 */
e(this, "_border_size");
/** 边框颜色 */
e(this, "_border_color");
/** 地图显示的基础方位 */
e(this, "_basePlacement");
/** 显示方位 */
e(this, "_placement", "top");
/** 绑定的图标 */
e(this, "_icon", null);
this._top = t, this._canShow = i.canShow, this._minShowZoom = i.minShowZoom, this._text = i.text, this._color = i.color, this._backgroundColor = i.backgroundColor, this._fontSize = i.fontSize, this._visible = i.visible, this._border_size = i.borderSize, this._border_color = i.borderColor, this._basePlacement = i.basePlacement, this._placement = i.placement ?? "top", this._icon = i.icon ?? null;
}
get canShow() {
return this._canShow;
}
get minShowZoom() {
return this._minShowZoom;
}
get text() {
return this._text;
}
get color() {
return this._color;
}
get backgroundColor() {
return this._backgroundColor;
}
get fontSize() {
return this._fontSize;
}
get borderSize() {
return this._border_size;
}
get borderColor() {
return this._border_color;
}
get visible() {
return this._visible;
}
get placement() {
return this._placement;
}
setText(t) {
this._text = t;
}
setVisible(t) {
this._visible = t;
}
getFont() {
const t = window.getComputedStyle(this._top.mapInstance.dom).getPropertyValue("font-family"), i = this._fontSize ?? 12, n = `${i + "px"} ${t}`;
return {
fontFamily: t,
fontSize: i,
font: n
};
}
getSize() {
const { font: t } = this.getFont(), i = r(this.text ?? "", t);
return new S(i.width, i.height);
}
getRealOffset(t) {
const [i, n, m, l] = (t == null ? void 0 : t.padding) ?? [0, 0, 0, 0], { font: f } = this.getFont(), o = r(this.text ?? "", f);
o.width += l + n, o.height += i + m;
const h = {
x: 0,
y: 0
};
this._basePlacement.includes("top") && (h.y += 0.5), this._basePlacement.includes("bottom") && (h.y -= 0.5), this._basePlacement.includes("left") && (h.x += 0.5), this._basePlacement.includes("right") && (h.x -= 0.5);
let c = o.width * h.x, _ = o.height * h.y;
return this._icon && (this._placement.includes("top") && (_ += -(this._icon.size.height / 2 + o.height / 2)), this._placement.includes("bottom") && (_ += this._icon.size.height / 2 + o.height / 2), this._placement.includes("left") && (c += -(this._icon.size.width / 2 + o.width / 2)), this._placement.includes("right") && (c += this._icon.size.width / 2 + o.width / 2), this._icon.placement.includes("top") && (_ += -this._icon.size.height / 2), this._icon.placement.includes("bottom") && (_ += this._icon.size.height / 2), this._icon.placement.includes("left") && (c += -this._icon.size.width / 2), this._icon.placement.includes("right") && (c += this._icon.size.width / 2), c += this._icon.offset.x, _ += this._icon.offset.y), new a(c, _);
}
listenAndUpdateVisible({
showLabel: t,
hiddenLabel: i
}) {
this._visible && !this._minShowZoom && (t == null || t()), this._canShow && this._minShowZoom && (this._top.mapInstance.zoom >= this._minShowZoom && this._visible ? t == null || t() : i == null || i(), this._top.mapInstance.on("zoom", (n) => {
n >= this._minShowZoom && !this._visible && (t == null || t()), n < this._minShowZoom && this._visible && (i == null || i());
}));
}
}
export {
b as Label
};