regionLayer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 { Circle as CircleStyle, Fill, Text } from "ol/style.js";
  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 { Vector as VectorSource } from "ol/source";
  14. import { newRegionFeature } from "../../zhgl/map";
  15. import Stroke from "ol/style/Stroke";
  16. /**
  17. * @description 地图层对象
  18. */
  19. class RegionLayer {
  20. constructor(map, farm) {
  21. let that = this;
  22. this.farmId = farm.id;
  23. this.vectorStyle = new KMap.VectorStyle();
  24. this.area = null
  25. this.index = null
  26. const colorObj = {
  27. "defalut":["#fba50410","#eee5e5"],
  28. "blue":["#2199f894","#2199f894"],
  29. "blue1":["#2199f804","#2199f804"],
  30. "blue2":["#2199f87a","#2199f87a"],
  31. "red":["#b8150094","#b8150094"],
  32. "red1":["#FF733F94","#FF733F94"],
  33. "red2":["#FFA96C94","#FFA96C94"],
  34. "red3":["#FF26267a","#FF26267a"],
  35. "green":["#006f0b94","#006f0b94"],
  36. "green1":["#32b81a94","#32b81a94"],
  37. "green2":["#B7FFAA94","#B7FFAA94"]
  38. }
  39. this.regionLayer = new KMap.VectorLayer("regionLayer", 99, {
  40. minZoom: 15,
  41. style: (f) => {
  42. const color = colorObj[f.get("bgColor")]
  43. const style1 = this.vectorStyle.getPolygonStyle(color[0], color[1], 2);
  44. // let style2 = null
  45. // if(f.get("id")==0){
  46. // style2 = new Style({
  47. // image: new Icon({
  48. // src: require("@/assets/images/map/yellow-block.png"),
  49. // scale: 0.4,
  50. // }),
  51. // // text: new Text({
  52. // // text: f.get("id"),
  53. // // color: "#120046",
  54. // // stroke: new Stroke({
  55. // // color: "#FFFFFF",
  56. // // width: 2,
  57. // // }),
  58. // // backgroundFill: new Fill({
  59. // // color: "#f8f9fa10",
  60. // // width: 2,
  61. // // }),
  62. // // font: "30px sans-serif",
  63. // // }),
  64. // });
  65. // }
  66. return [style1];
  67. },
  68. });
  69. map.addLayer(this.regionLayer.layer);
  70. this.initData(this.farmId);
  71. }
  72. //得到点样式
  73. getStyle(feature) {
  74. return this.getIconStyle(feature);
  75. }
  76. selectArea(index,color){
  77. if(this.index !=null){
  78. this.resetData()
  79. }
  80. this.area[index].set("bgColor", color);
  81. this.area[index].set("bgName", "active");
  82. this.index = index
  83. }
  84. resetData(){
  85. this.area.forEach(item =>{
  86. item.set("bgName", "defalut");
  87. })
  88. }
  89. selectAreaMultiple(arr){
  90. this.resetData()
  91. arr.forEach(item =>{
  92. this.area[item.value].set("bgColor", item.color);
  93. this.area[item.value].set("bgName", "active");
  94. })
  95. }
  96. initData(farmId) {
  97. let that = this;
  98. VE_API.region.listArea().then(({ data }) => {
  99. let features = [];
  100. for (let item of data) {
  101. let f = newRegionFeature({...item,bgName:"defalut",bgColor:"defalut"}, "wkt");
  102. features.push(f);
  103. }
  104. features.push(
  105. newRegionFeature({
  106. highYield:1,
  107. id:"0",
  108. blueZone:"0",
  109. dateValue:"",
  110. bgName:"defalut",
  111. bgColor:"red3",
  112. wkt:"MULTIPOLYGON (((113.61390710255375 23.586379215663726,113.6140224799741 23.586530760891492,113.61429532483612 23.586727463001182,113.61471252366596 23.587019343551333,113.6151313088028 23.587376262702094,113.61544063873373 23.587220804583183,113.61557230224264 23.58706376015681,113.61544222504097 23.586978099560497,113.6151614486422 23.586913060959716,113.61506827952462 23.586818940057697,113.61491218688275 23.58671519555776,113.61477512992872 23.58657909038834,113.61452195527784 23.586449647709514,113.61430590021848 23.586303072911505,113.61396420961808 23.58621741231542,113.61390710255375 23.586379215663726)))"
  113. },"wkt")
  114. )
  115. that.area = features
  116. const source = new VectorSource({
  117. features: features,
  118. });
  119. that.regionLayer.layer.setSource(source);
  120. });
  121. }
  122. reset(farm, region) {
  123. this.clearLayer();
  124. this.initData(farm.id);
  125. }
  126. // 清除聚合图层,解除绑定
  127. clearLayer() {
  128. if (this.regionLayer) {
  129. this.regionLayer.layer.getSource().clear();
  130. }
  131. }
  132. }
  133. export default RegionLayer;