@tuoyuan/map-adapter-tmap
Version:
天地图适配器
2 lines (1 loc) • 4.98 kB
JavaScript
;var E=Object.defineProperty;var v=(i,o,t)=>o in i?E(i,o,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[o]=t;var m=(i,o,t)=>v(i,typeof o!="symbol"?o+"":o,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("@tuoyuan/map-adapter-lib"),_=require("./Marker.js"),y=require("./Polyline.js"),A=require("./Polygon.js"),p={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 R extends n.BaseMap{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 n.loadScript({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 r=(c=p[e.layer??"NORMAL"])==null?void 0:c.call(p);return this.mapInstance.setMapType(r),this.initEvent(),Promise.resolve(s)}addMarker(t){const e=new _.TMapMarker({_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 y.TMapPolyline({_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 A.TMapPolygon({_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.Position(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.Position(t.lng,t.lat))}getSouthWest(){const t=this.mapInstance.getBounds().getSouthWest();return Promise.resolve(new n.Position(t.lng,t.lat))}getNorthWest(){const t=this.mapInstance.getBounds().getNorthEast(),e=this.mapInstance.getBounds().getSouthWest();return Promise.resolve(new n.Position(t.lng,e.lat))}getSouthEast(){const t=this.mapInstance.getBounds().getNorthEast(),e=this.mapInstance.getBounds().getSouthWest();return Promise.resolve(new n.Position(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.Position(e.lng,t.lat),NorthWest:new n.Position(t.lng,t.lat),SouthEast:new n.Position(e.lng,e.lat),SouthWest:new n.Position(t.lng,e.lat)})}getNavigationPlan(t,e,s,a){if(s!==n.ERouteType.DRIVING)throw new Error("[TMap.getNavigationPlan] 目前仅支持驾车导航");return new Promise(r=>{const c=this.TMap.LngLat(t.longitude,t.latitude),u=this.TMap.LngLat(e.longitude,e.latitude),P={policy:new Map([[n.ERoutePolicy.LEAST_TIME,0],[n.ERoutePolicy.LEAST_DISTANCE,1],[n.ERoutePolicy.AVOID_HIGHWAYS,2]]).get(a),onSearchComplete:I},g=[];function I(w){const h=w.getPlan(),{duration:M,distance:T}=h.result;h.getPath().forEach(l=>{g.push(new n.Position(l.lng,l.lat))});const d={time:M,distance:T,instruction:h.routes.item.map(l=>l.strguide).join(""),path:g};return r(d)}new this.TMap.DrivingRoute(this.mapInstance,P).search(c,u)})}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.Position(t.lnglat.lng,t.lnglat.lat),pixel:new n.Pixel(t.containerPoint.x,t.containerPoint.y)})}),this.mapInstance.on("dblclick",t=>{this.emit("dblclick",{e:t.originEvent,position:new n.Position(t.lnglat.lng,t.lnglat.lat),pixel:new n.Pixel(t.containerPoint.x,t.containerPoint.y)})}),this.mapInstance.on("rightclick",t=>{this.emit("rightclick",{e:t.originEvent,position:new n.Position(t.lnglat.lng,t.lnglat.lat),pixel:new n.Pixel(t.containerPoint.x,t.containerPoint.y)})}),this.mapInstance.on("move",t=>{const e=this.mapInstance.getCenter();this.emit("move",{center:new n.Position(e.lng,e.lat)})}),this.mapInstance.on("rotate",t=>{this._rotate=this.mapInstance.getRotation(),this.emit("rotate",this._rotate)})}getDistanceFromPixels(t,e){const r=4007501668557849e-8/(256*Math.pow(2,e??1));return Promise.resolve(r*t)}}exports.TMap=R;