@tuoyuan/map-adapter-tmap
Version:
天地图适配器
11 lines (10 loc) • 4.24 kB
JavaScript
"use strict";var x=Object.defineProperty;var w=(o,i,t)=>i in o?x(o,i,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[i]=t;var a=(o,i,t)=>w(o,typeof i!="symbol"?i+"":i,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("@tuoyuan/map-adapter-lib");class v extends l.BasePolygon{constructor(t,e){super(t._top,e,{labelBasePlacement:"right-bottom"});a(this,"_ctx");a(this,"polygonData");a(this,"currentLngLat");a(this,"polygonInstance");a(this,"labelInstance");this._ctx=t,this.polygonData=e,this.renderPolygon(),this.renderLabel(),this.addListener((n,s)=>{this._ctx._top.options.dev&&(console.groupCollapsed("[Polygon.event]",n),console.log("payload: ",s),console.groupEnd())})}initEvent(){["click","dblclick","rightclick"].forEach(e=>{this.polygonInstance.on(e,n=>{this.emit(e,{e:n.originalEvent,position:new l.Position(n.lnglat.lng,n.lnglat.lat),pixel:new l.Pixel(n.containerPoint.x,n.containerPoint.y),data:this.data})})})}renderPolygon(){var e,n,s,p,h,c,r;this.polygonInstance&&this._ctx._map.removeOverLay(this.polygonInstance);const t=[];(e=this._path)==null||e.map(d=>{t.push(this._ctx.TMap.LngLat(d.longitude,d.latitude))}),this.polygonInstance=new this._ctx.TMap.Polygon(t,{lineStyle:(n=this._style.line)==null?void 0:n.style,color:(s=this._style.line)==null?void 0:s.color,weight:(p=this._style.line)==null?void 0:p.width,opacity:(h=this._style.line)==null?void 0:h.opacity,fillColor:(c=this._style.fill)==null?void 0:c.color,fillOpacity:(r=this._style.fill)==null?void 0:r.opacity}),this._ctx._map.addOverLay(this.polygonInstance),this.initEvent()}renderLabel(){var h,c,r,d,g,y,b,_,I,f,u;if(this.labelInstance&&this._ctx._map.removeOverLay(this.labelInstance),!((h=this._label)!=null&&h.text))return;const t=l.getDPI(),e=(c=this.polygonInstance.getBounds())==null?void 0:c.getCenter(),n=(y=(g=(d=(r=this._label).getRealOffset)==null?void 0:d.call(r,{padding:[2,10,2,10]}))==null?void 0:g.toArray)==null?void 0:y.call(g);console.log(this._style,"this._label");const s=new this._ctx.TMap.Point(n[0]-12,n[1]+15);console.log(this._label,"this._label");const p={text:`<div style="line-height: 1.2; padding: 2px 0;">${((b=this._label)==null?void 0:b.text)||""}</div>`,position:new this._ctx.TMap.LngLat(e.lng,e.lat),offset:s};this.labelInstance=new this._ctx.TMap.Label(p),this.labelInstance.setBorderLine(0),this.labelInstance.setFontColor((_=this._style.label)==null?void 0:_.color),this.labelInstance.setFontSize(l.pxToPt((I=this._style.label)==null?void 0:I.fontSize,t)),this.labelInstance.setBackgroundColor((f=this._style.label)==null?void 0:f.backgroundColor),((u=this._label)==null?void 0:u.visible)===!1&&this.labelInstance.hide(),this._ctx._map.addOverLay(this.labelInstance)}setFontSize(t){var e;(e=this.labelInstance)==null||e.setFontSize(l.pxToPt(t,l.getDPI()))}setBackgroundColor(t){var e;(e=this.labelInstance)==null||e.setBackgroundColor(t)}setPath(t){var n;super.setPath(t);const e=[];(n=this.path)==null||n.map(s=>{e.push(this._ctx.TMap.LngLat(s.longitude,s.latitude))}),this.polygonInstance.setLngLats(e)}setStyle(t){super.setStyle(t),this.renderPolygon(),this.renderLabel()}openInfoWindow(t){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"
scrolling="auto">
</iframe>
</div>`:e=`<div style="width:${t.size.width}px; height:${t.size.height}px; overflow: hidden; pointer-events: none;">
${t.html}
</div>`;const n=new this._ctx.TMap.InfoWindow;n.setContent(e),this.polygonInstance.openInfoWindow(n)}closeInfoWindow(){this.polygonInstance.closeInfoWindow()}hidden(){super.hidden(),this.polygonInstance.setOpacity(0)}show(){super.show(),this.polygonInstance.setOpacity(1)}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.polygonInstance,this.labelInstance].filter(e=>!!e)}remove(){this.closeInfoWindow(),this.getInstances().forEach(t=>this._ctx._map.removeOverLay(t))}}exports.TMapPolygon=v;