regionLayer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "@/utils/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. "red4":["#ff262663","#ff262663"]
  39. }
  40. this.regionLayer = new KMap.VectorLayer("regionLayer", 99, {
  41. minZoom: 15,
  42. style: (f) => {
  43. const color = colorObj[f.get("bgColor")]
  44. const style1 = this.vectorStyle.getPolygonStyle(color[0], color[1], 2);
  45. // let style2 = null
  46. // if(f.get("id")==0){
  47. // style2 = new Style({
  48. // image: new Icon({
  49. // src: require("@/assets/images/map/yellow-block.png"),
  50. // scale: 0.4,
  51. // }),
  52. // // text: new Text({
  53. // // text: f.get("id"),
  54. // // color: "#120046",
  55. // // stroke: new Stroke({
  56. // // color: "#FFFFFF",
  57. // // width: 2,
  58. // // }),
  59. // // backgroundFill: new Fill({
  60. // // color: "#f8f9fa10",
  61. // // width: 2,
  62. // // }),
  63. // // font: "30px sans-serif",
  64. // // }),
  65. // });
  66. // }
  67. return [style1];
  68. },
  69. });
  70. map.addLayer(this.regionLayer.layer);
  71. this.initData(this.farmId);
  72. }
  73. //得到点样式
  74. getStyle(feature) {
  75. return this.getIconStyle(feature);
  76. }
  77. selectArea(index,color){
  78. if(this.index !=null){
  79. this.resetData()
  80. }
  81. this.area[index].set("bgColor", color);
  82. this.area[index].set("bgName", "active");
  83. this.index = index
  84. }
  85. resetData(){
  86. this.area.forEach(item =>{
  87. item.set("bgName", "defalut");
  88. item.set("bgColor", "defalut");
  89. })
  90. }
  91. selectAreaMultiple(arr){
  92. this.resetData()
  93. arr.forEach(item =>{
  94. this.area[item.value].set("bgColor", item.color);
  95. this.area[item.value].set("bgName", "active");
  96. })
  97. }
  98. initData(farmId) {
  99. let that = this;
  100. VE_API.region.listArea().then(({ data }) => {
  101. let features = [];
  102. for (let item of data) {
  103. let f = newRegionFeature({...item,bgName:"defalut",bgColor:"defalut"}, "wkt");
  104. features.push(f);
  105. }
  106. features.push(
  107. newRegionFeature({
  108. highYield:1,
  109. id:"0",
  110. blueZone:"0",
  111. dateValue:"",
  112. bgName:"defalut",
  113. bgColor:"red4",
  114. 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)))"
  115. },"wkt")
  116. )
  117. that.area = features
  118. const source = new VectorSource({
  119. features: features,
  120. });
  121. that.regionLayer.layer.setSource(source);
  122. });
  123. }
  124. reset(farm, region) {
  125. this.clearLayer();
  126. this.initData(farm.id);
  127. }
  128. // 清除聚合图层,解除绑定
  129. clearLayer() {
  130. if (this.regionLayer) {
  131. this.regionLayer.layer.getSource().clear();
  132. }
  133. }
  134. }
  135. export default RegionLayer;