regionLayer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.resetData()
  63. }
  64. this.colorArea = color
  65. this.area[index].set("bgName", "active");
  66. this.index = index
  67. }
  68. resetData(){
  69. this.area.forEach(item =>{
  70. item.set("bgName", "defalut");
  71. })
  72. }
  73. selectAreaMultiple(arr,color){
  74. this.resetData()
  75. this.colorArea = color
  76. arr.forEach(item =>{
  77. this.area[item].set("bgName", "active");
  78. })
  79. }
  80. initData(farmId) {
  81. let that = this;
  82. VE_API.region.listArea().then(({ data }) => {
  83. let features = [];
  84. for (let item of data) {
  85. let f = newRegionFeature({...item,bgName:"defalut"}, "wkt");
  86. features.push(f);
  87. }
  88. that.area = features
  89. const source = new VectorSource({
  90. features: features,
  91. });
  92. that.regionLayer.layer.setSource(source);
  93. });
  94. }
  95. reset(farm, region) {
  96. this.clearLayer();
  97. this.initData(farm.id);
  98. }
  99. // 清除聚合图层,解除绑定
  100. clearLayer() {
  101. if (this.regionLayer) {
  102. this.regionLayer.layer.getSource().clear();
  103. }
  104. }
  105. }
  106. export default RegionLayer;