samplePointLayer.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. VE_API.variety.pointList({farmId,regionId,selectAll}).then(({data})=>{
  95. let features = []
  96. for(let item of data){
  97. item.iconName='defalut'
  98. that.getIcon(item)
  99. let point = newPoint(item);
  100. features.push(point)
  101. }
  102. const source = new VectorSource({
  103. features: features,
  104. });
  105. that.clusterSource.setSource(source)
  106. setTimeout(()=>{
  107. that.mapRef.fit(that.clusterSource.source.getExtent(), {padding:[100,100,100,100]})
  108. },100)
  109. })
  110. }
  111. addMapSingerClick(kmap){
  112. let that = this
  113. kmap.on("singleclick", (evt) => {
  114. let hasFeature = false
  115. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  116. if (layer instanceof VectorLayer && layer.get("name") === "tree-cluster") {
  117. hasFeature = true
  118. if(that.curPoint){
  119. that.curPoint.set("iconName", "defalut");
  120. }
  121. const featureArr = feature.get("features")
  122. const fs = featureArr[0]
  123. that.curPoint = fs
  124. if(that.isUpdatePoint){
  125. fs.set("iconName", "active");
  126. }
  127. eventBus.emit("click:point",{farmId:fs.get("farmId"),sampleId:fs.get("sampleId"), data: fs.getProperties()})
  128. }
  129. if (layer instanceof VectorLayer && layer.get("name") === "yellow-block") {
  130. hasFeature = true
  131. eventBus.emit("click:yellowBlock",feature.get("geometry").flatCoordinates[0])
  132. }
  133. })
  134. if(!hasFeature){
  135. kmap.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  136. if (layer instanceof VectorLayer && layer.get("name") === "regionLayer") {
  137. hasFeature = false
  138. if(that.curArea){
  139. that.curArea.set("bgName", "defalut");
  140. }
  141. that.curArea = feature
  142. if(that.isUpdateArea){
  143. feature.set("bgName", "active");
  144. eventBus.emit("click:updateArea",{name:feature.get("id"),value:feature.get("blueZone")})
  145. }else{
  146. eventBus.emit("click:area",{name:feature.get("id"),value:feature.get("highYield")})
  147. }
  148. }
  149. })
  150. }
  151. })
  152. }
  153. resetPoint(){
  154. this.isUpdatePoint = null
  155. if(this.curPoint){
  156. this.curPoint.set("iconName", "defalut");
  157. }
  158. }
  159. updatePointStatus(e){
  160. this.isUpdatePoint = e
  161. }
  162. updateAreaStatus(e){
  163. this.isUpdateArea = e
  164. }
  165. getIcon(item){
  166. // let imgSrc = require(`@/assets/images/map/${item.iconName}-icon.png`)
  167. let imgSrc = require('@/assets/images/map/status/status-zc.png')
  168. let scale = 0.8
  169. if(item.status == 1){
  170. imgSrc = require('@/assets/images/map/status/status-szyc.png')
  171. }
  172. if(item.status == 2){
  173. imgSrc = require('@/assets/images/map/status/status-bh.png')
  174. }
  175. if(item.status == 3){
  176. imgSrc = require('@/assets/images/map/status/status-ch.png')
  177. }
  178. item["icon"] = imgSrc
  179. item["scale"] = scale
  180. }
  181. reset(farm, region){
  182. console.log('farm',farm);
  183. this.clearCluster()
  184. this.initData(farm.id, region.id)
  185. }
  186. // 清除聚合图层,解除绑定
  187. clearCluster() {
  188. if (this.treeClusterLayer) {
  189. this.treeClusterLayer.layer.getSource().getSource().clear()
  190. }
  191. }
  192. }
  193. export default SamplePointLayer;