warningMap.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 Point from "ol/geom/Point.js";
  5. import Feature from "ol/Feature";
  6. import VectorLayer from "ol/layer/Vector.js";
  7. import WKT from "ol/format/WKT.js";
  8. import ScaleLine from "ol/control/ScaleLine";
  9. import { useRouter } from "vue-router";
  10. import Overlay from 'ol/Overlay'
  11. import eventBus from "@/api/eventBus";
  12. /**
  13. * @description 地图层对象
  14. */
  15. class HomeMap {
  16. constructor() {
  17. let that = this;
  18. let vectorStyle = new KMap.VectorStyle();
  19. this.vectorStyle = vectorStyle;
  20. that.address = ""
  21. }
  22. initMap(location, target) {
  23. let level = 7.2;
  24. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  25. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 7, 22);
  26. eventBus.emit('warningMap:init', this.kmap);
  27. this.addMapListen()
  28. // 添加比例尺控件
  29. const scaleLine = new ScaleLine({
  30. units: 'metric' // 可以选择 'imperial' 或 'metric'
  31. });
  32. this.kmap.addControl(scaleLine);
  33. }
  34. addMapListen() {
  35. let that = this
  36. // 监听地图点击事件
  37. // 创建弹窗图层
  38. this.popup = new Overlay({
  39. element: document.getElementById('popup'),
  40. positioning: 'right-center',
  41. offset: [10, 10],
  42. });
  43. this.kmap.map.addOverlay(this.popup);
  44. // 点击地图弹窗的关闭-销毁dom
  45. eventBus.on("map:destroyPopup", () => {
  46. that.popup.setPosition(undefined)
  47. })
  48. that.kmap.on('singleclick', function (event) {
  49. let hasFeature = false
  50. let feature = that.kmap.map.forEachFeatureAtPixel(event.pixel, function (feature, layer) {
  51. if (layer instanceof VectorLayer && layer.get("name") === "MockFarmLayer") {
  52. hasFeature = true
  53. let fs = feature.get("features");
  54. fs.length > 0 && eventBus.emit('MockFarmLayer:click', { sampleIdVal: fs[0].get("targetSampleId"), farmIdVal: fs[0].get("mockFarmId") });
  55. }
  56. if (layer instanceof VectorLayer && layer.get("name") === "pointLayer") {
  57. hasFeature = true
  58. eventBus.emit("chat:showTrackDialog", feature.get("url"))
  59. }
  60. return feature;
  61. });
  62. if (!hasFeature) {
  63. const currentLonLat = event.coordinate
  64. const params = {
  65. point: `POINT(${currentLonLat[0]} ${currentLonLat[1]})`
  66. }
  67. VE_API.mini_farm.weather_warning_land_check(params).then(async (res) => {
  68. await that.getLocation(currentLonLat)
  69. if (res.code == 0) {
  70. // 在这里可以获取feature的属性,并显示在弹窗中
  71. let content = '<div>' + res.data?.content + '</div>';
  72. document.getElementById('popup-title').innerHTML = that.address;
  73. document.getElementById('popup-content').innerHTML = content;
  74. that.popup.setPosition(event.coordinate); // 设置弹窗位置为点击位置
  75. } else {
  76. that.popup.setPosition(undefined)
  77. }
  78. })
  79. } else {
  80. that.popup.setPosition(undefined); // 如果没有点击到feature,则隐藏弹窗
  81. }
  82. })
  83. }
  84. //通过经纬度查处对应的地址
  85. async getLocation(point) {
  86. const locationParams = {
  87. key: "CZLBZ-LJICQ-R4A5J-BN62X-YXCRJ-GNBUT",
  88. location: `${point[1]},${point[0]}`,
  89. };
  90. await VE_API.warning.transformLocation(locationParams).then(({ result }) => {
  91. this.address = result.address_component.province + "-" + result.address_component.city + "-" + result.address_component.district
  92. console.log('address', this.address);
  93. });
  94. }
  95. }
  96. export default HomeMap;