authenticMap.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. }
  105. });
  106. this.kmap.modifyDraw((e) => {
  107. mapData.isEditArea = false;
  108. mapData.isEditArea = true;
  109. });
  110. this.kmap.addLayer(this.clickPointLayer.layer);
  111. this.kmap.addLayer(this.locationLayer.layer);
  112. this.addMapSingerClick();
  113. }
  114. fit(geometriesWkt){
  115. let geometries = []
  116. let f = new WKT();
  117. for(let wkt of geometriesWkt){
  118. geometries.push(f.readGeometry(wkt))
  119. }
  120. let extent = new GeometryCollection(geometries).getExtent()
  121. this.kmap.fit(extent)
  122. }
  123. undoLastDraw() {
  124. const features = this.kmap.getLayerFeatures();
  125. features.forEach((feature) => {
  126. // console.log("feature", feature, mapData.point);
  127. const coord = feature.getGeometry().getCoordinates()[0];
  128. });
  129. // this.kmap.setFeatureCursor("move")
  130. // const lastFeature = this.drawnFeatures.pop();
  131. // if (lastFeature) {
  132. // // const features = this.kmap.getLayerFeatures();
  133. // console.log("this", this.kmap.polygonLayer.source);
  134. // this.kmap.polygonLayer.source.removeFeature(lastFeature);
  135. // // this.kmap.getLayers().getArray()[1].getSource().removeFeature(lastFeature);
  136. // }
  137. }
  138. // 取消地块
  139. cancelDraw() {
  140. this.kmap.polygonLayer.source.removeFeature(mapData.point);
  141. }
  142. // 添加点位
  143. addPoint(points) {
  144. const arrPoints = [];
  145. if (points && points.length > 0) {
  146. points.forEach((item) => {
  147. let f = newPoint({ ...item, icon: "point", iconBg: "name-bg" }, "point");
  148. arrPoints.push(f);
  149. });
  150. this.clickPointLayer.source.addFeatures(arrPoints);
  151. }
  152. }
  153. // 设置地图中心点位
  154. setMapCenter(v) {
  155. let arrayOfNumbers = [];
  156. const arrayOfStrings = v.split(",");
  157. arrayOfNumbers = [arrayOfStrings[1], arrayOfStrings[0]];
  158. this.kmap.map.getView().setCenter(arrayOfNumbers);
  159. this.locationLayer.source.clear();
  160. let point = new Feature(new Point(arrayOfNumbers));
  161. this.locationLayer.addFeature(point);
  162. }
  163. // 开始勾画
  164. startDraw() {
  165. this.kmap.setDefaultCursor("crosshair");
  166. this.kmap.startDraw();
  167. }
  168. //结束勾画
  169. endDraw() {
  170. this.kmap.endDraw();
  171. this.kmap.endModify();
  172. }
  173. // 开始编辑
  174. startModify() {
  175. this.kmap.startModify();
  176. mapData.point.set("icon", "point-act");
  177. }
  178. //结束编辑
  179. endModify() {
  180. this.kmap.endModify();
  181. }
  182. // 清空单个数据
  183. clearMapData(name, val, id) {
  184. name && (mapData[name] = val);
  185. resetMapData()
  186. this.clickPointLayer.source.forEachFeature((feature) => {
  187. if (feature.get("id") === id) {
  188. feature.set("icon", "point");
  189. feature.set("iconBg", "name-bg");
  190. }
  191. });
  192. const points = this.kmap.getLayerFeatures();
  193. points.forEach((feature) => {
  194. if (feature.get("id") === id) {
  195. feature.set("icon", "point");
  196. this.kmap.polygonStyle(feature);
  197. mapData.isPointHide = feature;
  198. }
  199. });
  200. mapData.selectPointArr = [];
  201. }
  202. //全选
  203. allSelect(ids){
  204. let arr = []
  205. this.clickPointLayer.source.forEachFeature((feature) => {
  206. if(!ids || ids.findIndex((id)=> id == feature.get('id')) > -1){
  207. feature.set("icon", "point-act");
  208. feature.set("iconBg", "name-act-bg");
  209. }
  210. });
  211. const points = this.kmap.getLayerFeatures();
  212. points.forEach((feature) => {
  213. if(!ids || ids.findIndex((id)=> id == feature.get('id')) > -1) {
  214. feature.set("icon", "point-act");
  215. this.kmap.polygonStyle(feature);
  216. mapData.isPointHide = feature;
  217. arr.push(feature)
  218. }
  219. });
  220. mapData.selectPointArr = arr;
  221. }
  222. //no全选
  223. allUnSelect(){
  224. this.clickPointLayer.source.forEachFeature((feature) => {
  225. feature.set("icon", "point");
  226. feature.set("iconBg", "name-bg");
  227. });
  228. const points = this.kmap.getLayerFeatures();
  229. points.forEach((feature) => {
  230. feature.set("icon", "point");
  231. this.kmap.polygonStyle(feature);
  232. mapData.isPointHide = feature;
  233. });
  234. mapData.selectPointArr = [];
  235. }
  236. // 地图点击事件
  237. addMapSingerClick() {
  238. let that = this;
  239. that.kmap.on("singleclick", (evt) => {
  240. let num = 0;
  241. that.kmap.map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  242. // 点击的图层是否是VectorLayer
  243. if (
  244. layer instanceof VectorLayer &&
  245. (layer.get("name") === "clickPointLayer" ||
  246. layer.get("name") === "defaultPolygonLayer")
  247. ) {
  248. // 每次点击,只走一遍该方法
  249. num = num + 1;
  250. if (num === 1) {
  251. that.getSelectPointArr(feature.get("id"));
  252. that.kmap.endDraw();
  253. }
  254. }
  255. });
  256. });
  257. }
  258. setPoint(name) {
  259. const arr = mapData.selectPointArr.filter(
  260. (item) => item.values_.icon === "point-act"
  261. );
  262. if (arr.length > 0) {
  263. mapData.point = arr[0];
  264. mapData.point.set("icon", name);
  265. mapData.isPointHide.set("icon", name);
  266. mapData.point.set("iconBg", "name-bg");
  267. }
  268. if (arr.length === 1) {
  269. mapData.selectPointArr = [];
  270. }
  271. }
  272. //添加地块
  273. setAreaGeometry(geometryArr) {
  274. let that = this;
  275. geometryArr.map((item) => {
  276. item.icon = "point";
  277. item.iconHide = "false";
  278. that.kmap.setLayerWkt(item.featureWkt, item);
  279. });
  280. }
  281. // 移除点的功能
  282. removePoint() {
  283. // console.log('getDefaultCursor',this.kmap.getDefaultCursor());
  284. // this.kmap.setDefaultCursor('text')
  285. // console.log('getDefaultCursor',this.kmap.getDefaultCursor());
  286. // var source = this.kmap.setDefaultCursor('move');
  287. // console.log('source',this.kmap);
  288. // var source = this.kmap.polygonLayer.source
  289. // console.log('mapData.curPointData',mapData.curPointData);
  290. // source.removeFeature(mapData.curPointData)
  291. // source.removeFeature(pointFeature);
  292. // this.kmap.endDraw1()
  293. }
  294. // 获取所有选中点位
  295. getSelectPointArr(id) {
  296. const arr = [];
  297. this.clickPointLayer.source.forEachFeature((feature) => {
  298. if (feature.get("id") === id) {
  299. // 修改当前点位高亮
  300. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  301. const iconBg =
  302. feature.get("iconBg") === "name-bg" ? "name-act-bg" : "name-bg";
  303. feature.set("icon", icon);
  304. feature.set("iconBg", iconBg);
  305. mapData.point = feature;
  306. mapData.curPointData = feature.values_;
  307. }
  308. if (feature.get("icon") === "point-act") {
  309. arr.push(feature);
  310. }
  311. });
  312. const points = this.kmap.getLayerFeatures();
  313. points.forEach((feature) => {
  314. if (feature.get("id") === id) {
  315. const icon = feature.get("icon") === "point" ? "point-act" : "point";
  316. feature.set("icon", icon);
  317. this.kmap.polygonStyle(feature);
  318. mapData.isPointHide = feature;
  319. }
  320. });
  321. mapData.selectPointArr = arr;
  322. }
  323. hidePoint() {
  324. const feature = mapData.isPointHide;
  325. feature.set("iconHide", "true");
  326. this.kmap.polygonStyle(feature);
  327. }
  328. clearLayer() {
  329. this.clickPointLayer.source.clear();
  330. this.kmap.polygonLayer.source.clear();
  331. }
  332. addLayer() {
  333. this.kmap.addLayer(this.kmap.polygonLayer.layer);
  334. this.kmap.addLayer(this.clickPointLayer.layer);
  335. }
  336. //获取地块信息
  337. getAreaGeometry() {
  338. const features = this.kmap.getLayerFeatures();
  339. let geometryArr = [];
  340. let area = 0;
  341. // 获取图层上的Polygon,转成geoJson用于回显
  342. features.forEach((item) => {
  343. geometryArr.push({ featureWkt: new WKT().writeFeature(item) });
  344. let geom = item.getGeometry().clone();
  345. geom.transform(proj.get("EPSG:4326"), proj.get("EPSG:38572"));
  346. let areaItem = getArea(geom);
  347. area = (areaItem + areaItem / 2) / 1000;
  348. });
  349. return { geometryArr, area: area.toFixed(2) };
  350. }
  351. /**
  352. address
  353. farmName
  354. masterName
  355. masterTel
  356. speciesTypeName
  357. ""
  358. * @param form
  359. */
  360. search(form){
  361. const points = this.kmap.getLayerFeatures();
  362. let arr = []
  363. let geomWkt = []
  364. points.forEach((feature) => {
  365. let condition = []
  366. if(form.address != ''){
  367. condition.push(feature.get("address").includes(form.address))
  368. }
  369. if(form.farmName != ''){
  370. condition.push(feature.get("farmName").includes(form.farmName))
  371. }
  372. if(form.masterName != ''){
  373. const text = feature.get("masterName")
  374. if(text!==form.masterName) return
  375. condition.push(feature.get("masterName").includes(form.masterName))
  376. }
  377. if(form.masterTel != ''){
  378. const text = feature.get("masterTel")
  379. if(text!==form.masterTel) return
  380. condition.push(feature.get("masterTel").includes(form.masterTel))
  381. }
  382. if(form.speciesTypeName != ''){
  383. const text = feature.get("speciesTypeName")
  384. if(text!==form.speciesTypeName) return
  385. condition.push(feature.get("speciesTypeName").includes(form.speciesTypeName))
  386. }
  387. let b = false
  388. for(let item of condition){
  389. if(item){
  390. b = true
  391. }else{
  392. b = false
  393. break
  394. }
  395. }
  396. if(b){
  397. arr.push(feature.get("id"))
  398. geomWkt.push(new WKT().writeGeometry(feature.getGeometry()))
  399. }
  400. });
  401. this.allUnSelect()
  402. this.allSelect(arr)
  403. if(geomWkt.length>0){
  404. this.fit(geomWkt)
  405. }
  406. }
  407. }
  408. export default AuthenticMap;