You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

var e=Math.PI;function t(e,t){return e<72.004||e>137.8347||t<.8293||t>55.8271}function n(t,n){let r=-100+2*t+3*n+.2*n*n+.1*t*n+.2*Math.sqrt(Math.abs(t));return r+=(20*Math.sin(6*t*e)+20*Math.sin(2*t*e))*2/3,r+=(20*Math.sin(n*e)+40*Math.sin(n/3*e))*2/3,r+=(160*Math.sin(n/12*e)+320*Math.sin(n*e/30))*2/3,r}function r(t,n){let r=300+t+2*n+.1*t*t+.1*t*n+.1*Math.sqrt(Math.abs(t));return r+=(20*Math.sin(6*t*e)+20*Math.sin(2*t*e))*2/3,r+=(20*Math.sin(t*e)+40*Math.sin(t/3*e))*2/3,r+=(150*Math.sin(t/12*e)+300*Math.sin(t/30*e))*2/3,r}function i(i,a){if(t(i,a))return{lng:i,lat:a};let o=6378245,s=.006693421622965943,c=n(i-105,a-35),l=r(i-105,a-35),u=a/180*e,d=Math.sin(u);d=1-s*d*d;let f=Math.sqrt(d);return c=c*180/(o*(1-s)/(d*f)*e),l=l*180/(o/f*Math.cos(u)*e),{lng:i+l,lat:a+c}}function a(e,n){if(t(e,n))return{lng:e,lat:n};let r=e,a=n;for(let t=0;t<2;t++){let t=i(r,a);r=e-(t.lng-r),a=n-(t.lat-a)}return{lng:r,lat:a}}var o=null;function s(){return!1}function c(){return s()?`/tianditu-tile-proxy`:`https://t0.tianditu.gov.cn`}function l(e){return`${c()}/vec_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${e}`}function u(e){return`${c()}/cva_c/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=c&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=${e}`}function d(){return`ebd6693edf7730d8f8f9a7ba1719f5c1`}function f(e,t=!1){let n=window.T,r=new n.TileLayer(l(e),{minZoom:1,maxZoom:18});return t?[r]:[r,new n.TileLayer(u(e),{minZoom:1,maxZoom:18})]}function p(e,t){let n=a(e,t);return new window.T.LngLat(n.lng,n.lat)}async function m(){if(typeof window>`u`)throw Error(`天地图仅支持浏览器环境`);if(window.T)return;let e=d();if(!e)throw Error(`请先配置 VITE_TIANDITU_TK天地图开放平台应用 Key`);if(o||=new Promise((t,n)=>{let r=document.createElement(`script`);r.src=`${s()?`/tianditu-api-proxy`:`https://api.tianditu.gov.cn`}/api?v=4.0&tk=${encodeURIComponent(e)}`,r.async=!0,r.onload=()=>t(),r.onerror=()=>n(Error(`天地图 SDK 加载失败`)),document.head.appendChild(r)}),await o,!window.T)throw Error(`天地图 SDK 未就绪`)}function h(e,t,n,r,i){let a=d(),o=i?.simple===!0,s=new window.T.Map(e,{projection:`EPSG:4326`,layers:f(a,o)});return s.centerAndZoom(p(t,n),r),s}function g(e,t,n,r){let i=window.T;t.overlay&&=(e.removeOverLay(t.overlay),null);let a=new i.Marker(p(r,n));e.addOverLay(a),t.overlay=a}function _(e,t){t&&e.removeOverLay(t)}function v(e,t,n,r=13){e.checkResize?.(),e.centerAndZoom(p(t,n),r),setTimeout(()=>{e.checkResize?.(),e.centerAndZoom(p(t,n),r)},120)}function y(e){let t=i(e.lnglat.getLng(),e.lnglat.getLat());return{lat:Number(t.lat.toFixed(6)),lng:Number(t.lng.toFixed(6))}}function b(e,t){e.addEventListener(`click`,e=>{let{lat:n,lng:r}=y(e);t(n,r)})}function x(){return s()?`/tianditu-api-proxy`:`https://api.tianditu.gov.cn`}function S(e){let t=e.code??e.status,n=String(e.msg??e.message??e.resolve??``).trim();return t===301012||n.includes(`权限`)?`天地图 Key 权限不足:请在控制台为该应用开通「地名搜索」并在浏览器访问(本地开发需 Vite 代理)。`:t===1e3||t===`1000`?null:t!=null&&String(t)!==`0`&&Number(t)!==0?n||`天地图请求失败`:null}function C(e,t){let n=t.trim();if(!n)return Promise.resolve([]);let r=window.T;return r?.LocalSearch?new Promise((t,a)=>{try{new r.LocalSearch(e,{pageCapacity:15,onSearchComplete(e){try{if(parseInt(String(e.getResultType?.()??`1`),10)!==1){t([]);return}let n=e.getPois?.()??[],r=[];for(let e of n){let t=String(e.lonlat??``),n=null,a=null;if(t.includes(`,`)){let[e,r]=t.split(`,`).map(e=>e.trim());n=Number(e),a=Number(r)}if(!Number.isFinite(a)||!Number.isFinite(n))continue;let o=i(n,a);r.push({title:String(e.name??``),address:String(e.address??``),lat:o.lat,lng:o.lng})}t(r)}catch(e){a(e)}}}).search(n)}catch(e){a(e)}}):Promise.reject(Error(`天地图 LocalSearch 未加载,请确认 SDK 已初始化`))}async function w(e,t){let n=d();if(!n)throw Error(`未配置 VITE_TIANDITU_TK`);let r=a(t,e),i=JSON.stringify({lon:r.lng,lat:r.lat,ver:1}),o=`${x()}/geocoder?postStr=${encodeURIComponent(i)}&type=geocode&tk=${encodeURIComponent(n)}`,s=await(await fetch(o)).json(),c=S(s);if(c)throw Error(c);let l=s.result??{},u=l.addressComponent??{};return{address:String(l.formatted_address??l.address??``).trim(),province:u.province?String(u.province):void 0,city:u.city?String(u.city):void 0,district:u.district?String(u.district):u.county?String(u.county):void 0}}async function T(e,t){let n=t.trim();return n?C(e,n):[]}async function E(e,t){return w(e,t)}export{h as a,g as c,_ as i,T as n,m as o,b as r,v as s,E as t};