selectLocationMap.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import * as KMap from "@/utils/ol-map/KMap";
  2. import * as util from "@/common/ol_common.js";
  3. import config from "@/api/config.js";
  4. import Style from "ol/style/Style";
  5. import Icon from "ol/style/Icon";
  6. import { Point } from "ol/geom";
  7. import Feature from "ol/Feature";
  8. import { boundingExtent } from "ol/extent";
  9. import { reactive } from "vue";
  10. export let mapLocation = reactive({
  11. data: null,
  12. });
  13. /**
  14. * @description 常驻点位选点地图
  15. */
  16. class SelectLocationMap {
  17. constructor() {
  18. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  19. style: () => {
  20. return new Style({
  21. image: new Icon({
  22. src: require("@/assets/img/map/current-point.png"),
  23. scale: 0.45,
  24. anchor: [0.5, 1],
  25. }),
  26. });
  27. },
  28. });
  29. this._clickKey = null;
  30. /** @type {[number, number, number, number]|null} [top, right, bottom, left] */
  31. this._padding = null;
  32. this._showPoint = true;
  33. }
  34. /**
  35. * @param {string} location WKT POINT
  36. * @param {HTMLElement} target
  37. * @param {{ enableClick?: boolean, padding?: [number, number, number, number], showPoint?: boolean }} [options]
  38. */
  39. initMap(location, target, options = {}) {
  40. const { enableClick = true, padding = null, showPoint = true } = options;
  41. this._padding = padding;
  42. this._showPoint = showPoint;
  43. const level = 16;
  44. const coordinate = util.wktCastGeom(location).getFirstCoordinate();
  45. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 8, 22);
  46. const xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
  47. this.kmap.addXYZLayer(xyz2, { minZoom: 8, maxZoom: 22 }, 2);
  48. this.kmap.addLayer(this.clickPointLayer.layer);
  49. if (showPoint) {
  50. this.setMapPoint(coordinate);
  51. }
  52. mapLocation.data = coordinate;
  53. if (enableClick) {
  54. this.addMapSingerClick();
  55. }
  56. this.fitCenter(coordinate, padding, showPoint);
  57. }
  58. /**
  59. * 用 padding 调整点位在视图中的位置
  60. * padding: [上, 右, 下, 左],上边距越大,点位越靠下
  61. */
  62. fitCenter(coordinate, padding = this._padding, showPoint = this._showPoint) {
  63. if (!this.kmap || !coordinate) return;
  64. const center = [Number(coordinate[0]), Number(coordinate[1])];
  65. this.kmap.map?.updateSize?.();
  66. if (padding) {
  67. const extent = boundingExtent([center]);
  68. this.kmap.getView().fit(extent, {
  69. padding,
  70. maxZoom: 16,
  71. duration: 0,
  72. });
  73. } else {
  74. this.kmap.getView().setCenter(center);
  75. }
  76. if (showPoint !== false) {
  77. this.setMapPoint(center);
  78. }
  79. }
  80. setMapPosition(center, showPoint = this._showPoint) {
  81. if (!this.kmap) return;
  82. const nextCenter = [Number(center[0]), Number(center[1])];
  83. this._showPoint = showPoint;
  84. this.fitCenter(nextCenter, this._padding, showPoint);
  85. mapLocation.data = nextCenter;
  86. }
  87. /** 显示点位 */
  88. showPoint(coordinate) {
  89. this._showPoint = true;
  90. if (coordinate) {
  91. this.setMapPoint(coordinate);
  92. this.fitCenter(coordinate, this._padding, true);
  93. }
  94. }
  95. /** 清除点位(保留底图) */
  96. hidePoint() {
  97. this._showPoint = false;
  98. this.clearLayer();
  99. }
  100. setMapPoint(coordinate) {
  101. this.clickPointLayer.source.clear();
  102. const point = new Feature(new Point(coordinate));
  103. this.clickPointLayer.addFeature(point);
  104. }
  105. addMapSingerClick() {
  106. const that = this;
  107. that._clickKey = that.kmap.on("singleclick", (evt) => {
  108. that.setMapPoint(evt.coordinate);
  109. mapLocation.data = evt.coordinate;
  110. });
  111. }
  112. clearLayer() {
  113. this.clickPointLayer?.source?.clear();
  114. }
  115. }
  116. export default SelectLocationMap;