@tuoyuan/map-adapter-amap
Version:
高德地图适配器
6 lines (5 loc) • 6.17 kB
JavaScript
;var x=Object.defineProperty;var S=(h,r,t)=>r in h?x(h,r,{enumerable:!0,configurable:!0,writable:!0,value:t}):h[r]=t;var E=(h,r,t)=>S(h,typeof r!="symbol"?r+"":r,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@tuoyuan/map-adapter-lib"),T=require("./Marker.js"),C=require("./Polyline.js"),L=require("./Polygon.js");class R extends o.BaseMap{constructor(){super();E(this,"AMap");E(this,"mapInstance");E(this,"coordinateSystem","GCJ02")}load(t,e){return new Promise(async(a,c)=>{try{if(typeof t.secret=="string"&&t.secret!==""){const l=`
window._AMapSecurityConfig = {
securityJsCode: '${t.secret}'
}
`;await o.loadScript({content:l})}const n=`https://webapi.amap.com/maps?v=2.0&key=${t.key}&plugin=AMap.Driving`;if(await o.loadScript({url:n}),e!=null&&e.loca){const l=`https://webapi.amap.com/loca?v=2.0.0&key=${t.key}`;await o.loadScript({url:l})}this.AMap=window.AMap}catch(n){c(n)}a()})}init(t,e){var c,n,l,d,y,P,w,m,I,M,p,g,v;this._dom=t.dom,t.zoom&&(this._zoom=t.zoom);const a=new this.AMap.Map(t.dom,{zoom:t.zoom,center:t.position});if(e!=null&&e.isRoadNet){const s=new this.AMap.TileLayer.RoadNet;a.addLayer(s)}if((e==null?void 0:e.layer)==="satellite"){const s=new this.AMap.TileLayer.Satellite;a.addLayer(s)}if((n=(c=t.boundary)==null?void 0:c.path)!=null&&n.length){const s=t.boundary,i=o.deepAssign({},(y=(d=(l=this._ctx)==null?void 0:l.options)==null?void 0:d.style)==null?void 0:y.polygon,s.style),A=new this.AMap.Polygon({path:s.path,strokeColor:(P=i.line)==null?void 0:P.color,strokeWeight:(w=i.line)==null?void 0:w.width,fillColor:(m=i.fill)==null?void 0:m.color,fillOpacity:(I=i.fill)==null?void 0:I.opacity});if((M=e==null?void 0:e.beyondTheBoundary)!=null&&M.hide){const u=new this.AMap.Polygon({path:[[[-360,90],[-360,-90],[360,-90],[360,90]],s.path],strokeWeight:0,fillColor:((p=e.beyondTheBoundary)==null?void 0:p.color)??"#000",fillOpacity:((g=e.beyondTheBoundary)==null?void 0:g.opacity)??1});a.add(u)}(v=e==null?void 0:e.beyondTheBoundary)!=null&&v.limit&&a.setLimitBounds(A.getBounds()),a.add(A)}return this.mapInstance=a,this.initEvent(),Promise.resolve(a)}initEvent(){this.mapInstance.on("complete",()=>{this._rotate=this.mapInstance.getRotation(),this._zoom=this.mapInstance.getZoom(),this.emit("complete")}),this.mapInstance.on("zoomchange",()=>{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 o.Position(t.lnglat.lng,t.lnglat.lat),pixel:new o.Pixel(t.pixel.x,t.pixel.y),pos:new o.Pixel(t.pos[0],t.pos[1])})}),this.mapInstance.on("dblclick",t=>{this.emit("dblclick",{e:t.originEvent,position:new o.Position(t.lnglat.lng,t.lnglat.lat),pixel:new o.Pixel(t.pixel.x,t.pixel.y),pos:new o.Pixel(t.pos[0],t.pos[1])})}),this.mapInstance.on("rightclick",t=>{this.emit("rightclick",{e:t.originEvent,position:new o.Position(t.lnglat.lng,t.lnglat.lat),pixel:new o.Pixel(t.pixel.x,t.pixel.y),pos:new o.Pixel(t.pos[0],t.pos[1])})}),this.mapInstance.on("mapmove",t=>{const e=this.mapInstance.getCenter();this.emit("move",{center:new o.Position(e.lng,e.lat),pos:new o.Pixel(e.pos[0],e.pos[1])})}),this.mapInstance.on("rotatechange",t=>{this._rotate=this.mapInstance.getRotation(),this.emit("rotate",this._rotate)})}addMarker(t){const e=new T.AMapMarker({_map:this.mapInstance,_top:this._ctx,AMap:this.AMap},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 C.AMapPolyline({_map:this.mapInstance,_top:this._ctx,AMap:this.AMap},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 L.AMapPolygon({_map:this.mapInstance,_top:this._ctx,AMap:this.AMap},t);return Promise.resolve(e)}removePolygon(t){return Array.isArray(t)?t.forEach(e=>e.remove()):t.remove(),Promise.resolve()}panTo(t,e){const a=new o.Position(...t);this.mapInstance.panTo(a.toArray())}setCenter(t){this.mapInstance.setCenter(t)}getCenter(){const t=this.mapInstance.getCenter();return Promise.resolve(new o.Position(t.lng,t.lat))}setZoom(t){this.mapInstance.setZoom(t)}getNorthEast(){const t=this.mapInstance.getBounds().getNorthWest(),e=this.mapInstance.getBounds().getSouthEast();return Promise.resolve(new o.Position(e.lng,t.lat))}getNorthWest(){const t=this.mapInstance.getBounds().getNorthWest();return Promise.resolve(new o.Position(t.lng,t.lat))}getSouthEast(){const t=this.mapInstance.getBounds().getSouthEast();return Promise.resolve(new o.Position(t.lng,t.lat))}getSouthWest(){const t=this.mapInstance.getBounds().getNorthWest(),e=this.mapInstance.getBounds().getSouthEast();return Promise.resolve(new o.Position(t.lng,e.lat))}getViewBounds(){const t=this.mapInstance.getBounds().getNorthWest(),e=this.mapInstance.getBounds().getSouthEast();return Promise.resolve({NorthEast:new o.Position(e.lng,t.lat),NorthWest:new o.Position(t.lng,t.lat),SouthEast:new o.Position(e.lng,e.lat),SouthWest:new o.Position(t.lng,e.lat)})}getNavigationPlan(t,e,a,c,n){if(a!==o.ERouteType.DRIVING)throw new Error("[AMap.getNavigationPlan] 目前仅支持驾车导航");return new Promise((l,d)=>{var p,g;const y=new this.AMap.LngLat(t.longitude,t.latitude),P=new this.AMap.LngLat(e.longitude,e.latitude),w=new Map([[o.ERoutePolicy.LEAST_TIME,0],[o.ERoutePolicy.LEAST_DISTANCE,2],[o.ERoutePolicy.AVOID_HIGHWAYS,1]]),m=[];function I(v,s){if(v==="complete"){const i=s.routes[0];i.steps.forEach(u=>{u.path.forEach(_=>{m.push(new o.Position(_.lng,_.lat))})});const A={time:i.time,distance:i.distance,instruction:i.steps.map(u=>u.instruction).join(""),path:m};return l(A)}else return d("获取驾车数据失败:"+s)}new this.AMap.Driving({policy:w.get(c),panel:(p=n==null?void 0:n.panel)!=null&&p.isShow?(g=n==null?void 0:n.panel)==null?void 0:g.panelDom:void 0}).search(y,P,I)})}destroy(){this.mapInstance.destroy()}setFitView(t){return this.mapInstance.setFitView(t),Promise.resolve()}getDistanceFromPixels(t,e){const n=4007501668557849e-8/(256*Math.pow(2,e??1));return Promise.resolve(t*n)}}exports.AMap=R;