@tuoyuan/map-adapter-lib
Version:
地图适配器库
210 lines (209 loc) • 5.35 kB
JavaScript
var f = Object.defineProperty;
var w = (b, h, t) => h in b ? f(b, h, { enumerable: !0, configurable: !0, writable: !0, value: t }) : b[h] = t;
var n = (b, h, t) => w(b, typeof h != "symbol" ? h + "" : h, t);
import { deepAssign as c } from "../utils/deep-assign.js";
import { getDistance as L } from "../utils/get-distance.js";
import { EventBus as y } from "./MapEvent.js";
import { Position as m } from "./Position.js";
import { Bounds as v } from "./Bounds.js";
import { Label as P } from "./Label.js";
class S extends y {
constructor(t, e, s) {
var l, o, i, r, d, a, _, u, g;
super();
n(this, "_top");
/** ID */
n(this, "_id");
/** 名称 */
n(this, "_name");
/** 路径 */
n(this, "_path");
/** 样式 */
n(this, "_style");
/** 图标 */
n(this, "_icon");
/** 文本标签 */
n(this, "_label");
/** 业务数据 */
n(this, "_data");
/** 是否可见 */
n(this, "_visible");
/** label基础方位 */
n(this, "_labelBasePlacement", "center");
this._top = t, this._labelBasePlacement = s.labelBasePlacement ?? "center", this._id = e.id, this._name = e.name, this._path = e.path.map((p) => new m(...p)), this._visible = e.visible ?? !0, this._data = e.data, this._style = c(
{},
t.options.style.polyline,
{
line: {
style: (o = (l = e.style) == null ? void 0 : l.line) == null ? void 0 : o.style,
color: (r = (i = e.style) == null ? void 0 : i.line) == null ? void 0 : r.color,
width: (a = (d = e.style) == null ? void 0 : d.line) == null ? void 0 : a.width,
opacity: (u = (_ = e.style) == null ? void 0 : _.line) == null ? void 0 : u.opacity
}
}
), this._label = this.genLabel(
t,
c(
{},
t.options.label,
t.options.style.polyline.label,
e.label,
(g = e.style) == null ? void 0 : g.label
)
), this._label.listenAndUpdateVisible({
showLabel: this.showLabel.bind(this),
hiddenLabel: this.hiddenLabel.bind(this)
});
}
genLabel(t, e) {
return new P(t, {
canShow: e.visible !== !1,
minShowZoom: e.minShowZoom,
color: e.color,
backgroundColor: e.backgroundColor,
fontSize: e.fontSize,
placement: e.placement,
visible: e.visible,
text: e.text,
basePlacement: this._labelBasePlacement
});
}
/** ID */
get id() {
return this._id;
}
/** 名称 */
get name() {
return this._name;
}
/** 位置 */
get path() {
return this._path;
}
/** 图标 */
get icon() {
return this._icon;
}
/** 业务数据 */
get data() {
return this._data;
}
/**
* 设置名称
* @param name 名称
*/
setName(t) {
this._name = t;
}
/**
* 设置路径
* @param path 路径
*/
setPath(t) {
this._path = t.map((e) => new m(...e));
}
/**
* 设置样式
* @param style 样式
*/
setStyle(t) {
var e, s, l, o;
this._style = c({}, this._style, t), this._label = this.genLabel(
this._top,
c(
{},
this._top.options.label,
this._top.options.style.polyline.label,
{
text: (e = this._label) == null ? void 0 : e.text,
minShowZoom: (s = this._label) == null ? void 0 : s.minShowZoom,
visible: (l = this._label) == null ? void 0 : l.visible
},
(o = this._style) == null ? void 0 : o.label
)
), this._label.listenAndUpdateVisible({
showLabel: this.showLabel.bind(this),
hiddenLabel: this.hiddenLabel.bind(this)
});
}
/**
* 设置业务数据
* @param data 业务数据
*/
setData(t) {
this._data = t;
}
/**
* 显示
*/
show() {
var t;
this._visible = !0, (t = this._label) != null && t.canShow && this.showLabel();
}
/**
* 隐藏
*/
hidden() {
this._visible = !1, this.hiddenLabel();
}
/**
* 显示label
*/
showLabel() {
var t, e;
(e = (t = this._label) == null ? void 0 : t.setVisible) == null || e.call(t, !0);
}
/**
* 隐藏label
*/
hiddenLabel() {
var t, e;
(e = (t = this._label) == null ? void 0 : t.setVisible) == null || e.call(t, !1);
}
/**
* 获取折线的总长度(米)
*/
getLength() {
return this._path.reduce((e, s, l) => (l < this._path.length - 1 && (e += L(s, this._path[l + 1])), e), 0);
}
/**
* 判断坐标是否在折线内
* @param point 点位坐标
*/
contains(t) {
for (let e = 0; e < this._path.length; e++)
if (this._path[e].equals(t)) return !0;
return !1;
}
/**
* 获取当前折线的矩形范围对象
*/
getBounds() {
return v.getBounds(this._path);
}
/**
* 查找距离质心最近的点
*/
findClosestPointToCentroid(t) {
function e(i) {
let r = 0, d = 0;
const a = i.length;
for (let _ = 0; _ < a; _++)
r += i[_].longitude, d += i[_].latitude;
return new m(r / a, d / a);
}
const s = e(t);
let l = null, o = 1 / 0;
for (let i = 0; i < t.length; i++) {
const r = t[i].longitude - s.longitude, d = t[i].longitude - s.longitude, a = Math.sqrt(r * r + d * d);
a < o && (o = a, l = t[i]);
}
return l;
}
}
class k extends S {
}
export {
k as AbstractPolyline,
S as BasePolyline
};