warningMap.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import * as KMap from "@/utils/ol-map/KMap";
  2. import * as util from "@/common/ol_common.js";
  3. import VectorLayer from "ol/layer/Vector.js";
  4. import ScaleLine from "ol/control/ScaleLine";
  5. import Overlay from "ol/Overlay";
  6. import eventBus from "@/api/eventBus";
  7. import Style from "ol/style/Style";
  8. /**
  9. * @description 地图层对象
  10. */
  11. class HomeMap {
  12. constructor() {
  13. let that = this;
  14. let vectorStyle = new KMap.VectorStyle();
  15. this.vectorStyle = vectorStyle;
  16. that.address = "";
  17. }
  18. initMap(location, target) {
  19. let level = 9;
  20. // 验证 location 是否为有效的 WKT 字符串
  21. let coordinate = [113.8, 23.1]; // 默认坐标(东莞附近)
  22. if (location && typeof location === 'string' && location.trim().length > 0) {
  23. try {
  24. const geom = util.wktCastGeom(location);
  25. if (geom) {
  26. coordinate = geom.getFirstCoordinate();
  27. }
  28. } catch (error) {
  29. console.warn('解析 location WKT 失败,使用默认坐标:', error, location);
  30. }
  31. } else {
  32. console.warn('location 为空或无效,使用默认坐标');
  33. }
  34. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 7, 22);
  35. // 开启地图点击事件监听,使点位点击可以触发弹窗
  36. this.addMapListen();
  37. // 添加比例尺控件
  38. const scaleLine = new ScaleLine({
  39. units: 'metric' // 可以选择 'imperial' 或 'metric'
  40. });
  41. this.kmap.addControl(scaleLine);
  42. }
  43. addMapListen() {
  44. let that = this
  45. // 监听地图点击事件
  46. // 创建弹窗图层
  47. this.popup = new Overlay({
  48. element: document.getElementById('popup'),
  49. positioning: 'right-center',
  50. offset: [10, 10],
  51. });
  52. this.kmap.map.addOverlay(this.popup);
  53. // 点击地图弹窗的关闭-销毁dom
  54. eventBus.on("map:destroyPopup", () => {
  55. that.popup.setPosition(undefined)
  56. })
  57. that.kmap.on('singleclick', function (event) {
  58. let hasFeature = false
  59. let feature = that.kmap.map.forEachFeatureAtPixel(event.pixel, function (feature, layer) {
  60. if (layer instanceof VectorLayer && layer.get("name") === "MockFarmLayer") {
  61. hasFeature = true
  62. let fs = feature.get("features");
  63. fs.length > 0 && eventBus.emit('MockFarmLayer:click', { sampleIdVal: fs[0].get("targetSampleId"), farmIdVal: fs[0].get("mockFarmId") });
  64. }
  65. if (layer instanceof VectorLayer && layer.get("name") === "pointLayer") {
  66. hasFeature = true
  67. eventBus.emit("chat:showTrackDialog", feature.get("url"))
  68. }
  69. if (layer instanceof VectorLayer && layer.get("name") === "imgLayer") {
  70. hasFeature = true
  71. eventBus.emit("chat:showImgDialog", feature.get("imageUrl"))
  72. }
  73. return feature;
  74. });
  75. if (!hasFeature) {
  76. const currentLonLat = event.coordinate
  77. const params = {
  78. point: `POINT(${currentLonLat[0]} ${currentLonLat[1]})`
  79. }
  80. // VE_API.mini_farm.weather_warning_land_check(params).then(async (res) => {
  81. // await that.getLocation(currentLonLat)
  82. // if (res.code == 0) {
  83. // // 在这里可以获取feature的属性,并显示在弹窗中
  84. // let content = '<div>' + res.data?.content + '</div>';
  85. // document.getElementById('popup-title').innerHTML = that.address;
  86. // document.getElementById('popup-content').innerHTML = content;
  87. // that.popup.setPosition(event.coordinate); // 设置弹窗位置为点击位置
  88. // } else {
  89. // that.popup.setPosition(undefined)
  90. // }
  91. // })
  92. } else {
  93. that.popup.setPosition(undefined); // 如果没有点击到feature,则隐藏弹窗
  94. }
  95. })
  96. }
  97. //通过经纬度查处对应的地址
  98. async getLocation(point) {
  99. const locationParams = {
  100. key: "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT",
  101. location: `${point[1]},${point[0]}`,
  102. };
  103. await VE_API.warning.transformLocation(locationParams).then(({ result }) => {
  104. this.address = result.address_component.province + "-" + result.address_component.city + "-" + result.address_component.district
  105. console.log('address', this.address);
  106. });
  107. }
  108. // 设置地图中心点位
  109. setMapCenter(v, isPoint = true) {
  110. let arrayOfNumbers = [];
  111. const arrayOfStrings = v.split(",");
  112. arrayOfNumbers = [arrayOfStrings[1], arrayOfStrings[0]];
  113. this.kmap.map.getView().setCenter(arrayOfNumbers);
  114. if (!isPoint) return
  115. // this.locationLayer.source.clear();
  116. // let point = new Feature(new Point(arrayOfNumbers));
  117. // this.locationLayer.addFeature(point);
  118. }
  119. }
  120. export default HomeMap;