authenticMap.js 13 KB

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