index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 Style from "ol/style/Style";
  5. import Icon from "ol/style/Icon";
  6. import { Point } from 'ol/geom';
  7. import Feature from "ol/Feature";
  8. import { reactive } from "vue";
  9. export let mapLocation = reactive({
  10. data: null,
  11. });
  12. /**
  13. * @description 地图层对象
  14. */
  15. class IndexMap {
  16. constructor() {
  17. let that = this;
  18. let vectorStyle = new KMap.VectorStyle();
  19. this.vectorStyle = vectorStyle;
  20. // 位置图标
  21. this.clickPointLayer = new KMap.VectorLayer("clickPointLayer", 9999, {
  22. style: (f) => {
  23. return new Style({
  24. image: new Icon({
  25. src: require("@/assets/img/home/garden-point.png"),
  26. scale: 0.5,
  27. anchor: [0.5, 1],
  28. }),
  29. });
  30. },
  31. });
  32. }
  33. initMap(location, target) {
  34. let level = 16;
  35. let coordinate = util.wktCastGeom(location).getFirstCoordinate();
  36. this.kmap = new KMap.Map(target, level, coordinate[0], coordinate[1], null, 8, 22);
  37. let xyz2 = config.base_img_url3 + "map/lby/{z}/{x}/{y}.png";
  38. this.kmap.addXYZLayer(xyz2, { minZoom: 8, maxZoom: 22 }, 2);
  39. this.kmap.addLayer(this.clickPointLayer.layer);
  40. this.setMapPoint(coordinate)
  41. this.addMapSingerClick()
  42. }
  43. setMapPosition(center) {
  44. this.kmap.getView().animate({
  45. center,
  46. zoom: 16,
  47. duration: 500,
  48. });
  49. this.setMapPoint(center)
  50. }
  51. setMapPoint(coordinate) {
  52. this.clickPointLayer.source.clear()
  53. let point = new Feature(new Point(coordinate))
  54. this.clickPointLayer.addFeature(point)
  55. }
  56. // 地图点击事件
  57. addMapSingerClick() {
  58. let that = this;
  59. that.kmap.on("singleclick", (evt) => {
  60. // that.kmap.map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
  61. // if ( layer instanceof VectorLayer && layer.get("name") === "reportPolygonLayer" ) {
  62. // areaId.data = feature.get("id")
  63. // }
  64. // });
  65. that.setMapPoint(evt.coordinate)
  66. mapLocation.data = evt.coordinate
  67. });
  68. }
  69. clearLayer() {
  70. // this.kmap.removeLayer(this.clickPointLayer.layer)
  71. this.kmap.polygonLayer.source.clear();
  72. }
  73. setAreaGeometry(geometryArr) {
  74. this.clearLayer()
  75. let that = this
  76. geometryArr.map(item => {
  77. that.kmap.setLayerWkt(item.featureWkt)
  78. })
  79. }
  80. }
  81. export default IndexMap;