2026-04-03 11:21:55 +08:00

2 lines
16 KiB
JavaScript

import{d as U,r as G,al as J,as as V,bI as F,J as X,h as Y,c as W,n as D,aH as ee,Y as $,k as E,M as te,am as K}from"./index-c42d56a0.js";/* empty css */import{X as ne}from"./sys-53bca014.js";function j(t,e){t=t.replace(/=/g,"");var n=[];switch(e.constructor){case String:case Number:case Boolean:n.push(encodeURIComponent(t)+"="+encodeURIComponent(e));break;case Array:e.forEach(function(o){n=n.concat(j(t+"[]=",o))});break;case Object:Object.keys(e).forEach(function(o){var p=e[o];n=n.concat(j(t+"["+o+"]",p))})}return n}function Z(t){var e=[];return t.forEach(function(n){typeof n=="string"?e.push(n):e=e.concat(Z(n))}),e}function re(t,e,n){if(e===void 0&&(e={}),typeof t!="string")throw new Error('[Vue-jsonp] Type of param "url" is not string.');if(typeof e!="object"||!e)throw new Error("[Vue-jsonp] Invalid params, should be an object.");return n=typeof n=="number"?n:5e3,new Promise(function(o,p){var v=typeof e.callbackQuery=="string"?e.callbackQuery:"callback",f=typeof e.callbackName=="string"?e.callbackName:"jsonp_"+(Math.floor(1e5*Math.random())*Date.now()).toString(16);e[v]=f,delete e.callbackQuery,delete e.callbackName;var l=[];Object.keys(e).forEach(function(b){l=l.concat(j(b,e[b]))});var s=Z(l).join("&"),c=function(){r(),clearTimeout(m),p({status:400,statusText:"Bad Request"})},r=function(){g.removeEventListener("error",c)},a=function(){document.body.removeChild(g),delete window[f]},m=null;n>-1&&(m=setTimeout(function(){r(),a(),p({statusText:"Request Timeout",status:408})},n)),window[f]=function(b){clearTimeout(m),r(),a(),o(b)};var g=document.createElement("script");g.addEventListener("error",c),g.src=t+(/\?/.test(t)?"&":"?")+s,document.body.appendChild(g)})}const C={},oe=(t,e,n)=>{var s,c,r;const o=window.TMap,p=o.LatLng;e.radius=e.radius??1e3,e.center=e.center??{lat:t.getCenter().lat,lng:t.getCenter().lng};const v=[Math.floor(Math.random()*255),Math.floor(Math.random()*255),Math.floor(Math.random()*255)],f=new o.MultiCircle({map:t,styles:{circle:new o.CircleStyle({color:`rgba(${v.toString()}, .4)`,showBorder:!0,borderColor:`rgb(${v.toString()})`,borderWidth:2}),highlight:new o.PolygonStyle({color:"rgba(255, 255, 0, 0.6)"})},geometries:[{styleId:"circle",center:new p(e.center.lat,e.center.lng),radius:parseInt(e.radius),id:e.key}]});if(C[e.key])try{C[e.key].graphical.remove(e.key),(s=C[e.key].editor)==null||s.delete()}catch{}let l=null;if(o.tools)try{l=new o.tools.GeometryEditor({map:t,overlayList:[{overlay:f,id:e.key,selectedStyleId:"highlight"}],actionMode:((r=(c=o.tools.constants)==null?void 0:c.EDITOR_ACTION)==null?void 0:r.INTERACT)||0,activeOverlayId:null,selectable:!0,snappable:!0}),l.setKeyboardDeleteEnable(!1),l.on("adjust_start",()=>{A==null||A.setDraggable(!1)}),l.on("adjust_complete",a=>{a!=null&&a.center&&a.radius!==void 0&&(e.center={lat:a.center.lat,lng:a.center.lng},e.radius=parseInt(a.radius)),setTimeout(()=>A==null?void 0:A.setDraggable(!0),100)}),l.on("adjust",a=>{a!=null&&a.center&&a.radius!==void 0&&(e.center={lat:a.center.lat,lng:a.center.lng},e.radius=parseInt(a.radius))}),l.on("select",()=>{n&&n(e.key)})}catch(a){console.warn("创建编辑器失败:",a)}f.on("click",()=>{var a;l&&(l.select([e.key]),(a=l.setActiveOverlay)==null||a.call(l,e.key)),n&&n(e.key)}),C[e.key]={graphical:f,editor:l}},le=(t,e,n)=>{var r,a,m;const o=window.TMap,p=o.LatLng,{lat:v,lng:f}=t.getCenter();e.paths=e.paths??[{lat:v+.01,lng:f+.01},{lat:v-.01,lng:f+.01},{lat:v-.01,lng:f-.01},{lat:v+.01,lng:f-.01}];const l=[Math.floor(Math.random()*255),Math.floor(Math.random()*255),Math.floor(Math.random()*255)];if(C[e.key])try{C[e.key].graphical.remove(e.key),(r=C[e.key].editor)==null||r.delete()}catch{}const s=new o.MultiPolygon({map:t,styles:{polygon:new o.PolygonStyle({color:`rgba(${l.toString()}, .4)`,showBorder:!0,borderColor:`rgb(${l.toString()})`,borderWidth:2}),highlight:new o.PolygonStyle({color:"rgba(255, 255, 0, 0.6)"})},geometries:[{id:e.key,styleId:"polygon",paths:e.paths.map(g=>new p(g.lat,g.lng))}]});let c=null;if(o.tools)try{c=new o.tools.GeometryEditor({map:t,overlayList:[{overlay:s,id:e.key,selectedStyleId:"highlight"}],actionMode:((m=(a=o.tools.constants)==null?void 0:a.EDITOR_ACTION)==null?void 0:m.INTERACT)||0,activeOverlayId:null,selectable:!0,snappable:!0}),c.setKeyboardDeleteEnable(!1),c.on("adjust_start",()=>{A==null||A.setDraggable(!1)}),c.on("adjust_complete",g=>{g!=null&&g.paths&&(e.paths=g.paths.map(b=>({lat:b.lat,lng:b.lng}))),setTimeout(()=>A==null?void 0:A.setDraggable(!0),100)}),c.on("adjust",g=>{g!=null&&g.paths&&(e.paths=g.paths.map(b=>({lat:b.lat,lng:b.lng})))}),c.on("select",()=>{n&&n(e.key)})}catch(g){console.warn("创建编辑器失败:",g)}s.on("click",()=>{var g;c&&(c.select([e.key]),(g=c.setActiveOverlay)==null||g.call(c,e.key)),n&&n(e.key)}),C[e.key]={graphical:s,editor:c}},z=t=>{var e,n;if(C[t])try{(e=C[t].graphical)==null||e.remove(t),(n=C[t].editor)==null||n.delete(),delete C[t]}catch(o){console.warn("删除图形失败:",o)}};let A=null;const ae=t=>{var e,n,o,p,v;if((e=C[t])!=null&&e.editor)try{Object.keys(C).forEach(f=>{var l,s,c,r,a;f!==t&&((l=C[f])!=null&&l.editor)&&((c=(s=C[f].editor).deselect)==null||c.call(s),(a=(r=C[f].editor).setActiveOverlay)==null||a.call(r,null))}),(o=(n=C[t].editor).select)==null||o.call(n,[t]),(v=(p=C[t].editor).setActiveOverlay)==null||v.call(p,t)}catch{}},ce=t=>{const e=window.TMap;return e.LatLng,new e.MultiMarker({map:t,geometries:[{id:"center",position:t.getCenter()}]})},ie=t=>re(`https://apis.map.qq.com/ws/geocoder/v1/?key=${t.mapKey}&location=${t.lat},${t.lng}&output=jsonp&callback=latLngToAddress`,{callbackName:"latLngToAddress"}),i={},se=(t,e,n,o)=>{const p=window.T;if(!p)return null;e.radius=e.radius??1e3,e.center=e.center??{lat:t.getCenter().lat,lng:t.getCenter().lng};const v=[Math.floor(Math.random()*255),Math.floor(Math.random()*255),Math.floor(Math.random()*255)],f={color:e.color||`rgba(${v.toString()}, .4)`,weight:e.weight||3,opacity:e.opacity||.8,fillColor:e.fillColor||`rgba(${v.toString()}, .4)`,fillOpacity:e.fillOpacity||.3},l=new p.Circle(new p.LngLat(e.center.lng,e.center.lat),e.radius,f);if(i[e.key])try{i[e.key].editTimer&&clearTimeout(i[e.key].editTimer),t.removeOverLay(i[e.key].graphical)}catch{}t.addOverLay(l);const s={...e};let c={...s.center},r=s.radius;const a=()=>{var g;l.isEditable()&&((g=i[s.key])!=null&&g.editTimer&&clearTimeout(i[s.key].editTimer),i[s.key].editTimer=setTimeout(()=>{const b=l.getCenter(),O=l.getRadius();(b.lng!==c.lng||b.lat!==c.lat)&&(s.center={lat:b.lat,lng:b.lng},c={...s.center},n&&n(s)),O!==r&&(s.radius=O,r=O,n&&n(s))},200))},m=()=>{o&&o(s.key)};return t.addEventListener("touchend",a),t.addEventListener("mouseup",a),l.addEventListener("click",m),i[s.key]={graphical:l,handleTouchEnd:a,handleClick:m,originalStyle:f},l},de=(t,e,n,o)=>{const p=window.T;if(!p)return null;const{lat:v,lng:f}=t.getCenter();e.paths=e.paths??[{lat:v+.01,lng:f+.01},{lat:v-.01,lng:f+.01},{lat:v-.01,lng:f-.01},{lat:v+.01,lng:f-.01}];const l=e.paths.map(O=>new p.LngLat(O.lng,O.lat)),s=[Math.floor(Math.random()*255),Math.floor(Math.random()*255),Math.floor(Math.random()*255)],c={color:e.color||`rgba(${s.toString()}, .4)`,weight:e.weight||3,opacity:e.opacity||.8,fillColor:e.fillColor||`rgba(${s.toString()}, .4)`,fillOpacity:e.fillOpacity||.3},r=new p.Polygon(l,c);if(i[e.key])try{i[e.key].editTimer&&clearTimeout(i[e.key].editTimer),t.removeOverLay(i[e.key].graphical)}catch{}t.addOverLay(r);let a=JSON.parse(JSON.stringify(e.paths));const m={...e},g=()=>{var O;r.isEditable()&&((O=i[m.key])!=null&&O.editTimer&&clearTimeout(i[m.key].editTimer),i[m.key].editTimer=setTimeout(()=>{try{const N=r.getLngLats();if(Array.isArray(N)){let S=N;Array.isArray(N[0])&&(S=N[0]);const _=S.map(I=>Array.isArray(I)?{lat:I[1],lng:I[0]}:{lat:I.lat,lng:I.lng});JSON.stringify(_)!==JSON.stringify(a)&&(m.paths=_,a=JSON.parse(JSON.stringify(m.paths)),n&&n(m))}else console.error("curLngLats 不是数组:",N)}catch(N){console.error("获取多边形路径失败:",N)}},200))},b=()=>{o&&o(m.key)};return t.addEventListener("touchend",g),t.addEventListener("mouseup",g),r.addEventListener("click",b),i[m.key]={graphical:r,handleTouchEnd:g,handleClick:b,originalStyle:c},r},ue=(t,e)=>{if(i[e])try{window.T&&i[e].graphical&&(i[e].editTimer&&clearTimeout(i[e].editTimer),i[e].handleTouchEnd&&(t.removeEventListener("touchend",i[e].handleTouchEnd),t.removeEventListener("mouseup",i[e].handleTouchEnd)),i[e].handleClick&&i[e].graphical.removeEventListener("click",i[e].handleClick),t.removeOverLay(i[e].graphical)),delete i[e]}catch(n){console.warn("删除图形失败:",n)}},fe=t=>{window.T&&(Object.keys(i).forEach(n=>{i[n].editTimer&&clearTimeout(i[n].editTimer),i[n].handleTouchEnd&&(t.removeEventListener("touchend",i[n].handleTouchEnd),t.removeEventListener("mouseup",i[n].handleTouchEnd)),i[n].handleClick&&i[n].graphical.removeEventListener("click",i[n].handleClick)}),t.clearOverLays()),Object.keys(i).forEach(n=>delete i[n])},he=t=>{var e,n,o;if((e=i[t])!=null&&e.graphical)try{ye(),(o=(n=i[t].graphical).setSelected)==null||o.call(n,!0),i[t].graphical.enableEdit();const p=[Math.floor(Math.random()*255),Math.floor(Math.random()*255),Math.floor(Math.random()*255)];i[t].graphical.setStyle({color:"#1890ff",weight:2,opacity:1,fillColor:`rgba(${p.toString()}, .6)`,fillOpacity:1})}catch(p){console.error("选中图形失败:",p)}},ye=()=>{Object.keys(i).forEach(t=>{var e,n,o,p,v,f,l;try{(o=(n=(e=i[t])==null?void 0:e.graphical)==null?void 0:n.setSelected)==null||o.call(n,!1),(f=(v=(p=i[t])==null?void 0:p.graphical)==null?void 0:v.disableEdit)==null||f.call(v),(l=i[t])!=null&&l.originalStyle&&i[t].graphical.setStyle(i[t].originalStyle)}catch(s){console.warn("取消选中图形失败:",s)}})},ge=t=>new Promise((e,n)=>{const o=window.T;if(!o){n(new Error("天地图服务未加载"));return}const p=new o.Geocoder,v=new o.LngLat(t.lng,t.lat);p.getLocation(v,f=>{if(f&&f.status==="0"){const l=f.addressComponent||{};let s="";if(f.formattedAddress)s=f.formattedAddress;else if(f.address)s=f.address;else{const r=[];l.road&&r.push(l.road),l.address&&r.push(l.address),l.poi&&r.push(l.poi),s=r.join("")}const c={location:{lat:t.lat,lng:t.lng},address:l?`${l.province||""}${l.city||""}${l.county||""}`:"",formatted_addresses:{recommend:s},address_component:{province:l.province||"",city:l.city||"",district:l.county||"",street:l.road||"",street_number:l.address||""},addressComponent:l,formatted_address:s};e(c)}else n(new Error((f==null?void 0:f.msg)||"地址解析失败"))})}),pe=["id"],me=U({__name:"index",props:{containerId:{default:"map-container"},containerClass:{default:"w-[800px] h-[500px] relative border border-gray-300"},containerStyle:{default:""},longitude:{default:116.397463},latitude:{default:39.909187},zoom:{default:14},selectedKey:{default:""},disabledClickMarker:{default:!1}},emits:["update:longitude","update:latitude","locationChange","areaChange","selectChange"],setup(t,{expose:e,emit:n}){const o=t,p=500,v=100,f=50,l=G(!0),s=G(""),c=G("tianditu");let r=null,a=null;const m=G([]),g=()=>ne({need_encrypt:!1}).then(d=>{c.value=d.data.map_type||"tianditu",s.value=c.value==="tianditu"?d.data.tianditu_map_web_key:d.data.key}).catch(d=>{l.value=!1,$.error(E("获取地图配置失败"))}),b=()=>{const d=document.createElement("script");d.src="https://map.qq.com/api/gljs?libraries=tools,service&v=1.exp&key="+s.value,document.body.appendChild(d),d.onload=()=>{setTimeout(()=>{const y=window.TMap;if(y){const h=o.longitude?Number(o.longitude):116.397463,w=o.latitude?Number(o.latitude):39.909187,T=new y.LatLng(w,h);r=new y.Map(o.containerId,{center:T,zoom:o.zoom}),r.on("tilesloaded",()=>l.value=!1),a=ce(r),o.disabledClickMarker||r.on("click",L=>{const u=L==null?void 0:L.latLng;if(!u)return;const k=typeof u.getLat=="function"?u.getLat():u.lat,M=typeof u.getLng=="function"?u.getLng():u.lng;if(k==null||M==null||Number.isNaN(k)||Number.isNaN(M))return;const P=new y.LatLng(k,M);r.setZoom(16),r.setCenter(P),a.updateGeometries({id:"center",position:P}),K(()=>{S(k,M)})}),o.latitude&&o.longitude&&S(w,h)}else l.value=!1,$.error(E("腾讯地图加载失败"))},p)},d.onerror=()=>{l.value=!1,$.error(E("腾讯地图脚本加载失败"))}},O=()=>{const d=document.createElement("script");d.src="https://api.tianditu.gov.cn/api?v=4.0&tk="+s.value,document.body.appendChild(d),d.onload=()=>{setTimeout(()=>{const y=window.T;if(y){const h=o.longitude?Number(o.longitude):116.397463,w=o.latitude?Number(o.latitude):39.909187;r=new y.Map(o.containerId),r.centerAndZoom(new y.LngLat(h,w),o.zoom);const T=new y.Control.Zoom;r.addControl(T),a=new y.Marker(new y.LngLat(h,w)),r.addOverLay(a),l.value=!1,o.disabledClickMarker||r.addEventListener("click",L=>{const u=L.lnglat;if(!u)return;const k=u.lat,M=u.lng;k==null||M==null||Number.isNaN(k)||Number.isNaN(M)||(r.removeOverLay(a),a=new y.Marker(new y.LngLat(M,k)),r.addOverLay(a),r.centerAndZoom(new y.LngLat(M,k),16),K(()=>{S(k,M)}))}),o.latitude&&o.longitude&&S(w,h)}else l.value=!1,$.error(E("天地图加载失败"))},p)},d.onerror=()=>{l.value=!1,$.error(E("天地图加载失败,请检查密钥配置"))}},N=()=>{if(!s.value){l.value=!1;return}r||(c.value==="tencent"?b():c.value==="tianditu"&&O())},S=(d,y)=>{n("update:longitude",String(y)),n("update:latitude",String(d)),c.value==="tencent"?ie({mapKey:s.value,lat:d,lng:y}).then(({message:h,result:w})=>{(h=="query ok"||h=="Success")&&n("locationChange",{lat:d,lng:y,address:w})}):c.value==="tianditu"&&ge({mapKey:s.value,lat:d,lng:y}).then(h=>{n("locationChange",{lat:d,lng:y,address:h})}).catch(h=>{$.error(h.message||E("地址解析失败"))})},_=(d,y)=>{if(!(!r||!a)){if(c.value==="tencent"){const h=window.TMap;if(!h)return;const w=new h.LatLng(d,y);r.setZoom(16),r.setCenter(w),a.updateGeometries({id:"center",position:w})}else if(c.value==="tianditu"){const h=window.T;if(!h)return;const w=new h.LngLat(y,d);r.removeOverLay(a),a=new h.Marker(w),r.addOverLay(a),r.centerAndZoom(w,16)}}},R=d=>new Promise((y,h)=>{if(!s.value){h(new Error(E("地图配置未加载")));return}(()=>new Promise((T,L)=>{if(r){T();return}let u=0;const k=setInterval(()=>{u++,r?(clearInterval(k),T()):u>f&&(clearInterval(k),L(new Error(E("地图初始化超时"))))},v)}))().then(()=>{if(c.value==="tencent"){const T=window.TMap;if(!(T!=null&&T.service)){h(new Error(E("腾讯地图服务未加载")));return}new T.service.Geocoder({key:s.value}).getLocation({address:d}).then(u=>{var k;if(u.status===0&&((k=u.result)!=null&&k.location)){const M=u.result.location,P=Number(M.lat),B=Number(M.lng);_(P,B),y({lat:P,lng:B})}else h(new Error(u.message||E("未找到该地址")))}).catch(u=>{h(u)})}else if(c.value==="tianditu"){const T=window.T;if(!T){h(new Error(E("天地图服务未加载")));return}new T.Geocoder().getPoint(d,u=>{if(u&&u.status==="0"&&u.location){const k=u.location.lat,M=u.location.lon||u.location.lng;k&&M?(_(k,M),y({lat:k,lng:M})):h(new Error(E("坐标解析失败")))}else h(new Error((u==null?void 0:u.msg)||E("未找到该地址")))})}}).catch(h)}),I=(d,y=1e3,h)=>{if(!r)return null;const w=(h==null?void 0:h.key)||`circle_${Date.now()}`;let T=null;const L={key:w,center:d,radius:y,...h};return c.value==="tencent"?(oe(r,L,u=>{n("selectChange",u)}),T={key:w,geometriesData:L}):(T=se(r,L,u=>{n("areaChange",{key:u.key,type:"circle",path:{key:u.key,center:u.center,radius:u.radius}})},u=>{n("selectChange",u)}),T&&(T.geometriesData=L)),T&&m.value.push(T),T},H=(d,y)=>{if(!r)return null;const h=(y==null?void 0:y.key)||`poly_${Date.now()}`;let w=null;const T={key:h,paths:d,...y};return c.value==="tencent"?(le(r,T,L=>{n("selectChange",L)}),w={key:h,geometriesData:T}):(w=de(r,T,L=>{n("areaChange",{key:L.key,type:"polygon",path:{key:L.key,paths:L.paths.map(u=>({lat:u.lat,lng:u.lng}))}})},L=>{n("selectChange",L)}),w&&(w.geometriesData=T)),w&&m.value.push(w),w},Q=d=>{if(!r||!d)return;const y=d.key||"";c.value==="tencent"?z(y):ue(r,y);const h=m.value.findIndex(w=>w.key===y);h>-1&&m.value.splice(h,1)},x=()=>{r&&(c.value==="tencent"?m.value.forEach(d=>z(d.key)):fe(r),m.value=[])},q=d=>{c.value==="tencent"?ae(d):he(d)};return J(()=>o.selectedKey,d=>{d&&q(d)}),J([s,c],([d,y])=>{d&&y&&N()}),V(()=>{g()}),F(()=>{if(r){try{c.value==="tencent"?r.destroy&&r.destroy():c.value==="tianditu"&&(x(),r.clearOverLays&&r.clearOverLays())}catch{}r=null,a=null,m.value=[]}}),e({updateLocation:_,searchAddress:R,addCircle:I,addPolygon:H,removeOverlay:Q,clearOverlays:x,selectGeometry:q,getMap:()=>r,getMarker:()=>a,handleLocationChange:S}),(d,y)=>{const h=te;return X((Y(),W("div",{id:t.containerId,class:D(t.containerClass),style:ee(t.containerStyle)},null,14,pe)),[[h,l.value]])}}});export{me as default};