UNPKG

@tuoyuan/map-adapter-amap

Version:

高德地图适配器

10 lines (9 loc) 4.6 kB
"use strict";var m=Object.defineProperty;var u=(c,a,t)=>a in c?m(c,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[a]=t;var p=(c,a,t)=>u(c,typeof a!="symbol"?a+"":a,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("@tuoyuan/map-adapter-lib");class w extends h.BasePolygon{constructor(t,e){super(t._top,e,{});p(this,"_ctx");p(this,"polygonInstance");p(this,"labelInstance");p(this,"infoWindowInstance");this._ctx=t,this.renderPolygon(),this.renderLabel(),this.addListener((o,i)=>{this._ctx._top.options.dev&&(console.groupCollapsed("[Polygon.event]",o),console.log("payload: ",i),console.groupEnd())})}initEvent(){["click","dblclick","rightclick"].forEach(e=>{this.polygonInstance.on(e,o=>{this.emit(e,{e:o.originEvent,position:new h.Position(o.lnglat.lng,o.lnglat.lat),pixel:new h.Pixel(o.pixel.x,o.pixel.y),pos:new h.Pixel(o.pos[0],o.pos[1]),data:this.data})})})}renderPolygon(){var e,o,i,n,l,s;this.polygonInstance&&this.polygonInstance.remove();const t={path:this._path.map(r=>r.toArray()),strokeColor:(e=this._style.line)==null?void 0:e.color,strokeOpacity:(o=this._style.line)==null?void 0:o.opacity,strokeWeight:(i=this._style.line)==null?void 0:i.width,fillColor:(n=this._style.fill)==null?void 0:n.color,fillOpacity:(l=this._style.fill)==null?void 0:l.opacity,strokeStyle:(s=this._style.line)==null?void 0:s.style};this.polygonInstance=new this._ctx.AMap.Polygon(t),this._ctx._map.add(this.polygonInstance),this.initEvent()}renderLabel(){var o,i,n,l,s,r,d,g,y,f,_,b;if(this.labelInstance&&this.labelInstance.remove(),!((o=this._label)!=null&&o.text))return;const t=(i=this.polygonInstance.getBounds())==null?void 0:i.getCenter(),e={name:this._name,position:[t.lng,t.lat],text:(n=this._label)==null?void 0:n.text,visible:(l=this._label)==null?void 0:l.visible,anchor:"bottom-center",offset:((g=(d=(r=(s=this._label)==null?void 0:s.getRealOffset)==null?void 0:r.call(s,{padding:[2,3,2,3]}))==null?void 0:d.toArray)==null?void 0:g.call(d))??[0,(y=this._icon)==null?void 0:y.getRealOffset().y],style:{border:"none","background-color":(f=this._label)==null?void 0:f.backgroundColor,"font-size":((_=this._label)==null?void 0:_.fontSize)+"px",color:(b=this._label)==null?void 0:b.color}};this.labelInstance=new this._ctx.AMap.Text(e),this._ctx._map.add(this.labelInstance)}setPath(t){super.setPath(t),this.polygonInstance.setPath(this._path.map(e=>e.toArray()))}setStyle(t){var o,i,n,l,s,r;super.setStyle(t);const e={strokeColor:(o=this._style.line)==null?void 0:o.color,strokeOpacity:(i=this._style.line)==null?void 0:i.opacity,strokeWeight:(n=this._style.line)==null?void 0:n.width,fillColor:(l=this._style.fill)==null?void 0:l.color,fillOpacity:(s=this._style.fill)==null?void 0:s.opacity,strokeStyle:(r=this._style.line)==null?void 0:r.style};this.polygonInstance.setOptions(e)}hidden(){super.hidden(),this.polygonInstance.hide()}show(){super.show(),this.polygonInstance.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.polygonInstance,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.polygonInstance.getBounds();return h.Bounds.getBounds([new h.Position(t.lng,t.lat),new h.Position(e.lng,e.lat)])}getArea(){return this.polygonInstance.getArea()}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:h.Bounds.getCenter(this.getBounds()).toArray()},n=new this._ctx.AMap.InfoWindow(i);n.open(this._ctx._map),n.dom.style.height="0";const l=n.dom.querySelector(".amap-info-contentContainer");l&&(l.style.pointerEvents="none"),n.on("close",()=>{const s=n.dom;s.parentNode.removeChild(s)})}closeInfoWindow(){this._ctx._map.clearInfoWindow()}}exports.AMapPolygon=w;