{"version":3,"file":"CustomScale.mjs","sources":["../../../../../packages/sdk/plugins/CustomScale.ts"],"sourcesContent":["import { Control, DomUtil } from 'leaflet'\nimport type { ControlOptions, Map as LeafletMap } from 'leaflet'\n\n// 定义比例尺单位类型\ntype ScaleUnit = 'nautical' | 'kilometer' | 'mile'\n\n// 定义自定义比例尺控件的选项类型\ninterface CustomScaleOptions extends ControlOptions {\n  maxWidth?: number\n  units?: ScaleUnit[]\n  unitLabels?: { [key in ScaleUnit]: string }\n}\n\n// 自定义比例尺控件类\nclass CustomScale extends Control {\n  private _container!: HTMLElement\n  private _map!: LeafletMap\n  private _maxWidth: number\n  private _position: string\n  private _units: ScaleUnit[]\n  private _unitLabels: { [key in ScaleUnit]: string }\n\n  constructor(options?: CustomScaleOptions) {\n    super(options)\n\n    this._maxWidth = options?.maxWidth || 100\n    this._position = options?.position || 'bottomleft'\n    this._units = options?.units || ['kilometer']\n    this._unitLabels = options?.unitLabels || {\n      nautical: 'nm',\n      kilometer: 'km',\n      mile: 'mi',\n    }\n\n    this.onAdd = this.onAdd.bind(this)\n    this.update = this.update.bind(this)\n  }\n\n  override onAdd(map: LeafletMap): HTMLElement {\n    this._map = map\n\n    // 创建比例尺容器\n    this._container = DomUtil.create(\n      'div',\n      'leaflet-control-custom-scale leaflet-bar'\n    )\n    this._container.style.backgroundColor = '#fff'\n    this._container.style.boxShadow = '0 1px 5px rgba(0, 0, 0, 0.65)'\n    this._container.style.padding = '6px'\n    this._container.style.borderRadius = '4px'\n\n    // 设置控件位置\n    this.setPosition(this._position)\n\n    // 监听地图缩放事件，触发比例尺更新\n    map.on('zoomend', this.update)\n\n    // 初次添加时立即更新比例尺\n    this.update()\n\n    return this._container\n  }\n\n  override onRemove(map: LeafletMap): void {\n    map.off('zoomend', this.update)\n  }\n\n  override setPosition(position: string): this {\n    if (position !== this._position) {\n      this._position = position\n      this._map.removeControl(this)\n      this._map.addControl(this)\n    }\n    return this\n  }\n\n  update(): void {\n    // 清除现有内容\n    DomUtil.empty(this._container)\n\n    // 计算当前地图分辨率\n    const bounds = this._map.getBounds()\n    const centerLat = bounds.getCenter().lat\n    const wFactor = 1 / Math.cos((centerLat * Math.PI) / 180)\n    const size = this._map.getSize().y\n    const resolutions: { [key in ScaleUnit]: number } = {\n      nautical:\n        this._map.distance(bounds.getNorthWest(), bounds.getSouthEast()) *\n        wFactor *\n        0.539956803,\n      kilometer:\n        this._map.distance(bounds.getNorthWest(), bounds.getSouthEast()) *\n        wFactor *\n        1000,\n      mile:\n        this._map.distance(bounds.getNorthWest(), bounds.getSouthEast()) *\n        wFactor *\n        0.621371192,\n    }\n\n    // 创建比例尺元素和文本\n    for (const unit of this._units) {\n      const resolution = resolutions[unit]\n      const width = Math.round((size * resolution) / this._maxWidth)\n      const text = `${resolution.toFixed(1)} ${this._unitLabels[unit]}`\n\n      const scaleElement = DomUtil.create(\n        'div',\n        'leaflet-control-custom-scale-line'\n      )\n      scaleElement.style.width = `${width}px`\n      scaleElement.style.height = '10px'\n      scaleElement.style.backgroundColor = '#333'\n      scaleElement.style.borderRadius = '2px'\n\n      const scaleTextElement = DomUtil.create(\n        'div',\n        'leaflet-control-custom-scale-text'\n      )\n      scaleTextElement.innerText = text\n\n      this._container.appendChild(scaleElement)\n      this._container.appendChild(scaleTextElement)\n    }\n  }\n}\n\nexport default CustomScale\n\n// const map = L.map('mapid').setView([51.505, -0.09], 13)\n\n// L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {\n//   attribution:\n//     '&copy; <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors'\n// }).addTo(map)\n\n// new CustomScale({\n//   position: 'bottomright',\n//   units: ['nautical', 'kilometer'],\n//   unitLabels: {\n//     nautical: 'n.m.',\n//     kilometer: 'km'\n//   }\n// }).addTo(map)\n"],"names":["u","m","constructor","t","super","this","_maxWidth","maxWidth","_position","position","_units","units","_unitLabels","unitLabels","nautical","kilometer","mile","onAdd","bind","update","_map","_container","i","create","style","backgroundColor","boxShadow","padding","borderRadius","setPosition","on","onRemove","off","removeControl","addControl","empty","getBounds","r","getCenter","lat","a","Math","cos","PI","l","getSize","y","h","distance","getNorthWest","getSouthEast","s","n","c","round","d","toFixed","e","width","height","o","innerText","appendChild"],"mappings":"+CAA+C,MAAMA,UAAUC,EAAE,WAAAC,CAAYC,GAAGC,MAAMD,GAAGE,KAAKC,WAAc,MAAHH,OAAQ,EAAOA,EAAEI,WAAW,IAAIF,KAAKG,WAAc,MAAHL,OAAQ,EAAOA,EAAEM,WAAW,aAAaJ,KAAKK,QAAW,MAAHP,OAAQ,EAAOA,EAAEQ,QAAQ,CAAC,aAAaN,KAAKO,aAAgB,MAAHT,OAAQ,EAAOA,EAAEU,aAAa,CAACC,SAAS,KAAKC,UAAU,KAAKC,KAAK,MAAMX,KAAKY,MAAMZ,KAAKY,MAAMC,KAAKb,MAAMA,KAAKc,OAAOd,KAAKc,OAAOD,KAAKb,KAAK,CAAC,KAAAY,CAAMd,GAAG,OAAOE,KAAKe,KAAKjB,EAAEE,KAAKgB,WAAWC,EAAEC,OAAO,MAAM,4CAA4ClB,KAAKgB,WAAWG,MAAMC,gBAAgB,OAAOpB,KAAKgB,WAAWG,MAAME,UAAU,gCAAgCrB,KAAKgB,WAAWG,MAAMG,QAAQ,MAAMtB,KAAKgB,WAAWG,MAAMI,aAAa,MAAMvB,KAAKwB,YAAYxB,KAAKG,WAAWL,EAAE2B,GAAG,UAAUzB,KAAKc,QAAQd,KAAKc,SAASd,KAAKgB,UAAU,CAAC,QAAAU,CAAS5B,GAAGA,EAAE6B,IAAI,UAAU3B,KAAKc,OAAO,CAAC,WAAAU,CAAY1B,GAAG,OAAOA,IAAIE,KAAKG,YAAYH,KAAKG,UAAUL,EAAEE,KAAKe,KAAKa,cAAc5B,MAAMA,KAAKe,KAAKc,WAAW7B,OAAOA,IAAI,CAAC,MAAAc,GAASG,EAAEa,MAAM9B,KAAKgB,YAAY,MAAMlB,EAAEE,KAAKe,KAAKgB,YAA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