@tuoyuan/map-adapter-amap
Version:
高德地图适配器
10 lines (9 loc) • 4.02 kB
JavaScript
"use strict";var _=Object.defineProperty;var g=(l,o,t)=>o in l?_(l,o,{enumerable:!0,configurable:!0,writable:!0,value:t}):l[o]=t;var c=(l,o,t)=>g(l,typeof o!="symbol"?o+"":o,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("@tuoyuan/map-adapter-lib");class I extends h.BaseMarker{constructor(t,e){super(t._top,e,{basePlacement:"right-bottom",labelBasePlacement:"top"});c(this,"_ctx");c(this,"iconInstance");c(this,"labelInstance");this._ctx=t,this.renderIcon(),this.renderLabel(),this.addListener((i,s)=>{this._ctx._top.options.dev&&(console.groupCollapsed("[Marker.event]",i),console.log("payload: ",s),console.groupEnd())})}initEvent(){["click","dblclick","rightclick"].forEach(e=>{this.iconInstance.on(e,i=>{this.emit(e,{e:i.originEvent,position:new h.Position(i.lnglat.lng,i.lnglat.lat),pixel:new h.Pixel(i.pixel.x,i.pixel.y),pos:new h.Pixel(i.pos[0],i.pos[1]),data:this.data})})})}renderIcon(){var e,i,s;this.iconInstance&&this.iconInstance.remove();const t={title:this._name,position:this._position.toArray(),icon:new this._ctx.AMap.Icon({image:(e=this._icon)==null?void 0:e.image,imageSize:(i=this._icon)==null?void 0:i.size.toArray()}),visible:this._visible,offset:(s=this._icon)==null?void 0:s.getRealOffset().toArray()};this.iconInstance=new this._ctx.AMap.Marker(t),this._ctx._map.add(this.iconInstance),this.initEvent()}renderLabel(){var e,i,s,n,r,a,p,d,m,b,f;if(this.labelInstance&&this.labelInstance.remove(),!((e=this._label)!=null&&e.text))return;const t={name:this._name,position:this._position.toArray(),text:(i=this._label)==null?void 0:i.text,visible:(s=this._label)==null?void 0:s.visible,anchor:"bottom-center",offset:((p=(a=(r=(n=this._label)==null?void 0:n.getRealOffset)==null?void 0:r.call(n,{padding:[2,3,2,3]}))==null?void 0:a.toArray)==null?void 0:p.call(a))??[0,(d=this._icon)==null?void 0:d.getRealOffset().y],style:{border:"none","background-color":(m=this._label)==null?void 0:m.backgroundColor,"font-size":((b=this._label)==null?void 0:b.fontSize)+"px",color:(f=this._label)==null?void 0:f.color}};this.labelInstance=new this._ctx.AMap.Text(t),this._ctx._map.add(this.labelInstance)}get map(){return this.iconInstance.getMap()}setName(t){super.setName(t),this.iconInstance.name=this._name}setPosition(t){super.setPosition(t),this.iconInstance.setPosition(this._position.toArray())}setIcon(t){super.setIcon(t),this.renderIcon()}setLabel(t){super.setLabel(t),this.renderLabel()}hidden(){super.hidden(),this.iconInstance.hide()}show(){super.show(),this.iconInstance.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.iconInstance,this.labelInstance].filter(e=>!!e)}remove(){this.getInstances().forEach(t=>t.remove())}panTo(){this.map.panTo(this._position.toArray())}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 s={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:this._position.toArray()},n=new this._ctx.AMap.InfoWindow(s);n.open(this._ctx._map),n.dom.style.height="0";const r=n.dom.querySelector(".amap-info-contentContainer");r&&(r.style.pointerEvents="none"),n.on("close",()=>{const a=n.dom;a.parentNode.removeChild(a)})}closeInfoWindow(){this._ctx._map.clearInfoWindow()}setStyle(t){super.setStyle(t),this.renderIcon(),this.renderLabel()}}exports.AMapMarker=I;