authenticMap.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 Style from "ol/style/Style";
  5. import Icon from "ol/style/Icon";
  6. import VectorLayer from "ol/layer/Vector.js";
  7. import WKT from "ol/format/WKT.js";
  8. import { reactive } from "vue";
  9. import Point from "ol/geom/Point.js";
  10. import Feature from "ol/Feature";
  11. import { newPoint } from "@/utils/map.js";
  12. import { Fill, Text } from "ol/style";
  13. import { getArea } from "ol/sphere.js";
  14. import * as proj from "ol/proj";
  15. import proj4 from "proj4";
  16. import { register } from "ol/proj/proj4";
  17. import GeometryCollection from 'ol/geom/GeometryCollection.js';
  18. proj4.defs(
  19. "EPSG:38572",
  20. "+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"
  21. );
  22. register(proj4);
  23. export let mapData = reactive({
  24. isEdit: false,
  25. isEditArea: false,
  26. curPointData: {},
  27. point: null,
  28. selectPointArr: [],
  29. isPointHide: null,
  30. });
  31. function resetMapData(){
  32. mapData.isEdit= false
  33. mapData.isEditArea= false
  34. mapData.curPointData= {}
  35. mapData.point= null
  36. mapData.selectPointArr= []
  37. mapData.isPointHide= null
  38. }
  39. /**
  40. * @description 地图层对象
  41. */
  42. class AuthenticMap {
  43. constructor() {
  44. let that = this;
  45. let vectorStyle = new KMap.VectorStyle();
  46. this.vectorStyle = vectorStyle;
  47. // 位置图标
  48. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  49. style: (f) => {
  50. const style1 = new Style({
  51. image: new Icon({
  52. src: require(`@/assets/images/map/${f.get("icon")}-icon.png`),
  53. scale: 0.45,
  54. }),
  55. });
  56. const style2 = new Style({
  57. text: new Text({
  58. font: "16px sans-serif",
  59. text: f.get("masterName"),
  60. offsetY: -40,
  61. padding: [4, 3, 2, 106],
  62. fill: new Fill({ color: "#fff" }), // 字体颜色
  63. }),
  64. });
  65. const style3 = new Style({
  66. image: new Icon({
  67. src: require(`@/assets/images/map/${f.get("iconBg")}.png`),
  68. scale: 0.45,
  69. displacement: [0, 90],
  70. }),
  71. });
  72. return [style1, style2, style3];
  73. },
  74. });
  75. this.locationLayer = new KMap.VectorLayer("locationLayer", 9999, {
  76. style: () => {
  77. return new Style({
  78. image: new Icon({
  79. src: require("@/assets/images/map/location.png"),
  80. scale: 0.45,
  81. }),
  82. });
  83. },
  84. });
  85. // 存储绘制的地块特征
  86. // this.drawnFeatures = [];
  87. }
  88. initMap(location, target) {
  89. let level = 16;
  90. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  91. this.kmap = new KMap.Map(
  92. target,
  93. level,
  94. coordinate[0],
  95. coordinate[1],
  96. null,
  97. 6,
  98. 22
  99. );
  100. this.kmap.initDraw((e) => {
  101. if (e.type === "drawend") {
  102. mapData.isEdit = true;
  103. mapData.point = e.feature;
  104. const coord = e.feature.getGeometry().getCoordinates()[0];
  105. // console.log('coord',coord);
  106. }else{
  107. const coord = e.feature.getGeometry().getCoordinates()[0];
  108. // console.log('coord',coord);
  109. }
  110. // console.log("e", e);
  111. // console.log('111',e.feature.get('geometry'));
  112. // this.drawnFeatures.push(e.feature);
  113. });
  114. this.kmap.modifyDraw((e) => {
  115. mapData.isEditArea = false;
  116. mapData.isEditArea = true;
  117. });
  118. this.kmap.addLayer(this.clickPointLayer.layer);
  119. this.kmap.addLayer(this.locationLayer.layer);
  120. this.addMapSingerClick();
  121. }
  122. fit(geometriesWkt){
  123. let geometries = []
  124. let f = new WKT();
  125. for(let wkt of geometriesWkt){
  126. geometries.push(f.readGeometry(wkt))
  127. }
  128. let extent = new GeometryCollection(geometries).getExtent()
  129. this.kmap.fit(extent)
  130. }
  131. undoLastDraw() {
  132. const features = this.kmap.getLayerFeatures();
  133. features.forEach((feature) => {
  134. console.log("feature", feature, mapData.point);
  135. const coord = feature.getGeometry().getCoordinates()[0];
  136. });
  137. // this.kmap.setFeatureCursor("move")
  138. // const lastFeature = this.drawnFeatures.pop();
  139. // if (lastFeature) {
  140. // // const features = this.kmap.getLayerFeatures();
  141. // console.log("this", this.kmap.polygonLayer.source);
  142. // this.kmap.polygonLayer.source.removeFeature(lastFeature);
  143. // // this.kmap.getLayers().getArray()[1].getSource().removeFeature(lastFeature);
  144. // }
  145. }
  146. // 取消地块
  147. cancelDraw() {
  148. this.kmap.polygonLayer.source.removeFeature(mapData.point);
  149. }
  150. // 添加点位
  151. addPoint(points) {
  152. const arrPoints = [];
  153. if (points && points.length > 0) {
  154. points.forEach((item) => {
  155. let f = newPoint({ ...item, icon: "point", iconBg: "name-bg" }, "point");
  156. arrPoints.push(f);
  157. });
  158. this.clickPointLayer.source.addFeatures(arrPoints);
  159. }
  160. }
  161. // 设置地图中心点位
  162. setMapCenter(v) {
  163. let arrayOfNumbers = [];
  164. const arrayOfStrings = v.split(",");
  165. arrayOfNumbers = [arrayOfStrings[1], arrayOfStrings[0]];
  166. this.kmap.map.getView().setCenter(arrayOfNumbers);
  167. this.locationLayer.source.clear();
  168. let point = new Feature(new Point(arrayOfNumbers));
  169. this.locationLayer.addFeature(point);
  170. }
  171. // 开始勾画
  172. startDraw() {
  173. this.kmap.setDefaultCursor("crosshair");
  174. this.kmap.startDraw();
  175. }
  176. //结束勾画
  177. endDraw() {
  178. this.kmap.endDraw();
  179. this.kmap.endModify();
  180. }
  181. // 开始编辑
  182. startModify() {
  183. this.kmap.startModify();
  184. mapData.point.set("icon", "point-act");
  185. }
  186. //结束编辑
  187. endModify() {
  188. this.kmap.endModify();
  189. }
  190. // 清空单个数据
  191. clearMapData(name, val, id) {
  192. name && (mapData[name] = val);
  193. resetMapData()
  194. this.clickPointLayer.source.forEachFeature((feature) => {
  195. if (feature.get("id") === id) {
  196. feature.set("icon", "point");
  197. feature.set("iconBg", "name-bg");
  198. }
  199. });
  200. const points = this.kmap.getLayerFeatures();
  201. points.forEach((feature) => {
  202. if (feature.get("id") === id) {
  203. feature.set("icon", "point");
  204. this.kmap.polygonStyle(feature);
  205. mapData.isPointHide = feature;
  206. }
  207. });
  208. mapData.selectPointArr = [];
  209. }
  210. //全选
  211. allSelect(ids){
  212. let arr = []
  213. this.clickPointLayer.source.forEachFeature((feature) => {
  214. if(!ids || ids.findIndex((id)=> id == feature.get('id')) > -1){
  215. feature.set("icon", "point-act");
  216. feature.set("iconBg", "name-act-bg");
  217. }
  218. });
  219. const points = this.kmap.getLayerFeatures();
  220. points.forEach((feature) => {
  221. if(!ids || ids.findIndex((id)=> id == feature.get('id')) > -1) {
  222. feature.set("icon", "point-act");
  223. this.kmap.polygonStyle(feature);
  224. mapData.isPointHide = feature;
  225. arr.push(feature)
  226. }
  227. });
  228. mapData.selectPointArr = arr;
  229. }
  230. //no全选
  231. allUnSelect(){
  232. this.clickPointLayer.source.forEachFeature((feature) => {
  233. feature.set("icon", "point");
  234. feature.set("iconBg", "name-bg");
  235. });
  236. const points = this.kmap.getLayerFeatures();
  237. points.forEach((feature) => {
  238. feature.set("icon", "point");
  239. this.kmap.polygonStyle(feature);
  240. mapData.isPointHide = feature;
  241. });
  242. mapData.selectPointArr = [];
  243. }
  244. // 地图点击事件
  245. addMapSingerClick() {
  246. let that = this;
  247. that.kmap.on("singleclick", (evt) => {
  248. let num = 0;
  249. that.kmap.map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  250. // 点击的图层是否是VectorLayer
  251. if (
  252. layer instanceof VectorLayer &&
  253. (layer.get("name") === "clickPointLayer" ||
  254. layer.get("name") === "defaultPolygonLayer")
  255. ) {
  256. // 每次点击,只走一遍该方法
  257. num = num + 1;
  258. if (num === 1) {
  259. that.getSelectPointArr(feature.get("id"));
  260. that.kmap.endDraw();
  261. }
  262. }
  263. });
  264. });
  265. }
  266. setPoint(name) {
  267. const arr = mapData.selectPointArr.filter(
  268. (item) => item.values_.icon === "point-act"
  269. );
  270. if (arr.length > 0) {
  271. mapData.point = arr[0];
  272. mapData.point.set("icon", name);
  273. mapData.isPointHide.set("icon", name);
  274. mapData.point.set("iconBg", "name-bg");
  275. }
  276. if (arr.length === 1) {
  277. mapData.selectPointArr = [];
  278. }
  279. }
  280. //添加地块
  281. setAreaGeometry(geometryArr) {
  282. let that = this;
  283. geometryArr.map((item) => {
  284. item.icon = "point";
  285. item.iconHide = "false";
  286. that.kmap.setLayerWkt(item.featureWkt, item);
  287. });
  288. }
  289. // 移除点的功能
  290. removePoint() {
  291. // console.log('getDefaultCursor',this.kmap.getDefaultCursor());
  292. // this.kmap.setDefaultCursor('text')
  293. // console.log('getDefaultCursor',this.kmap.getDefaultCursor());
  294. // var source = this.kmap.setDefaultCursor('move');
  295. // console.log('source',this.kmap);
  296. // var source = this.kmap.polygonLayer.source
  297. // console.log('mapData.curPointData',mapData.curPointData);
  298. // source.removeFeature(mapData.curPointData)
  299. // source.removeFeature(pointFeature);
  300. // this.kmap.endDraw1()
  301. }
  302. // 获取所有选中点位
  303. getSelectPointArr(id) {
  304. const arr = [];
  305. this.clickPointLayer.source.forEachFeature((feature) => {
  306. if (feature.get("id") === id) {
  307. // 修改当前点位高亮
  308. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  309. const iconBg =
  310. feature.get("iconBg") === "name-bg" ? "name-act-bg" : "name-bg";
  311. feature.set("icon", icon);
  312. feature.set("iconBg", iconBg);
  313. mapData.point = feature;
  314. mapData.curPointData = feature.values_;
  315. }
  316. if (feature.get("icon") === "point-act") {
  317. arr.push(feature);
  318. }
  319. });
  320. const points = this.kmap.getLayerFeatures();
  321. points.forEach((feature) => {
  322. if (feature.get("id") === id) {
  323. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  324. feature.set("icon", icon);
  325. this.kmap.polygonStyle(feature);
  326. mapData.isPointHide = feature;
  327. }
  328. });
  329. mapData.selectPointArr = arr;
  330. }
  331. hidePoint() {
  332. const feature = mapData.isPointHide;
  333. feature.set("iconHide", "true");
  334. this.kmap.polygonStyle(feature);
  335. }
  336. clearLayer() {
  337. this.clickPointLayer.source.clear();
  338. this.kmap.polygonLayer.source.clear();
  339. }
  340. addLayer() {
  341. this.kmap.addLayer(this.kmap.polygonLayer.layer);
  342. this.kmap.addLayer(this.clickPointLayer.layer);
  343. }
  344. //获取地块信息
  345. getAreaGeometry() {
  346. const features = this.kmap.getLayerFeatures();
  347. let geometryArr = [];
  348. let area = 0;
  349. // 获取图层上的Polygon,转成geoJson用于回显
  350. features.forEach((item) => {
  351. geometryArr.push({ featureWkt: new WKT().writeFeature(item) });
  352. let geom = item.getGeometry().clone();
  353. geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
  354. let areaItem = getArea(geom);
  355. area = (areaItem + areaItem / 2) / 1000;
  356. });
  357. return { geometryArr, area: area.toFixed(2) };
  358. }
  359. /**
  360. address
  361. farmName
  362. masterName
  363. masterTel
  364. speciesTypeName
  365. ""
  366. * @param form
  367. */
  368. search(form){
  369. const points = this.kmap.getLayerFeatures();
  370. let arr = []
  371. points.forEach((feature) => {
  372. let condition = []
  373. if(form.address != ''){
  374. condition.push(feature.get("address").includes(form.address))
  375. }
  376. if(form.farmName != ''){
  377. condition.push(feature.get("farmName").includes(form.farmName))
  378. }
  379. if(form.masterName != ''){
  380. condition.push(feature.get("masterName").includes(form.masterName))
  381. }
  382. if(form.masterTel != ''){
  383. condition.push(feature.get("masterTel").includes(form.masterTel))
  384. }
  385. if(form.speciesTypeName != ''){
  386. condition.push(feature.get("speciesTypeName").includes(form.speciesTypeName))
  387. }
  388. let b = false
  389. for(let item of condition){
  390. if(item){
  391. b = true
  392. }else{
  393. b = false
  394. break
  395. }
  396. }
  397. if(b){
  398. arr.push(feature.get("id"))
  399. }
  400. });
  401. this.allUnSelect()
  402. // alert(arr)
  403. this.allSelect(arr)
  404. }
  405. }
  406. export default AuthenticMap;