samplePointLayer.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 "@/utils/map";
  15. import eventBus from "@/api/eventBus";
  16. /**
  17. * @description 地图层对象
  18. */
  19. class SamplePointLayer {
  20. constructor(map, organId, regionId) {
  21. let that = this;
  22. this.farmId = organId
  23. this.regionId = regionId
  24. let vectorStyle = new KMap.VectorStyle();
  25. this.vectorStyle = vectorStyle;
  26. this.clusterSource = new Cluster({
  27. distance: 15,
  28. minDistance: 60,
  29. });
  30. this.mapRef = map
  31. this.curPoint = null
  32. this.curArea = null
  33. this.isUpdatePoint = null
  34. this.isUpdateArea = null
  35. this.treeClusterLayer = new KMap.VectorLayer("tree-cluster",999,{
  36. minZoom:15,
  37. source:this.clusterSource,
  38. style:(f)=> this.getStyle(f)})
  39. this.yellowBlockLayer = new KMap.VectorLayer("yellow-block",999,{
  40. minZoom:15,
  41. // source:this.clusterSource,
  42. // source:"POINT(113.61396985128522 23.5859386716038)",
  43. style: () => {
  44. return new Style({
  45. image: new Icon({
  46. src: require("@/assets/images/map/yellow-block.png"),
  47. scale: 0.4,
  48. }),
  49. });
  50. },
  51. })
  52. map.addLayer(this.treeClusterLayer.layer)
  53. // map.addLayer(this.yellowBlockLayer.layer);
  54. let point = new Feature(new Point([113.61396985128522,23.5859386716038]));
  55. let point1 = new Feature(new Point([113.61390710255375 ,23.586379215663726]));
  56. let point2 = new Feature(new Point([113.61491218688275 ,23.58671519555776]));
  57. // this.yellowBlockLayer.addFeature(point);
  58. // this.yellowBlockLayer.addFeature(point1);
  59. // this.yellowBlockLayer.addFeature(point2);
  60. this.addMapSingerClick(map.map);
  61. }
  62. getIconStyle(feature){
  63. let style = new Style({
  64. image: new Icon({
  65. src: feature.get('icon'),
  66. // src: require(`@/assets/images/map/${feature.get('iconName')}-icon.png`),
  67. scale:feature.get('scale'),
  68. })
  69. });
  70. return style
  71. }
  72. //多点的过滤方法
  73. manyFeatureFilter(features){
  74. let res = features[0]
  75. if(features.length == 1){
  76. return res
  77. }
  78. for(let item of features){
  79. res = res.get('status') > item.get('status') ? res : item
  80. }
  81. return res;
  82. }
  83. //得到点样式
  84. getStyle(feature){
  85. feature = this.manyFeatureFilter(feature.get('features'))
  86. return this.getIconStyle(feature)
  87. }
  88. initData(farmId, regionId){
  89. let that = this
  90. let selectAll = undefined
  91. if(regionId===0){
  92. selectAll = 1
  93. }
  94. const areaId = selectAll?undefined:regionId
  95. VE_API.variety.pointList({farmId,regionId:areaId,selectAll}).then(({data})=>{
  96. let features = []
  97. for(let item of data){
  98. item.iconName='defalut'
  99. that.getIcon(item)
  100. let point = newPoint(item);
  101. features.push(point)
  102. }
  103. const source = new VectorSource({
  104. features: features,
  105. });
  106. that.clusterSource.setSource(source)
  107. setTimeout(()=>{
  108. that.mapRef.fit(that.clusterSource.source.getExtent(), {padding:[100,100,100,100]})
  109. },100)
  110. })
  111. }
  112. addMapSingerClick(kmap){
  113. let that = this
  114. kmap.on("singleclick", (evt) => {
  115. let hasFeature = false
  116. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  117. if (layer instanceof VectorLayer && layer.get("name") === "tree-cluster") {
  118. hasFeature = true
  119. if(that.curPoint){
  120. that.curPoint.set("iconName", "defalut");
  121. }
  122. const featureArr = feature.get("features")
  123. const fs = featureArr[0]
  124. that.curPoint = fs
  125. if(that.isUpdatePoint){
  126. fs.set("iconName", "active");
  127. }
  128. eventBus.emit("click:point",{farmId:fs.get("farmId"),sampleId:fs.get("sampleId"), data: fs.getProperties()})
  129. }
  130. if (layer instanceof VectorLayer && layer.get("name") === "yellow-block") {
  131. hasFeature = true
  132. eventBus.emit("click:yellowBlock",feature.get("geometry").flatCoordinates[0])
  133. }
  134. })
  135. if(!hasFeature){
  136. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  137. if (layer instanceof VectorLayer && layer.get("name") === "regionLayer") {
  138. hasFeature = false
  139. if(that.curArea){
  140. that.curArea.set("bgName", "defalut");
  141. }
  142. that.curArea = feature
  143. if(that.isUpdateArea){
  144. feature.set("bgName", "active");
  145. eventBus.emit("click:updateArea",{name:feature.get("id"),value:feature.get("blueZone")})
  146. }else{
  147. eventBus.emit("click:area",{name:feature.get("id"),value:feature.get("highYield")})
  148. }
  149. }
  150. })
  151. }
  152. })
  153. }
  154. resetPoint(){
  155. this.isUpdatePoint = null
  156. if(this.curPoint){
  157. this.curPoint.set("iconName", "defalut");
  158. }
  159. }
  160. updatePointStatus(e){
  161. this.isUpdatePoint = e
  162. }
  163. updateAreaStatus(e){
  164. this.isUpdateArea = e
  165. }
  166. getIcon(item){
  167. // let imgSrc = require(`@/assets/images/map/${item.iconName}-icon.png`)
  168. let imgSrc = require('@/assets/images/map/status/status-zc.png')
  169. let scale = 0.8
  170. if(item.status == 1){
  171. imgSrc = require('@/assets/images/map/status/status-szyc.png')
  172. }
  173. if(item.status == 2){
  174. imgSrc = require('@/assets/images/map/status/status-bh.png')
  175. }
  176. if(item.status == 3){
  177. imgSrc = require('@/assets/images/map/status/status-ch.png')
  178. }
  179. item["icon"] = imgSrc
  180. item["scale"] = scale
  181. }
  182. reset(farm, region){
  183. console.log('farm',farm);
  184. this.clearCluster()
  185. this.initData(farm.id, region.id)
  186. }
  187. // 清除聚合图层,解除绑定
  188. clearCluster() {
  189. if (this.treeClusterLayer) {
  190. this.treeClusterLayer.layer.getSource().getSource().clear()
  191. }
  192. }
  193. }
  194. export default SamplePointLayer;