warningMap.js 4.0 KB

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