regionLayer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. this.colorArea = ["#fba50410","#eee5e5"]
  27. this.regionLayer = new KMap.VectorLayer("regionLayer", 99, {
  28. minZoom: 15,
  29. style: (f) => {
  30. let style1
  31. style1 = this.vectorStyle.getPolygonStyle("#fba50410", "#eee5e5", 2);
  32. if(f.get("bgName")==="active"){
  33. style1 = this.vectorStyle.getPolygonStyle(this.colorArea[0], this.colorArea[1], 2);
  34. }
  35. const style2 = new Style({
  36. text: new Text({
  37. text: f.get("id"),
  38. color: "#120046",
  39. stroke: new Stroke({
  40. color: "#FFFFFF",
  41. width: 2,
  42. }),
  43. backgroundFill: new Fill({
  44. color: "#f8f9fa10",
  45. width: 2,
  46. }),
  47. font: "30px sans-serif",
  48. }),
  49. });
  50. return [style1];
  51. },
  52. });
  53. map.addLayer(this.regionLayer.layer);
  54. this.initData(this.farmId);
  55. }
  56. //得到点样式
  57. getStyle(feature) {
  58. return this.getIconStyle(feature);
  59. }
  60. selectArea(index,color){
  61. if(this.index !=null){
  62. this.area[this.index].set("bgName", "defalut");
  63. }
  64. this.colorArea = color
  65. this.area[index].set("bgName", "active");
  66. this.index = index
  67. }
  68. selectAreaMultiple(arr,color){
  69. arr.forEach(item =>{
  70. console.log('item',item);
  71. this.area[item].set("bgName", "active");
  72. })
  73. this.colorArea = color
  74. }
  75. initData(farmId) {
  76. let that = this;
  77. VE_API.region.listArea().then(({ data }) => {
  78. let features = [];
  79. for (let item of data) {
  80. let f = newRegionFeature({...item,bgName:"defalut"}, "wkt");
  81. features.push(f);
  82. }
  83. that.area = features
  84. const source = new VectorSource({
  85. features: features,
  86. });
  87. that.regionLayer.layer.setSource(source);
  88. });
  89. }
  90. reset(farm, region) {
  91. this.clearLayer();
  92. this.initData(farm.id);
  93. }
  94. // 清除聚合图层,解除绑定
  95. clearLayer() {
  96. if (this.regionLayer) {
  97. this.regionLayer.layer.getSource().clear();
  98. }
  99. }
  100. }
  101. export default RegionLayer;