@tuoyuan/map-adapter-amap
Version:
高德地图适配器
10 lines (9 loc) • 4.34 kB
JavaScript
"use strict";var m=Object.defineProperty;var _=(h,l,t)=>l in h?m(h,l,{enumerable:!0,configurable:!0,writable:!0,value:t}):h[l]=t;var c=(h,l,t)=>_(h,typeof l!="symbol"?l+"":l,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("@tuoyuan/map-adapter-lib");class u extends a.BasePolyline{constructor(t,e){super(t._top,e,{});c(this,"_ctx");c(this,"polylineInstance");c(this,"labelInstance");c(this,"infoWindowInstance");this._ctx=t,this.renderPolyline(),this.renderLabel(),this.addListener((n,i)=>{this._ctx._top.options.dev&&(console.groupCollapsed("[Polyline.event]",n),console.log("payload: ",i),console.groupEnd())})}initEvent(){["click","dblclick","rightclick"].forEach(e=>{this.polylineInstance.on(e,n=>{this.emit(e,{e:n.originEvent,position:new a.Position(n.lnglat.lng,n.lnglat.lat),pixel:new a.Pixel(n.pixel.x,n.pixel.y),pos:new a.Pixel(n.pos[0],n.pos[1]),data:this.data})})})}renderPolyline(){var e,n,i,s;this.polylineInstance&&this.polylineInstance.remove();const t={path:this._path.map(o=>o.toArray()),strokeColor:(e=this._style.line)==null?void 0:e.color,strokeOpacity:(n=this._style.line)==null?void 0:n.opacity,strokeWeight:(i=this._style.line)==null?void 0:i.width,strokeStyle:(s=this._style.line)==null?void 0:s.style};this.polylineInstance=new this._ctx.AMap.Polyline(t),this._ctx._map.add(this.polylineInstance),this.initEvent()}renderLabel(){var n,i,s,o,r,p,d,y,b,f,g;if(this.labelInstance&&this.labelInstance.remove(),!((n=this._label)!=null&&n.text))return;const t=super.findClosestPointToCentroid(this._path),e={name:this._name,position:t==null?void 0:t.toArray(),text:(i=this._label)==null?void 0:i.text,visible:(s=this._label)==null?void 0:s.visible,anchor:"bottom-center",offset:((d=(p=(r=(o=this._label)==null?void 0:o.getRealOffset)==null?void 0:r.call(o,{padding:[2,3,2,3]}))==null?void 0:p.toArray)==null?void 0:d.call(p))??[0,(y=this._icon)==null?void 0:y.getRealOffset().y],style:{border:"none","background-color":(b=this._label)==null?void 0:b.backgroundColor,"font-size":((f=this._label)==null?void 0:f.fontSize)+"px",color:(g=this._label)==null?void 0:g.color}};this.labelInstance=new this._ctx.AMap.Text(e),this._ctx._map.add(this.labelInstance)}setPath(t){super.setPath(t),this.polylineInstance.setPath(this._path.map(e=>e.toArray()))}setStyle(t){var n,i,s,o;super.setStyle(t);const e={strokeColor:(n=this._style.line)==null?void 0:n.color,strokeOpacity:(i=this._style.line)==null?void 0:i.opacity,strokeWeight:(s=this._style.line)==null?void 0:s.width,strokeStyle:(o=this._style.line)==null?void 0:o.style};this.polylineInstance.setOptions(e)}hidden(){super.hidden(),this.polylineInstance.hide()}show(){super.show(),this.polylineInstance.show()}showLabel(){var t,e;super.showLabel(),(e=(t=this.labelInstance)==null?void 0:t.show)==null||e.call(t)}hiddenLabel(){var t,e;super.hiddenLabel(),(e=(t=this.labelInstance)==null?void 0:t.hide)==null||e.call(t)}getInstances(){return[this.polylineInstance,this.labelInstance].filter(e=>!!e)}remove(){this.closeInfoWindow(),this.getInstances().forEach(t=>t.remove())}panTo(){this._ctx._map.setFitView(this.getInstances())}getBounds(){const{northEast:t,southWest:e}=this.polylineInstance.getBounds();return a.Bounds.getBounds([new a.Position(t.lng,t.lat),new a.Position(e.lng,e.lat)])}openInfoWindow(t){this.closeInfoWindow();let e="";t.url?e=`<div style="width:${t.size.width}px; height:${t.size.height}px; padding:10px;">
<iframe src=${t.url}
width="100%"
height="100%"
frameborder="0">
</iframe>
</div>`:e=`<div style="width:${t.size.width}px; height:${t.size.height}px; overflow: hidden; pointer-events: auto;">
${t.html}
</div>`;const i={isCustom:!0,autoMove:!1,closeWhenClickMap:!1,content:e,anchor:{bottom:"top-center",top:"bottom-center",right:"middle-left",left:"middle-right","right-bottom":"top-left","right-top":"bottom-left","left-bottom":"top-right","left-top":"bottom-right"}[t.placement??"top"],position:a.Bounds.getCenter(this.getBounds()).toArray()},s=new this._ctx.AMap.InfoWindow(i);s.open(this._ctx._map),s.dom.style.height="0";const o=s.dom.querySelector(".amap-info-contentContainer");o&&(o.style.pointerEvents="none"),s.on("close",()=>{const r=s.dom;r.parentNode.removeChild(r)})}closeInfoWindow(){this._ctx._map.clearInfoWindow()}}exports.AMapPolyline=u;