@tuoyuan/map-container
Version:
地图容器组件
554 lines (553 loc) • 20.4 kB
JavaScript
var Ae = Object.defineProperty;
var Te = (G, f, u) => f in G ? Ae(G, f, { enumerable: !0, configurable: !0, writable: !0, value: u }) : G[f] = u;
var $ = (G, f, u) => Te(G, typeof f != "symbol" ? f + "" : f, u);
import je from "../../../node_modules/gcoord/dist/gcoord.esm-bundler.js";
import q from "../apis/index.js";
import { deepAssign as H } from "../utils/deep-assign.js";
import { debounce as Ie } from "../utils/debounce.js";
class Pe {
constructor() {
$(this, "_id");
this._id = 0;
}
get id() {
return this._id;
}
next() {
return this._id += 1, this.id;
}
}
const J = [];
class De {
constructor() {
$(this, "idManager", new Pe());
$(this, "idMap", /* @__PURE__ */ new Map());
$(this, "geometrySourceMap", /* @__PURE__ */ new Map());
}
genId() {
return this.idManager.next();
}
// 检查id是否存在
checkId(f) {
return this.idMap.has(f);
}
getGeometry(f) {
if (this.idMap.has(f))
return this.geometrySourceMap.get(this.idMap.get(f));
}
filterGeometry(f) {
const u = [];
for (const [S, P] of this.geometrySourceMap)
f(P) && u.push(P);
return u;
}
bindGeometry(f, u) {
this.idMap.set(f.id, u.id);
const S = {
id: f.id,
type: f.type,
name: f.name,
geometry: u,
data: f.data,
visible: f.visible
};
this.geometrySourceMap.set(u.id, S);
}
remove(f) {
const u = this.getGeometry(f);
u && (u.geometry.remove(), this.geometrySourceMap.delete(this.idMap.get(f)), this.idMap.delete(f));
}
}
function Ye({
getMap: G,
bus: f,
options: u
}) {
let S;
class P {
constructor(e) {
$(this, "getMap");
$(this, "_projectConfig", null);
$(this, "mapConfig", null);
$(this, "domain_ids");
$(this, "tags", /* @__PURE__ */ new Set());
this.getMap = e;
}
setProjectConfig(e) {
this._projectConfig = e;
}
setMapConfig(e) {
this.mapConfig = e;
}
getMapConfig() {
return this.mapConfig;
}
get projectConfig() {
return this._projectConfig;
}
canUse() {
if (!(this.projectConfig && this.mapConfig))
throw new Error("projectConfig or mapConfig is not set.");
}
async getRange() {
var c, s, g, a;
const e = this.getMap(), i = await ((c = e == null ? void 0 : e.getNorthEast) == null ? void 0 : c.call(e)), n = await ((s = e == null ? void 0 : e.getSouthEast) == null ? void 0 : s.call(e)), r = await ((g = e == null ? void 0 : e.getNorthWest) == null ? void 0 : g.call(e)), o = await ((a = e == null ? void 0 : e.getSouthWest) == null ? void 0 : a.call(e));
return {
northeast: i,
southeast: n,
northwest: r,
southwest: o
};
}
genResourceGeometryOptions(e, i) {
var c, s, g, a, l, d, y, p;
const n = e.properties, r = n.styles.find((h) => h.is_default), o = {
id: n.resource_id,
name: n.resource_name,
data: {
...e,
...i,
$tags: (n.tags ?? []).map((h) => h.tag_id) ?? []
},
style: r == null ? void 0 : r.style_info,
label: {
text: n.resource_name,
minShowZoom: (s = (c = r == null ? void 0 : r.style_info) == null ? void 0 : c.label) == null ? void 0 : s.minShowZoom,
visible: ((a = (g = r == null ? void 0 : r.style_info) == null ? void 0 : g.label) == null ? void 0 : a.visible) ?? ((l = this.projectConfig) == null ? void 0 : l.is_show_label) ?? !0
}
};
return e.geometry.type === "Point" ? (n.agg_info ? n.agg_info && (o.id = `agg_${n.resource_id}_${+/* @__PURE__ */ new Date()}`, o.type = "marker", o.data.$isAgg = !0, o.position = e.geometry.coordinates, o.label.text = n.agg_info.agg_num + "") : (o.type = "marker", o.data.$isAgg = !1, o.position = e.geometry.coordinates), (p = (y = (d = r == null ? void 0 : r.style_info) == null ? void 0 : d.icon) == null ? void 0 : y.image) != null && p.startsWith("http") || (o.style.icon.image = (u.staticUrl ?? "") + r.style_info.icon.image)) : e.geometry.type === "Polygon" ? (o.type = "polygon", o.path = e.geometry.coordinates[0], o.holes = e.geometry.coordinates.slice(1)) : e.geometry.type === "LineString" && (o.type = "polyline", o.path = e.geometry.coordinates), o;
}
async refresh() {
if (this.canUse(), !this.tags.size) return;
const e = await this.fetchSource({
tag_ids: [...this.tags],
domain_ids: this.domain_ids,
scope: !0,
zoom_level: !0
}), n = [...A((s) => {
var g, a;
return ((g = s.data) == null ? void 0 : g.$isSource) && ((a = s.data) == null ? void 0 : a.$isAgg);
}).map((s) => s.id)], r = A((s) => {
var g, a;
return ((g = s.data) == null ? void 0 : g.$isSource) && !((a = s.data) != null && a.$isAgg);
}).map((s) => s.id), o = [];
for (let s = 0; s < e.length; s++) {
const g = e[s], a = g.properties;
if (r.includes(a.resource_id) && !a.agg_info) continue;
const l = this.genResourceGeometryOptions(g, {
$isSource: !0,
$isExternal: !!a.external_id
});
o.push(l);
}
const c = /* @__PURE__ */ new Set([
...e.filter((s) => {
var g;
return !((g = s.properties) != null && g.agg_info);
}).map((s) => s.properties.resource_id)
]);
r.forEach((s) => {
c.has(s) || n.push(s);
}), await k(o), E(n);
}
async fetchSource(e) {
var c, s, g;
const i = [], n = this.getMap(), r = n.coordinateSystem, o = {
coordinate_system: r
};
if (e.scope) {
const a = await this.getRange(), l = Object.values(a).reduce(
(d, y) => (d.minX = Math.min(d.minX, y.longitude), d.minY = Math.min(d.minY, y.latitude), d.maxX = Math.max(d.maxX, y.longitude), d.maxY = Math.max(d.maxY, y.latitude), d),
{ minX: 1 / 0, minY: 1 / 0, maxX: -1 / 0, maxY: -1 / 0 }
);
o.scope = {
range: [l.minX, l.minY, l.maxX, l.maxY],
coordinate_system: r
};
}
if (S || (S = n.getZoom()), e.tag_ids && (o.tag_ids = e.tag_ids), e.type_ids && (o.type_ids = e.type_ids), e.domain_ids && (o.domain_ids = e.domain_ids), e.resource_ids && (o.resource_ids = e.resource_ids), e.external_ids && (o.external_ids = e.external_ids), e.zoom_level && (o.zoom_level = S), e.local_agg || ((c = this.projectConfig) == null ? void 0 : c.agg_type) === "distance") {
const a = this.getMap(), l = await (((g = a == null ? void 0 : a.getDistanceFromPixels) == null ? void 0 : g.call(a, ((s = this.projectConfig) == null ? void 0 : s.agg_distance) ?? 100, S ?? 1)) ?? 1e3);
o.zoom_level = S, o.chunk_length = l;
}
return e.local_agg && (o.is_agg = !1), await q.mapManage.resource.get(o).then((a) => {
var y, p;
const l = ((y = this.projectConfig) == null ? void 0 : y.is_agg_point) && ((p = this.projectConfig) == null ? void 0 : p.agg_type) === "distance", d = a.data.map((h) => {
const M = { ...h };
return M.geometry.type === "Point" && l && (M.properties.__needDistanceAgg = l), h;
});
i.push(...d);
}), i;
}
async setDomain(e) {
let i;
typeof e == "number" ? i = [e] : Array.isArray(e) && e.length ? i = e : i = void 0, this.domain_ids = i, await this.refresh();
}
async addTag(e) {
e.forEach((i) => {
this.tags.add(i);
}), await this.refresh();
}
async removeTag(e) {
e.forEach((n) => {
this.tags.delete(n);
});
const i = A((n) => {
var o, c;
return (o = n.data) != null && o.$isSource ? (((c = n.data) == null ? void 0 : c.$tags) ?? []).every((s) => !this.tags.has(s)) : !1;
}).map((n) => n.id);
E(i), await this.refresh();
}
hasTag(e) {
return this.tags.has(e);
}
}
const m = new De(), w = new P(G), Q = (t) => {
w.setProjectConfig(t);
}, ee = (t) => {
w.setMapConfig(t);
}, W = Ie(100, () => {
w.refresh();
});
f.$on("move", W), f.$on("zoom", W), f.$on("zoom", (t) => {
S = t;
}), f.$on("rotate", W), f.$on("rotate", (t) => {
});
const D = (t, e) => H(e, { $meta: t });
function te() {
return new Promise((t) => {
requestAnimationFrame(t);
});
}
function ie(t, e) {
let i = 0;
return async () => {
if (i++, i === t) {
for (let n = 0; n < e; n++)
await te();
i = 0;
}
return Promise.resolve();
};
}
const oe = ie(10, 5);
async function k(t) {
var n, r, o, c, s, g;
const e = G(), i = Array.isArray(t) ? t : [t];
for (const a of i) {
const { id: l, type: d, name: y, visible: p } = a;
if ((n = a.data) != null && n.$isSource && !((r = a.data) != null && r.$isAgg) && !((o = a.data) != null && o.$tags.some((_) => w.hasTag(_))))
continue;
const h = D({ id: l, type: d }, a.data ?? {});
if (m.checkId(l)) {
console.warn(`已存在ID为 ${JSON.stringify(l)} 的资源!`);
continue;
}
const M = m.genId();
let v;
if (d === "marker") {
const { position: _, style: x, label: C } = a, b = {
id: M,
name: y,
position: _,
style: x,
label: C,
visible: p,
data: h
};
v = await ((c = e.addMarker) == null ? void 0 : c.call(e, b));
} else if (d === "polyline") {
const { path: _, style: x, label: C } = a, b = {
id: M,
name: y,
path: _,
style: x,
label: C,
visible: p,
data: h
};
v = await ((s = e.addPolyline) == null ? void 0 : s.call(e, b));
} else if (d === "polygon") {
const { path: _, style: x, label: C } = a, b = {
id: M,
name: y,
path: _,
style: x,
label: C,
visible: p,
data: h
};
v = await ((g = e.addPolygon) == null ? void 0 : g.call(e, b));
}
m.bindGeometry(a, v), v.addListener((_, x) => {
f.$emit(`${_}-${d}`, x);
}), await oe();
}
}
function E(t) {
var i, n;
const e = Array.isArray(t) ? t : [t];
for (const r of e) {
const o = m.getGeometry(r);
o && ((n = (i = o.geometry).closeInfoWindow) == null || n.call(i)), m.remove(r);
}
}
function ne(t) {
const e = m.filterGeometry((i) => !J.includes(i.id) && i.type === t).map((i) => i.id);
for (const i of e)
m.remove(i);
}
function re() {
const t = m.filterGeometry((e) => !J.includes(e.id)).map((e) => e.id);
for (const e of t)
m.remove(e);
}
function A(t) {
return typeof t == "function" ? m.filterGeometry(t) : m.getGeometry(t);
}
function se(t) {
return m.filterGeometry((e) => e.type === t);
}
function B() {
return m.filterGeometry((t) => !0);
}
function ae(t, e) {
const i = m.getGeometry(t);
i && (i.name = e, i.geometry.setName(e));
}
function ce(t, e) {
const i = m.getGeometry(t);
i && (i.visible = e, e && i.geometry.show(), !e && i.geometry.hidden());
}
function ge(t, e, i = "deep") {
const n = m.getGeometry(t);
if (n) {
const r = ((o, c, s = "deep") => {
const g = {
id: t,
type: n.type
};
if (s) {
if (s === "deep")
return D(g, H(o, c));
if (s === !0)
return D(g, Object.assign(o, c));
} else return D(g, c);
})(n.data ?? {}, e, i);
n.data = r, n.geometry.setData(r);
}
}
function fe(t) {
const e = m.getGeometry(t);
return e == null ? void 0 : e.data;
}
function le(t) {
var i, n;
const e = G();
if (t != null && t.length) {
const r = t.map((o) => {
var c, s;
return (s = (c = m.getGeometry(o)) == null ? void 0 : c.geometry) == null ? void 0 : s.id;
}).filter((o) => o !== void 0);
(i = e == null ? void 0 : e.setFitView) == null || i.call(e, r);
} else {
const r = B().map((o) => {
var c;
return (c = o.geometry) == null ? void 0 : c.id;
}).filter((o) => o !== void 0);
(n = e == null ? void 0 : e.setFitView) == null || n.call(e, r);
}
}
function de(t, e) {
const i = m.getGeometry(t);
i && i.geometry.setPosition(e);
}
function me(t, e) {
const i = m.getGeometry(t);
i && i.geometry.setIcon(e);
}
function ye(t, e) {
var n, r;
const i = m.getGeometry(t);
i && ((r = (n = i.geometry).bounce) == null || r.call(n, e));
}
function ue(t, e) {
var n, r;
const i = m.getGeometry(t);
i && ((r = (n = i.geometry).flash) == null || r.call(n, e));
}
function _e(t, e) {
var n, r;
const i = m.getGeometry(t);
i && ((r = (n = i.geometry).openInfoWindow) == null || r.call(n, e));
}
function he(t) {
var i, n;
const e = m.getGeometry(t);
e && ((n = (i = e.geometry).closeInfoWindow) == null || n.call(i));
}
function pe(t, e) {
const i = m.getGeometry(t);
i && i.geometry.setPath(e);
}
function we(t, e) {
const i = m.getGeometry(t);
i && i.geometry && i.geometry.setStyle(e);
}
function Ge(t) {
var c, s, g, a, l, d, y, p, h, M, v, _, x, C, b, T, Y, X, F, L, N, O, R, U, Z, V, K;
const e = m.getGeometry(t), i = G(), n = w.getMapConfig(), r = ["longitude", "latitude", "height", "heading", "pitch", "roll"], o = ["longitude", "latitude"];
n != null && n.is_3d_map ? r.every((z) => {
var j, I;
return z in ((I = (j = e == null ? void 0 : e.data) == null ? void 0 : j.properties) == null ? void 0 : I.camera_info);
}) ? i.panTo(
[
(g = (s = (c = e == null ? void 0 : e.data) == null ? void 0 : c.properties) == null ? void 0 : s.camera_info) == null ? void 0 : g.longitude,
(d = (l = (a = e == null ? void 0 : e.data) == null ? void 0 : a.properties) == null ? void 0 : l.camera_info) == null ? void 0 : d.latitude,
(h = (p = (y = e == null ? void 0 : e.data) == null ? void 0 : y.properties) == null ? void 0 : p.camera_info) == null ? void 0 : h.height
],
{
heading: (_ = (v = (M = e == null ? void 0 : e.data) == null ? void 0 : M.properties) == null ? void 0 : v.camera_info) == null ? void 0 : _.heading,
pitch: (b = (C = (x = e == null ? void 0 : e.data) == null ? void 0 : x.properties) == null ? void 0 : C.camera_info) == null ? void 0 : b.pitch,
roll: (X = (Y = (T = e == null ? void 0 : e.data) == null ? void 0 : T.properties) == null ? void 0 : Y.camera_info) == null ? void 0 : X.roll
}
) : e && e.geometry.panTo() : (o.every((z) => {
var j, I;
return z in ((I = (j = e == null ? void 0 : e.data) == null ? void 0 : j.properties) == null ? void 0 : I.camera_info);
}) ? i.panTo([
(N = (L = (F = e == null ? void 0 : e.data) == null ? void 0 : F.properties) == null ? void 0 : L.camera_info) == null ? void 0 : N.longitude,
(U = (R = (O = e == null ? void 0 : e.data) == null ? void 0 : O.properties) == null ? void 0 : R.camera_info) == null ? void 0 : U.latitude
]) : e == null || e.geometry.panTo(), (K = (V = (Z = e == null ? void 0 : e.data) == null ? void 0 : Z.properties) == null ? void 0 : V.camera_info) != null && K.zoom && i.setZoom(e.data.properties.camera_info.zoom));
}
function Me(t) {
return w.setDomain(t);
}
async function ve(t) {
const e = Array.isArray(t) ? t : [t];
await w.addTag(e);
}
async function xe(t) {
const e = Array.isArray(t) ? t : [t];
await w.removeTag(e);
}
function $e(t, e, i) {
var c, s, g, a, l, d;
const n = m.getGeometry(t), o = (((s = (c = n == null ? void 0 : n.data) == null ? void 0 : c.properties) == null ? void 0 : s.styles) ?? []).find((y) => i ? y.tag_id === e && y.field_key === i : y.tag_id === e && y.is_default);
o ? (typeof ((a = (g = o == null ? void 0 : o.style_info) == null ? void 0 : g.icon) == null ? void 0 : a.image) == "string" && !o.style_info.icon.image.startsWith("http") && (o.style_info.icon.image = (u.staticUrl ?? "") + o.style_info.icon.image), (d = (l = n == null ? void 0 : n.geometry) == null ? void 0 : l.setStyle) == null || d.call(l, o == null ? void 0 : o.style_info)) : console.warn(`未找到ID"${t}"对应Tag"${e}"的样式`);
}
async function Se(t, e) {
const i = await w.fetchSource({
external_ids: t,
tag_ids: e == null ? void 0 : e.tag_ids,
type_ids: e == null ? void 0 : e.type_ids,
scope: e == null ? void 0 : e.scope,
zoom_level: e == null ? void 0 : e.zoom_level
}), n = [];
for (let r = 0; r < i.length; r++) {
const o = i[r], c = w.genResourceGeometryOptions(o, {
$isExternal: !0
});
n.push(c);
}
await k(n);
}
function Ce(t) {
const e = A((i) => {
var n, r, o;
return (n = i.data) != null && n.$isExternal ? t.includes((o = (r = i.data) == null ? void 0 : r.properties) == null ? void 0 : o.external_id) : !1;
}).map((i) => i.id);
E(e);
}
async function be(t) {
var c, s, g, a, l, d, y, p, h, M, v;
if (typeof t != "object" || t === null) {
console.warn("addWithTag 参数错误");
return;
}
if (!t.resource_id && !t.external_id) {
console.warn("addWithTag 参数错误,resource_id 和 external_id 不能同时为空");
return;
}
const e = await q.mapManage.tag.get({
tag_id: t.tag_id
}), i = (((c = e == null ? void 0 : e.data) == null ? void 0 : c.styles) ?? []).find((_) => t.styleKey ? _.field_key === t.styleKey : _.is_default), n = G();
n.coordinateSystem !== t.coordinate_system && je.transform(
t.geometry,
t.coordinate_system,
n.coordinateSystem
);
const r = t.resource_id ?? `external_${t.external_id}`, o = {
id: r,
name: t.name,
data: {
properties: {
resource_id: t.resource_id,
resource_name: t.name,
external_id: t.external_id,
tags: [
{ tag_id: t.tag_id, tag_name: (s = e == null ? void 0 : e.data) == null ? void 0 : s.name }
],
styles: (((g = e == null ? void 0 : e.data) == null ? void 0 : g.styles) ?? []).map((_) => ({
..._,
tag_id: t.tag_id
}))
},
...t.data,
$tags: [t.tag_id],
$isExternal: !!t.external_id
},
style: i == null ? void 0 : i.style_info,
label: {
text: t.name,
minShowZoom: (l = (a = i == null ? void 0 : i.style_info) == null ? void 0 : a.label) == null ? void 0 : l.minShowZoom,
visible: ((y = (d = i == null ? void 0 : i.style_info) == null ? void 0 : d.label) == null ? void 0 : y.visible) ?? ((p = w.projectConfig) == null ? void 0 : p.is_show_label) ?? !0
}
};
return t.geometry.type === "Point" ? (o.type = "marker", o.data.$isAgg = !1, o.position = t.geometry.coordinates, (v = (M = (h = i == null ? void 0 : i.style_info) == null ? void 0 : h.icon) == null ? void 0 : M.image) != null && v.startsWith("http") || (o.style.icon.image = (u.staticUrl ?? "") + i.style_info.icon.image)) : t.geometry.type === "Polygon" ? (o.type = "polygon", o.path = t.geometry.coordinates[0], o.holes = t.geometry.coordinates.slice(1)) : t.geometry.type === "LineString" && (o.type = "polyline", o.path = t.geometry.coordinates), await k(o), r;
}
return {
mapServer: {
fetchResource: w.fetchSource.bind(w),
projectConfig: w.projectConfig
},
setProjectConfig: Q,
setMapConfig: ee,
exposes: {
// 资源方法
add: k,
remove: E,
removeByType: ne,
removeAll: re,
get: A,
getByType: se,
getAll: B,
setName: ae,
setVisible: ce,
setData: ge,
getData: fe,
setFitView: le,
setStyle: we,
panToSource: Ge,
// Marker 方法
setPosition: de,
setIcon: me,
bounce: ye,
flash: ue,
openInfoWindow: _e,
closeInfoWindow: he,
// Polyline/Polygon 方法
setPath: pe,
// 地图管理平台-资源方法
setDomain: Me,
addByTag: ve,
removeByTag: xe,
setStyleByTag: $e,
addByExternalIds: Se,
removeByExternalIds: Ce,
addWithTag: be
}
};
}
export {
Ye as useGeometrySourceHook
};