regionLayer.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 ScaleLine from "ol/control/ScaleLine";
  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. /**
  16. * @description 地图层对象
  17. */
  18. class RegionLayer {
  19. constructor(map, farm) {
  20. let that = this;
  21. this.farmId = farm.id
  22. this.vectorStyle = new KMap.VectorStyle();
  23. this.regionLayer = new KMap.VectorLayer("regionLayer",999,{
  24. minZoom:15,
  25. source:new VectorSource(),
  26. style:(f)=> this.getStyle(f)})
  27. map.addLayer(this.regionLayer.layer)
  28. this.initData(this.farmId)
  29. }
  30. getIconStyle(feature){
  31. let style = new Style({
  32. image: new Icon({
  33. src: feature.get('icon'),
  34. scale:1,
  35. })
  36. });
  37. return style
  38. }
  39. //得到点样式
  40. getStyle(feature){
  41. return this.getIconStyle(feature)
  42. }
  43. initData(farmId){
  44. let that = this
  45. VE_API.sample.list({farmId}).then(({data})=>{
  46. let features = []
  47. for(let item of data){
  48. that.getIcon(item)
  49. let f = newRegionFeature(item);
  50. features.push(f)
  51. }
  52. const source = new VectorSource({
  53. features: features,
  54. });
  55. that.regionLayer.setSource(source)
  56. })
  57. }
  58. reset(farm, region){
  59. this.clearCluster()
  60. this.initData(farm.id, region.id)
  61. }
  62. // 清除聚合图层,解除绑定
  63. clearLayer() {
  64. if (this.regionLayer) {
  65. this.regionLayer.layer.getSource().clear()
  66. }
  67. }
  68. }
  69. export default RegionLayer;