UNPKG

leaflet-coord-converter-xiaoai

Version:

一个 Leaflet 坐标转换插件,支持GPS84、GCJ02、BD09坐标系之间的转换,提供简单易用的 API 和完整的 TypeScript 类型定义。

2 lines (1 loc) 4.4 kB
(function(n,c){typeof exports=="object"&&typeof module!="undefined"?module.exports=c(require("leaflet")):typeof define=="function"&&define.amd?define(["leaflet"],c):(n=typeof globalThis!="undefined"?globalThis:n||self,n.CoordConverter=c(n.L))})(this,function(n){"use strict";return n.CoordConverter=class{constructor(){this.pi=Math.PI,this.a=6378245,this.ee=.006693421622965943,this.x_pi=this.pi*3e3/180,this.R=6378137}bd09ToGps84(o,t){const s=this.bd09ToGcj02(o,t);return this.gcj02ToGps84(s.lng,s.lat)}gps84ToBd09(o,t){const s=this.gps84ToGcj02(o,t);return this.gcj02ToBd09(s.lng,s.lat)}gps84ToGcj02(o,t){let s=this._transformLat(o-105,t-35),i=this._transformLng(o-105,t-35);const e=t/180*this.pi;let r=Math.sin(e);r=1-this.ee*r*r;const h=Math.sqrt(r);s=s*180/(this.a*(1-this.ee)/(r*h)*this.pi),i=i*180/(this.a/h*Math.cos(e)*this.pi);const a=t+s;return{lng:o+i,lat:a}}gcj02ToGps84(o,t){const s=this._transform(o,t),i=o*2-s.lng,e=t*2-s.lat;return{lng:i,lat:e}}gcj02ToBd09(o,t){const s=Math.sqrt(o*o+t*t)+2e-5*Math.sin(t*this.x_pi),i=Math.atan2(t,o)+3e-6*Math.cos(o*this.x_pi),e=s*Math.cos(i)+.0065,r=s*Math.sin(i)+.006;return{lng:e,lat:r}}bd09ToGcj02(o,t){const s=o-.0065,i=t-.006,e=Math.sqrt(s*s+i*i)-2e-5*Math.sin(i*this.x_pi),r=Math.atan2(i,s)-3e-6*Math.cos(s*this.x_pi),h=e*Math.cos(r),a=e*Math.sin(r);return{lng:h,lat:a}}convertArray(o,t,s){if(t===s)return o;const i={gps84_gcj02:this.gps84ToGcj02.bind(this),gps84_bd09:this.gps84ToBd09.bind(this),gcj02_gps84:this.gcj02ToGps84.bind(this),gcj02_bd09:this.gcj02ToBd09.bind(this),bd09_gps84:this.bd09ToGps84.bind(this),bd09_gcj02:this.bd09ToGcj02.bind(this)},e=`${t}_${s}`,r=i[e];if(!r)throw new Error(`不支持的转换类型: ${t} -> ${s}`);return o.map(h=>r(h.lng,h.lat))}_transform(o,t){let s=this._transformLat(o-105,t-35),i=this._transformLng(o-105,t-35);const e=t/180*this.pi;let r=Math.sin(e);r=1-this.ee*r*r;const h=Math.sqrt(r);s=s*180/(this.a*(1-this.ee)/(r*h)*this.pi),i=i*180/(this.a/h*Math.cos(e)*this.pi);const a=t+s;return{lng:o+i,lat:a}}_transformLat(o,t){let s=-100+2*o+3*t+.2*t*t+.1*o*t+.2*Math.sqrt(Math.abs(o));return s+=(20*Math.sin(6*o*this.pi)+20*Math.sin(2*o*this.pi))*2/3,s+=(20*Math.sin(t*this.pi)+40*Math.sin(t/3*this.pi))*2/3,s+=(160*Math.sin(t/12*this.pi)+320*Math.sin(t*this.pi/30))*2/3,s}_transformLng(o,t){let s=300+o+2*t+.1*o*o+.1*o*t+.1*Math.sqrt(Math.abs(o));return s+=(20*Math.sin(6*o*this.pi)+20*Math.sin(2*o*this.pi))*2/3,s+=(20*Math.sin(o*this.pi)+40*Math.sin(o/3*this.pi))*2/3,s+=(150*Math.sin(o/12*this.pi)+300*Math.sin(o/30*this.pi))*2/3,s}},n.coordConverter=new n.CoordConverter,n.coordConvert=function(){return n.coordConverter},n.GridLayer.include({_setZoomTransform:function(o,t,s){let i=t;i!=null&&this.options&&(this.options.currentType==="gps84"&&(this.options.coordType==="gcj02"?i=n.coordConverter.gps84ToGcj02(t.lng,t.lat):this.options.coordType==="bd09"&&(i=n.coordConverter.gps84ToBd09(t.lng,t.lat))),this.options.currentType==="gcj02"&&(this.options.coordType==="gps84"?i=n.coordConverter.gcj02ToGps84(t.lng,t.lat):this.options.coordType==="bd09"&&(i=n.coordConverter.gcj02ToBd09(t.lng,t.lat))),this.options.currentType==="bd09"&&(this.options.coordType==="gps84"?i=n.coordConverter.bd09ToGps84(t.lng,t.lat):this.options.coordType==="gcj02"&&(i=n.coordConverter.bd09ToGcj02(t.lng,t.lat))));const e=this._map.getZoomScale(s,o.zoom),r=o.origin.multiplyBy(e).subtract(this._map._getNewPixelOrigin(i,s)).round();n.Browser.any3d?n.DomUtil.setTransform(o.el,r,e):n.DomUtil.setPosition(o.el,r)},_getTiledPixelBounds:function(o){let t=o;t!=null&&this.options&&this.options.currentType==="gps84"&&(this.options.coordType==="gcj02"?t=n.coordConverter.gps84ToGcj02(o.lng,o.lat):this.options.coordType==="bd09"&&(t=n.coordConverter.gps84ToBd09(o.lng,o.lat))),this.options.currentType==="gcj02"&&(this.options.coordType==="gps84"?t=n.coordConverter.gcj02ToGps84(o.lng,o.lat):this.options.coordType==="bd09"&&(t=n.coordConverter.gcj02ToBd09(o.lng,o.lat))),this.options.currentType==="bd09"&&(this.options.coordType==="gps84"?t=n.coordConverter.bd09ToGps84(o.lng,o.lat):this.options.coordType==="gcj02"&&(t=n.coordConverter.bd09ToGcj02(o.lng,o.lat)));const s=this._map,i=s._animatingZoom?Math.max(s._animateToZoom,s.getZoom()):s.getZoom(),e=s.getZoomScale(i,this._tileZoom),r=s.project(t,this._tileZoom).floor(),h=s.getSize().divideBy(e*2);return new n.Bounds(r.subtract(h),r.add(h))}}),n.CoordConverter});