areaMap.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import * as KMap from "@/utils/ol-map/KMap";
  2. import * as util from "@/common/ol_common.js";
  3. import config from "@/api/config.js";
  4. import { Vector as VectorSource } from "ol/source.js";
  5. import { Point } from 'ol/geom';
  6. import { newPoint, newAreaFeature } from "@/utils/map";
  7. import { GeoJSON, WKT } from 'ol/format'
  8. import { Feature } from "ol";
  9. import { getArea } from "ol/sphere"
  10. import * as turf from "@turf/turf"
  11. import Style from "ol/style/Style";
  12. import Icon from "ol/style/Icon";
  13. import { Fill, Text, Stroke } from "ol/style";
  14. import * as proj from "ol/proj";
  15. import proj4 from "proj4"
  16. import { register } from "ol/proj/proj4";
  17. proj4.defs("EPSG:38572", "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs +type=crs");
  18. register(proj4);
  19. /**
  20. *
  21. */
  22. class AreaMap {
  23. constructor() {
  24. let that = this;
  25. let vectorStyle = new KMap.VectorStyle()
  26. this.gardenPolygonLayer = new KMap.VectorLayer("gardenPolygonLayer", 999, {
  27. minZoom: 11,
  28. maxZoom: 22,
  29. source: new VectorSource({}),
  30. style: function (f) {
  31. let style2 = vectorStyle.getPolygonStyle("#00000080", "#2199F8", 2)
  32. let style3 = new Style({
  33. text: new Text({
  34. font: "14px sans-serif",
  35. text: f.get("mianji") + "亩",
  36. // offsetX: 28,
  37. offsetY: 10,
  38. fill: new Fill({ color: "#fff" }), // 字体颜色
  39. }),
  40. });
  41. return [style2]
  42. }
  43. });
  44. // 位置图标
  45. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  46. style: (f) => {
  47. let pointIcon = new Style({
  48. image: new Icon({
  49. src: require("@/assets/img/home/garden-point.png"),
  50. scale: 0.5,
  51. anchor: [0.5, 1],
  52. }),
  53. });
  54. let nameText = new Style({
  55. text: new Text({
  56. font: "14px sans-serif",
  57. text: f.get("name"),
  58. offsetY: 10,
  59. fill: new Fill({ color: "#fff" }), // 字体颜色
  60. stroke: new Stroke({
  61. color: "#000",
  62. width: 0.5,
  63. }),
  64. }),
  65. });
  66. return [pointIcon, nameText]
  67. },
  68. });
  69. }
  70. initMap(location, target) {
  71. let level = 16;
  72. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  73. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 8, 22);
  74. let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
  75. this.kmap.addXYZLayer(xyz2, { minZoom: 8, maxZoom: 22 }, 2);
  76. this.kmap.addLayer(this.gardenPolygonLayer.layer);
  77. this.kmap.addLayer(this.clickPointLayer.layer);
  78. }
  79. initLayer(item) {
  80. // this.gardenPolygonLayer.refresh()
  81. if (this.gardenPolygonLayer.source) {
  82. this.gardenPolygonLayer.source.clear()
  83. }
  84. if (item.pointWkt) {
  85. let f = newPoint(item, "pointWkt")
  86. f.set("name", item.name)
  87. this.clickPointLayer.source.addFeature(f)
  88. }
  89. console.log('initLayer', item)
  90. if (item.geomWkt) {
  91. // let f = new Feature({
  92. // organId: item.organId, // 用于查找点击选中地块的编辑,有多个地块时用id筛选
  93. // geometry: new WKT().readGeometry(item.geomWkt)
  94. // })
  95. // let geometry = new WKT().readGeometry(item.geomWkt)
  96. // geometry.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"))
  97. let f = newAreaFeature(item, "geomWkt")
  98. // let area = getArea(geometry)
  99. // area = (area + area / 2) / 1000;
  100. f.set("mianji", item.mianji)
  101. this.gardenPolygonLayer.source.addFeature(f)
  102. }
  103. this.fitView()
  104. }
  105. /**
  106. * 调整地图视图以适应地块范围
  107. */
  108. fitView() {
  109. let extent = this.gardenPolygonLayer.source.getExtent()
  110. if (extent) {
  111. // 地图自适应到区域可视范围
  112. this.kmap.getView().fit(extent, { duration: 50, padding: [80, 80, 80, 80] });
  113. }
  114. }
  115. fitByGardenId(gardenId, hasMapAnimate) {
  116. this.gardenPolygonLayer.source.forEachFeature((f) => {
  117. if (f.get("organId") == gardenId) {
  118. const extent = f.getGeometry().getExtent()
  119. this.kmap.getView().fit(extent, { padding: [160, 60, 340, 60], duration: hasMapAnimate ? 1500 : 0 });
  120. const currentZoom = this.kmap.getView().getZoom();
  121. if (currentZoom > 16) {
  122. // this.kmap.getView().setZoom(16);
  123. this.kmap.getView().animate({
  124. zoom: 16,
  125. duration: hasMapAnimate ? 1500 : 0 // 动画持续时间,单位为毫秒
  126. });
  127. }
  128. }
  129. })
  130. }
  131. }
  132. export default AreaMap;