Map.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import OLMap from "ol/Map";
  2. import View from "ol/View";
  3. import * as proj from "ol/proj";
  4. import * as interaction from "ol/interaction";
  5. import "ol/ol.css";
  6. import "./css/KMap.css";
  7. import Common from "./Common";
  8. import VectorLayer from "./VectorLayer";
  9. import WMTSLayer from "./WMTSLayer";
  10. import XYZLayer from "./XYZLayer";
  11. import config from "@/api/config.js";
  12. /**
  13. * KMap.Map —— 与 feiniao-pc-vue 用法对齐的精简实现
  14. * new KMap.Map(target, level, lng, lat, projection, minZoom, maxZoom, ...)
  15. */
  16. class Map {
  17. static Instance = null;
  18. constructor(id, zoomLevel, lng, lat, projection, minZoom, maxZoom) {
  19. this.defaultCursor = "default";
  20. Map.Instance = this;
  21. if (projection) {
  22. projection = proj.get(projection);
  23. }
  24. projection = projection || proj.get("EPSG:4326");
  25. let lnglat = [lng, lat];
  26. if (projection.getCode() === "EPSG:3857") {
  27. lnglat = proj.fromLonLat(lnglat);
  28. }
  29. Common.checkLngLat(lng, lat);
  30. this.view = new View({
  31. center: lnglat,
  32. zoom: zoomLevel,
  33. minZoom: minZoom || Common.ShowLevel[0],
  34. maxZoom: maxZoom || Common.ShowLevel[1],
  35. projection,
  36. });
  37. this.map = new OLMap({
  38. interactions: interaction.defaults().extend([new interaction.DragRotateAndZoom()]),
  39. target: id,
  40. layers: [],
  41. view: this.view,
  42. controls: [],
  43. });
  44. this.initBaseLayer(projection);
  45. this.initBusinessLayer();
  46. }
  47. /** 与 pc-vue / IDN 对齐:连续卫星底图 + WMTS + 农场 XYZ */
  48. async initBaseLayer(projection) {
  49. // 先铺连续卫星底图(农场 lby 只覆盖局部,无此层会黑底碎块)
  50. // 与 IDN-pc-vue 一致使用 ArcGIS World Imagery;放在 await 之前,避免异步等待期间黑屏
  51. const worldImg =
  52. "https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}";
  53. this.addXYZLayer(worldImg, { minZoom: 1, maxZoom: 19 }, 1);
  54. try {
  55. const getCfg = window.VE_API?.system?.getCfg;
  56. if (getCfg) {
  57. const img_wmts = await getCfg({ k: "img_wmts_mkt", resultType: "json" });
  58. const cva_wmts = await getCfg({ k: "cva_wmts_mkt", resultType: "json" });
  59. const imgData = typeof img_wmts?.data === "string" ? JSON.parse(img_wmts.data) : img_wmts?.data;
  60. const cvaData = typeof cva_wmts?.data === "string" ? JSON.parse(cva_wmts.data) : cva_wmts?.data;
  61. if (imgData?.url) {
  62. this.tdtImgLayer = new WMTSLayer(imgData, projection, this);
  63. }
  64. if (cvaData?.url) {
  65. this.cva_torLayer = new WMTSLayer(cvaData, projection, this);
  66. }
  67. }
  68. } catch (e) {
  69. console.warn("[KMap.Map] 底图 WMTS 配置加载失败", e);
  70. }
  71. const xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
  72. this.addXYZLayer(xyz2, { minZoom: 15, maxZoom: 22 }, 3);
  73. }
  74. addXYZLayer(url, options, zIndex = 3) {
  75. return new XYZLayer(url, options, zIndex, this);
  76. }
  77. initBusinessLayer() {
  78. const vm = this;
  79. const map = vm.map;
  80. vm.markerLayer = new VectorLayer("defaultMarkerLayer", 101);
  81. vm.polyLineLayer = new VectorLayer("defaultPolylineLayer", 101);
  82. vm.polygonLayer = new VectorLayer("defaultPolygonLayer", 1000);
  83. vm.labelLayer = new VectorLayer("defaultLabelLayer", 99);
  84. map.addLayer(vm.polygonLayer.layer);
  85. map.once("postrender", () => {
  86. map.addLayer(vm.markerLayer.layer);
  87. map.addLayer(vm.polyLineLayer.layer);
  88. map.addLayer(vm.labelLayer.layer);
  89. });
  90. }
  91. addLayer(layer) {
  92. this.map.addLayer(layer);
  93. }
  94. fit(geometryOrExtent, options) {
  95. this.view.fit(geometryOrExtent, options);
  96. }
  97. on(type, listener) {
  98. return this.map.on(type, listener);
  99. }
  100. updateSize() {
  101. this.map.updateSize();
  102. }
  103. destroy() {
  104. if (this.map) {
  105. this.map.setTarget(null);
  106. this.map = null;
  107. }
  108. if (Map.Instance === this) {
  109. Map.Instance = null;
  110. }
  111. }
  112. }
  113. export default Map;