samplePointLayer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 {unByKey} from "ol/Observable";
  11. import Style from "ol/style/Style";
  12. import Icon from "ol/style/Icon";
  13. import {Cluster, Vector as VectorSource} from "ol/source";
  14. import {newPoint} from "../../zhgl/map";
  15. import eventBus from "@/api/eventBus";
  16. /**
  17. * @description 地图层对象
  18. */
  19. class SamplePointLayer {
  20. constructor(map, farm, region) {
  21. let that = this;
  22. this.farmId = farm.id
  23. this.regionId = region.id
  24. let vectorStyle = new KMap.VectorStyle();
  25. this.vectorStyle = vectorStyle;
  26. this.clusterSource = new Cluster({
  27. distance: 15,
  28. minDistance: 60,
  29. });
  30. this.curPoint = null
  31. this.curArea = null
  32. this.treeClusterLayer = new KMap.VectorLayer("tree-cluster",999,{
  33. minZoom:15,
  34. source:this.clusterSource,
  35. style:(f)=> this.getStyle(f)})
  36. map.addLayer(this.treeClusterLayer.layer)
  37. this.initData(this.farmId, this.regionId)
  38. this.addMapSingerClick(map);
  39. }
  40. getIconStyle(feature){
  41. let style = new Style({
  42. image: new Icon({
  43. // src: feature.get('icon'),
  44. src: require(`@/assets/images/map/${feature.get('iconName')}-icon.png`),
  45. scale:0.3,
  46. })
  47. });
  48. return style
  49. }
  50. //多点的过滤方法
  51. manyFeatureFilter(features){
  52. let res = features[0]
  53. if(features.length == 1){
  54. return res
  55. }
  56. for(let item of features){
  57. res = res.get('status') > item.get('status') ? res : item
  58. }
  59. return res;
  60. }
  61. //得到点样式
  62. getStyle(feature){
  63. feature = this.manyFeatureFilter(feature.get('features'))
  64. return this.getIconStyle(feature)
  65. }
  66. initData(farmId, regionId){
  67. let that = this
  68. VE_API.sample.list({farmId,regionId}).then(({data})=>{
  69. // data[0].status = 9
  70. let features = []
  71. for(let item of data){
  72. item.iconName='defalut'
  73. that.getIcon(item)
  74. let point = newPoint(item);
  75. features.push(point)
  76. }
  77. const source = new VectorSource({
  78. features: features,
  79. });
  80. that.clusterSource.setSource(source)
  81. })
  82. }
  83. addMapSingerClick(kmap){
  84. let that = this
  85. kmap.on("singleclick", (evt) => {
  86. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  87. if (layer instanceof VectorLayer && layer.get("name") === "tree-cluster") {
  88. if(that.curPoint){
  89. that.curPoint.set("iconName", "defalut");
  90. }
  91. const featureArr = feature.get("features")
  92. const fs = featureArr[0]
  93. that.curPoint = fs
  94. // fs.set("iconName", "active");
  95. eventBus.emit("click:point",{name:fs.get("id"),value:fs.get("highYield")})
  96. }
  97. if (layer instanceof VectorLayer && layer.get("name") === "regionLayer") {
  98. if(that.curArea){
  99. that.curArea.set("bgName", "defalut");
  100. }
  101. feature.set("bgName", "active");
  102. that.curArea = feature
  103. eventBus.emit("click:area",{name:feature.get("id"),value:feature.get("highYield")})
  104. }
  105. })
  106. })
  107. }
  108. getIcon(item){
  109. // let imgSrc = require(`@/assets/images/map/${item.iconName}-icon.png`)
  110. // if(item.status == 9){
  111. // imgSrc = require('@/assets/images/map/active-icon.png')
  112. // }
  113. // if(item.status == 2){
  114. // imgSrc = require('@/assets/status/status_szyc.png')
  115. // }
  116. // if(item.status == 3){
  117. // imgSrc = require('@/assets/status/status_bcyc.png')
  118. // }
  119. // item["icon"] = imgSrc
  120. }
  121. ccc(){
  122. console.log('00000000');
  123. }
  124. reset(farm, region){
  125. console.log('farm',farm);
  126. this.clearCluster()
  127. this.initData(farm.id, region.id)
  128. }
  129. // 清除聚合图层,解除绑定
  130. clearCluster() {
  131. if (this.treeClusterLayer) {
  132. this.treeClusterLayer.layer.getSource().getSource().clear()
  133. }
  134. }
  135. }
  136. export default SamplePointLayer;