regionLayer.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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, newPoint} from "@/utils/map";
  15. import Stroke from "ol/style/Stroke";
  16. /**
  17. * @description 地图层对象
  18. */
  19. class RegionLayer {
  20. constructor(map) {
  21. let that = this;
  22. this.vectorStyle = new KMap.VectorStyle();
  23. this.area = null
  24. this.index = null
  25. this.regionLayer = new KMap.VectorLayer("regionLayer", 99, {
  26. minZoom: 15,
  27. style: (f) => {
  28. const style = new Style({
  29. fill: new Fill({
  30. color: "#0000004d",
  31. }),
  32. stroke: new Stroke({
  33. color: "#FFE17E80", // 边框
  34. width: 2,
  35. }),
  36. });
  37. return [style];
  38. },
  39. });
  40. this.gardenPointLayer = new KMap.VectorLayer("gardenUserLayer", 100, {
  41. minZoom: 15,
  42. maxZoom: 22,
  43. source: new VectorSource({}),
  44. style: (feature) => {
  45. let style1 = new Style({
  46. image: new Icon({
  47. src: require("@/assets/images/foster-home/area-bg.png"),
  48. scale: 1,
  49. // displacement: [70, 25], // 向右30px,向上30px
  50. }),
  51. text: new Text({
  52. text: feature.get("name"), // 显示的文字
  53. font: "30px PangMenZhengDao",
  54. fill: new Fill({
  55. color: "#FFFFFF", // 文字颜色
  56. }),
  57. stroke: new Stroke({
  58. color: "#000", // 文字描边
  59. width: 1,
  60. }),
  61. }),
  62. });
  63. return [style1];
  64. },
  65. });
  66. map.addLayer(this.regionLayer.layer);
  67. map.addLayer(this.gardenPointLayer.layer);
  68. this.initData(this.farmId);
  69. }
  70. //得到点样式
  71. getStyle(feature) {
  72. return this.getIconStyle(feature);
  73. }
  74. selectArea(index,color){
  75. if(this.index !=null){
  76. this.resetData()
  77. }
  78. this.area[index].set("bgColor", color);
  79. this.area[index].set("bgName", "active");
  80. this.index = index
  81. }
  82. resetData(){
  83. this.area.forEach(item =>{
  84. item.set("bgName", "defalut");
  85. item.set("bgColor", "defalut");
  86. })
  87. }
  88. selectAreaMultiple(arr){
  89. this.resetData()
  90. arr.forEach(item =>{
  91. this.area[item.value].set("bgColor", item.color);
  92. this.area[item.value].set("bgName", "active");
  93. })
  94. }
  95. initData(data) {
  96. console.log('da', data);
  97. if (!data) return
  98. let that = this;
  99. let features = [];
  100. for (let item of data) {
  101. item.regionWkt = item.wkt
  102. let f = newRegionFeature({...item,bgName:"defalut",bgColor:"defalut"}, "wkt");
  103. features.push(f);
  104. item.wkt = item.pointWkt
  105. this.gardenPointLayer.source.addFeature(newPoint(item, "wktVal", "myGarden"))
  106. }
  107. that.area = features
  108. const source = new VectorSource({
  109. features: features,
  110. });
  111. that.regionLayer.layer.setSource(source);
  112. }
  113. reset(farm, region) {
  114. this.clearLayer();
  115. this.initData(farm.id);
  116. }
  117. // 清除聚合图层,解除绑定
  118. clearLayer() {
  119. if (this.regionLayer) {
  120. this.regionLayer.layer.getSource().clear();
  121. }
  122. }
  123. }
  124. export default RegionLayer;