selectLocationMap.js 3.8 KB

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