@tuoyuan/map-adapter-tmap
Version:
天地图适配器
214 lines (213 loc) • 6.54 kB
JavaScript
var _ = Object.defineProperty;
var A = (r, o, t) => o in r ? _(r, o, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[o] = t;
var m = (r, o, t) => A(r, typeof o != "symbol" ? o + "" : o, t);
import { BaseMap as y, loadScript as L, Position as n, ERouteType as R, ERoutePolicy as p, Pixel as g } from "@tuoyuan/map-adapter-lib";
import { TMapMarker as N } from "./Marker.js";
import { TMapPolyline as S } from "./Polyline.js";
import { TMapPolygon as f } from "./Polygon.js";
const u = {
NORMAL: () => window.TMAP_NORMAL_MAP,
// 普通街道视图
SATELLITE: () => window.TMAP_SATELLITE_MAP,
// 卫星视图
HYBRID: () => window.TMAP_HYBRID_MAP,
// 卫星和路网的混合视图
TERRAIN: () => window.TMAP_TERRAIN_MAP,
// 地形视图
TERRAIN_HYBRID: () => window.TMAP_TERRAIN_HYBRID_MAP
// 地形和路网的混合视图
};
class H extends y {
constructor() {
super();
m(this, "TMap");
m(this, "mapInstance");
m(this, "coordinateSystem", "WGS84");
}
load(t) {
return new Promise(async (e, s) => {
try {
const a = `https://api.tianditu.gov.cn/api?v=4.0&tk=${t.key}`;
await L({ url: a }), this.TMap = window.T;
} catch (a) {
s(a);
}
e();
});
}
init(t, e) {
var c;
this._dom = t.dom;
const s = new this.TMap.Map(t.dom, {
zoom: t.zoom,
center: t.position
}), a = this.TMap.LngLat(t.position[0], t.position[1]);
s.centerAndZoom(a, t.zoom), this.mapInstance = s, t.zoom && (this._zoom = t.zoom);
const i = (c = u[e.layer ?? "NORMAL"]) == null ? void 0 : c.call(u);
return this.mapInstance.setMapType(i), this.initEvent(), Promise.resolve(s);
}
addMarker(t) {
const e = new N(
{
_map: this.mapInstance,
_top: this._ctx,
TMap: this.TMap
},
t
);
return Promise.resolve(e);
}
removeMarker(t) {
return Array.isArray(t) ? t.forEach((e) => e.remove()) : t.remove(), Promise.resolve();
}
addPolyline(t) {
const e = new S(
{
_map: this.mapInstance,
_top: this._ctx,
TMap: this.TMap
},
t
);
return Promise.resolve(e);
}
removePolyline(t) {
return Array.isArray(t) ? t.forEach((e) => e.remove()) : t.remove(), Promise.resolve();
}
addPolygon(t) {
const e = new f(
{
_map: this.mapInstance,
_top: this._ctx,
TMap: this.TMap
},
t
);
return Promise.resolve(e);
}
removePolygon(t) {
return Array.isArray(t) ? t.forEach((e) => e.remove()) : t.remove(), Promise.resolve();
}
setCenter(t) {
const e = this.TMap.LngLat(t[0], t[1]);
this.mapInstance.centerAndZoom(e);
}
getCenter() {
const t = this.mapInstance.getCenter();
return Promise.resolve(new n(t.lng, t.lat));
}
setZoom(t) {
this.mapInstance.setZoom(t);
}
getZoom() {
return this.mapInstance.getZoom();
}
setLayer(t) {
this.mapInstance.setLayer(t);
}
getLayer() {
return this.mapInstance.getLayer();
}
panTo(t) {
const e = this.TMap.LngLat(t[0], t[1]);
this.mapInstance.panTo(e);
}
getNorthEast() {
const t = this.mapInstance.getBounds().getNorthEast();
return Promise.resolve(new n(t.lng, t.lat));
}
getSouthWest() {
const t = this.mapInstance.getBounds().getSouthWest();
return Promise.resolve(new n(t.lng, t.lat));
}
getNorthWest() {
const t = this.mapInstance.getBounds().getNorthEast(), e = this.mapInstance.getBounds().getSouthWest();
return Promise.resolve(new n(t.lng, e.lat));
}
getSouthEast() {
const t = this.mapInstance.getBounds().getNorthEast(), e = this.mapInstance.getBounds().getSouthWest();
return Promise.resolve(new n(e.lng, t.lat));
}
setFitView(t) {
return this.mapInstance.setViewport(t), Promise.resolve();
}
getViewBounds() {
const t = this.mapInstance.getBounds().getNorthWest(), e = this.mapInstance.getBounds().getSouthEast();
return Promise.resolve({
NorthEast: new n(e.lng, t.lat),
NorthWest: new n(t.lng, t.lat),
SouthEast: new n(e.lng, e.lat),
SouthWest: new n(t.lng, e.lat)
});
}
getNavigationPlan(t, e, s, a) {
if (s !== R.DRIVING)
throw new Error("[TMap.getNavigationPlan] 目前仅支持驾车导航");
return new Promise((i) => {
const c = this.TMap.LngLat(t.longitude, t.latitude), w = this.TMap.LngLat(e.longitude, e.latitude), P = {
policy: (/* @__PURE__ */ new Map([
[p.LEAST_TIME, 0],
[p.LEAST_DISTANCE, 1],
[p.AVOID_HIGHWAYS, 2]
])).get(a),
// 策略
onSearchComplete: T
//检索完成后的回调函数
}, I = [];
function T(M) {
const h = M.getPlan(), { duration: E, distance: d } = h.result;
h.getPath().forEach((l) => {
I.push(new n(l.lng, l.lat));
});
const v = {
time: E,
distance: d,
instruction: h.routes.item.map((l) => l.strguide).join(""),
path: I
};
return i(v);
}
new this.TMap.DrivingRoute(this.mapInstance, P).search(c, w);
});
}
initEvent() {
this.mapInstance.on("complete", () => {
this.emit("complete");
}), this.mapInstance.on("zoom", () => {
const t = this.mapInstance.getZoom();
this._zoom = t, this.emit("zoom", t);
}), this.mapInstance.on("click", (t) => {
this.emit("click", {
e: t.originEvent,
position: new n(t.lnglat.lng, t.lnglat.lat),
pixel: new g(t.containerPoint.x, t.containerPoint.y)
});
}), this.mapInstance.on("dblclick", (t) => {
this.emit("dblclick", {
e: t.originEvent,
position: new n(t.lnglat.lng, t.lnglat.lat),
pixel: new g(t.containerPoint.x, t.containerPoint.y)
});
}), this.mapInstance.on("rightclick", (t) => {
this.emit("rightclick", {
e: t.originEvent,
position: new n(t.lnglat.lng, t.lnglat.lat),
pixel: new g(t.containerPoint.x, t.containerPoint.y)
});
}), this.mapInstance.on("move", (t) => {
const e = this.mapInstance.getCenter();
this.emit("move", {
center: new n(e.lng, e.lat)
});
}), this.mapInstance.on("rotate", (t) => {
this._rotate = this.mapInstance.getRotation(), this.emit("rotate", this._rotate);
});
}
getDistanceFromPixels(t, e) {
const i = 4007501668557849e-8 / (256 * Math.pow(2, e ?? 1));
return Promise.resolve(i * t);
}
}
export {
H as TMap
};