varietyMap.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { getCenter } from 'ol/extent';
  2. import config from "@/api/config.js";
  3. import * as KMap from "@/utils/ol-map/KMap";
  4. import Stroke from "ol/style/Stroke";
  5. import * as util from "@/common/ol_common.js";
  6. import VectorSource from 'ol/source/Vector';
  7. import {newPoint} from "@/utils/map";
  8. import Feature from "ol/Feature";
  9. import Draw from 'ol/interaction/Draw';
  10. import { Style, Fill, Circle as CircleStyle } from 'ol/style';
  11. import { Polygon } from 'ol/geom';
  12. import Icon from "ol/style/Icon";
  13. import { Tile as TileLayer, Vector as VectorLayer } from 'ol/layer';
  14. import {Cluster} from "ol/source";
  15. /**
  16. * @description
  17. */
  18. class VarietyMap {
  19. constructor() {
  20. let that = this;
  21. let vectorStyle = new KMap.VectorStyle();
  22. this.vectorStyle = vectorStyle;
  23. this.drawInteraction = null;
  24. this.vectorSource = new VectorSource();
  25. this.clusterSource = new Cluster({
  26. distance: 15,
  27. minDistance: 60,
  28. });
  29. this.centerPointLayer = new KMap.VectorLayer("centerPointLayer", 1000, {
  30. source:this.clusterSource,
  31. style: (f) => {
  32. return new Style({
  33. image: new Icon({
  34. src: require('@/assets/images/map/active-icon.png'),
  35. scale: 0.4,
  36. }),
  37. });
  38. },
  39. });
  40. }
  41. initMap(location, target) {
  42. let level = 16;
  43. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  44. this.kmap = new KMap.Map( target, level, coordinate[0], coordinate[1], null, 1, 22 ,"vec", true, true);
  45. // 添加矢量图层用于显示框选结果
  46. const vectorLayer = new VectorLayer({
  47. source: this.vectorSource,
  48. style: new Style({
  49. fill: new Fill({
  50. color: 'rgba(255, 255, 255, 0.2)',
  51. }),
  52. stroke: new Stroke({
  53. color: '#ffcc33',
  54. width: 2,
  55. }),
  56. image: new CircleStyle({
  57. radius: 7,
  58. fill: new Fill({
  59. color: '#ffcc33',
  60. }),
  61. }),
  62. }),
  63. });
  64. this.kmap.addLayer(this.centerPointLayer.layer);
  65. this.kmap.addLayer(vectorLayer);
  66. // this.startDrawing(this.kmap.map)
  67. // // 初始化框选交互
  68. this.draw = new Draw({
  69. source: this.vectorSource,
  70. type: 'Circle', // 框选类型
  71. // geometryFunction: (coordinates, geometry) => {
  72. // if (coordinates.length < 2) {
  73. // return null;
  74. // }
  75. // return this.drawSquare(coordinates); // 绘制矩形
  76. // },
  77. });
  78. // 添加框选交互到地图
  79. this.kmap.map.addInteraction(this.draw);
  80. this.initData()
  81. // 监听drawend事件,获取框选范围
  82. let that = this
  83. this.draw.on('drawend', (event) => {
  84. // const feature = event.feature;
  85. let pixel=that.kmap.map.getEventPixel(event.originalEvent)
  86. const geometry = feature.getGeometry();
  87. const extent = geometry.getExtent();
  88. let feature=that.kmap.map.forEachFeatureAtPixel(pixel,function (feature) {
  89. console.log('feature',feature);
  90. return feature
  91. })
  92. // if(feature != null && feature == vm.polyline){
  93. // that.pointmoveFeature = feature
  94. // callback(vm.polyline)
  95. // }
  96. // if(feature==undefined){
  97. // if(that.pointmoveFeature){
  98. // that.pointmoveFeature = null
  99. // callback(null)
  100. // }
  101. // }
  102. console.log('Selected extent:', extent);
  103. console.log('Center of selected area:', getCenter(extent));
  104. });
  105. }
  106. initData(farmId, regionId){
  107. let that = this
  108. VE_API.sample.list({farmId:766,regionId:2}).then(({data})=>{
  109. // data[0].status = 9
  110. let features = []
  111. for(let item of data){
  112. let point = newPoint(item);
  113. features.push(point)
  114. }
  115. const source = new VectorSource({
  116. features: features,
  117. });
  118. that.clusterSource.setSource(source)
  119. })
  120. }
  121. // 绘制矩形
  122. drawRectangle = (coordinates) => {
  123. const start = coordinates[0];
  124. const end = coordinates[1];
  125. const minX = Math.min(start[0], end[0]);
  126. const maxX = Math.max(start[0], end[0]);
  127. const minY = Math.min(start[1], end[1]);
  128. const maxY = Math.max(start[1], end[1]);
  129. // 定义矩形的四个顶点
  130. const rectangleCoordinates = [
  131. [minX, minY],
  132. [maxX, minY],
  133. [maxX, maxY],
  134. [minX, maxY],
  135. [minX, minY], // 闭合矩形
  136. ];
  137. return new Polygon([rectangleCoordinates]);
  138. };
  139. drawSquare = (coordinates) => {
  140. const start = coordinates[0];
  141. const end = coordinates[1];
  142. const dx = end[0] - start[0];
  143. const dy = end[1] - start[1];
  144. const size = Math.max(Math.abs(dx), Math.abs(dy));
  145. const minX = start[0];
  146. const minY = start[1];
  147. const maxX = minX + size * Math.sign(dx);
  148. const maxY = minY + size * Math.sign(dy);
  149. // 定义正方形的四个顶点
  150. const squareCoordinates = [
  151. [minX, minY],
  152. [maxX, minY],
  153. [maxX, maxY],
  154. [minX, maxY],
  155. [minX, minY], // 闭合正方形
  156. ];
  157. return new Polygon([squareCoordinates]);
  158. };
  159. startDrawing = (map) => {
  160. if (this.drawInteraction) {
  161. map.removeInteraction(this.drawInteraction);
  162. }
  163. // 自定义绘制逻辑
  164. this.drawInteraction = new Draw({
  165. source: this.vectorSource,
  166. type: 'Circle', // 使用 Circle 作为基础
  167. geometryFunction: (coordinates, geometry) => {
  168. if (coordinates.length < 2) {
  169. return null;
  170. }
  171. return this.drawRectangle(coordinates); // 绘制矩形
  172. },
  173. });
  174. map.addInteraction(this.drawInteraction);
  175. // 监听绘制完成事件
  176. this.drawInteraction.on('drawend', (event) => {
  177. const feature = event.feature;
  178. const geometry = feature.getGeometry();
  179. console.log('正多边形坐标:', geometry.getCoordinates());
  180. });
  181. };
  182. // 清除绘制
  183. clearDrawing = () => {
  184. this.vectorSource.clear();
  185. };
  186. // 重新渲染地图
  187. updateMap() {
  188. setTimeout(() => {
  189. this.kmap.map.updateSize()
  190. }, 1000);
  191. }
  192. fit(geomOrExtent, padding) {
  193. this.kmap.fit(geomOrExtent, padding);
  194. }
  195. }
  196. export default VarietyMap;