pdfMap.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import config from "@/api/config.js";
  2. import * as KMap from "@/utils/ol-map/KMap";
  3. import * as util from "@/common/ol_common.js";
  4. import Style from "ol/style/Style";
  5. import Icon from "ol/style/Icon";
  6. import VectorLayer from "ol/layer/Vector.js";
  7. import WKT from "ol/format/WKT.js";
  8. import { reactive } from "vue";
  9. import Point from "ol/geom/Point.js";
  10. import Feature from "ol/Feature";
  11. import { newPoint } from "@/utils/map.js";
  12. import { Fill, Text } from "ol/style";
  13. import { getArea } from "ol/sphere.js";
  14. import * as proj from "ol/proj";
  15. import proj4 from "proj4";
  16. import { register } from "ol/proj/proj4";
  17. import {DragPan, MouseWheelZoom} from "ol/interaction";
  18. let mapData = reactive({
  19. isEdit: false,
  20. isEditArea: false,
  21. curPointData: {},
  22. point: null,
  23. selectPointArr: [],
  24. isPointHide: false,
  25. });
  26. proj4.defs(
  27. "EPSG:38572",
  28. "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs"
  29. );
  30. register(proj4);
  31. /**
  32. * @description 地图层对象
  33. */
  34. class PdfMap {
  35. constructor() {
  36. let that = this;
  37. let vectorStyle = new KMap.VectorStyle();
  38. this.vectorStyle = vectorStyle;
  39. // 位置图标
  40. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  41. style: (f) => {
  42. const style1 = new Style({
  43. image: new Icon({
  44. src: require(`@/assets/images/map/${f.get("icon")}-icon.png`),
  45. scale: 0.45,
  46. }),
  47. });
  48. const style2 = new Style({
  49. text: new Text({
  50. font: "16px sans-serif",
  51. text: f.get("masterName"),
  52. offsetY: -40,
  53. padding: [4, 3, 2, 106],
  54. fill: new Fill({ color: "#fff" }), // 字体颜色
  55. }),
  56. });
  57. const style3 = new Style({
  58. image: new Icon({
  59. src: require(`@/assets/images/map/${f.get("iconBg")}.png`),
  60. scale: 0.45,
  61. displacement: [0, 90],
  62. }),
  63. });
  64. return [style1, style2, style3];
  65. },
  66. });
  67. this.locationLayer = new KMap.VectorLayer("locationLayer", 9999, {
  68. style: () => {
  69. return new Style({
  70. image: new Icon({
  71. src: require("@/assets/images/map/location.png"),
  72. scale: 0.45,
  73. }),
  74. });
  75. },
  76. });
  77. }
  78. initMap(data, target) {
  79. let level = 16;
  80. let coordinate = util.wktCastGeom(data.point).getFirstCoordinate();
  81. this.kmap = new KMap.Map(
  82. target,
  83. level,
  84. coordinate[0],
  85. coordinate[1],
  86. null,
  87. 6,
  88. 22
  89. );
  90. this.lock(false)
  91. this.kmap.setLayerWkt(data.geom, data, true, [50, 50, 50, 50] )
  92. this.kmap.addLayer(this.clickPointLayer.layer);
  93. this.kmap.addLayer(this.locationLayer.layer);
  94. this.addPoint(data)
  95. this.getSelectPointArr(data.id)
  96. }
  97. getSelectPointArr(id) {
  98. const arr = [];
  99. this.clickPointLayer.source.forEachFeature((feature) => {
  100. if (feature.get("id") === id) {
  101. // 修改当前点位高亮
  102. feature.set("icon", "point");
  103. feature.set("iconBg", "name-act-bg");
  104. mapData.point = feature;
  105. mapData.curPointData = feature.values_;
  106. }
  107. arr.push(feature);
  108. });
  109. const points = this.kmap.getLayerFeatures();
  110. points.forEach((feature) => {
  111. if (feature.get("id") === id) {
  112. feature.set("icon", "point-act");
  113. this.kmap.polygonStyle(feature);
  114. mapData.isPointHide = feature;
  115. }
  116. });
  117. mapData.selectPointArr = arr;
  118. }
  119. // 添加点位
  120. addPoint(point) {
  121. const arrPoints = [];
  122. arrPoints.push(
  123. newPoint({ ...point, icon: "point", iconBg: "name-bg" }, "point")
  124. );
  125. this.clickPointLayer.source.addFeatures(arrPoints);
  126. }
  127. lock(lockval) {
  128. let pan;
  129. let mousezoom;
  130. this.kmap.map.getInteractions().forEach(function (element) {
  131. if (element instanceof DragPan)//获取 控制能否使用鼠标,手指拖动地图的对象
  132. pan = element;
  133. if(element instanceof MouseWheelZoom)//获取 控制能否使用滚轮滚动放大缩小地图的对象
  134. mousezoom = element;
  135. if (pan) {
  136. if (lockval) {
  137. pan.setActive(true);//此对象的setActive方法用来设置是否可以拖动滚动查看
  138. }
  139. else {
  140. pan.setActive(false);
  141. }
  142. }
  143. if (mousezoom) {
  144. if (lockval) {
  145. mousezoom.setActive(true);
  146. }
  147. else {
  148. mousezoom.setActive(false);
  149. }
  150. }
  151. });
  152. }
  153. }
  154. export default PdfMap;