homeMap.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <div>
  3. <div id="map" ref="areaRef" class="area-map"></div>
  4. </div>
  5. </template>
  6. <script setup>
  7. import * as KMap from "@/utils/ol-map/KMap";
  8. import * as util from "@/common/ol_common.js";
  9. import "ol/ol.css";
  10. import Map from "ol/Map";
  11. import View from "ol/View";
  12. import TileLayer from "ol/layer/Tile";
  13. import OSM from "ol/source/OSM";
  14. import VectorLayer from "ol/layer/Vector";
  15. // import VectorSource from "ol/source/Vector";
  16. import Feature from "ol/Feature";
  17. import Point from "ol/geom/Point";
  18. import Icon from "ol/style/Icon";
  19. import { fromLonLat } from "ol/proj";
  20. import DragBox from "ol/interaction/DragBox";
  21. import { platformModifierKeyOnly } from "ol/events/condition";
  22. import { newPoint } from "@/utils/map.js";
  23. import { Circle, Fill, Stroke, Style, Text } from "ol/style.js";
  24. import { Cluster, Vector as VectorSource } from "ol/source.js";
  25. import { boundingExtent } from "ol/extent.js";
  26. import RegionLayer from "./map/regionLayer";
  27. import eventBus from "@/api/eventBus";
  28. import { onMounted, ref } from "vue";
  29. import { useStore } from "vuex";
  30. import { unByKey } from "ol/Observable";
  31. let store = useStore();
  32. // 选中样式(高亮)
  33. // const selectedStyle = new Style({
  34. // image: new Icon({
  35. // src: require("@/assets/images/map/status/active-icon.png"),
  36. // scale: 0.6,
  37. // }),
  38. // });
  39. let kmap = null;
  40. const areaRef = ref(null);
  41. let dragBox;
  42. const isDrawing = ref(false);
  43. const enableBoxSelect = () => {
  44. const data = mapPoint.value.filter((item) => item.fosterStatus === 0);
  45. addCluster(data, 1);
  46. isDrawing.value = true;
  47. dragBox = new DragBox({
  48. condition: platformModifierKeyOnly, // 按住 Ctrl 才可框选(可去掉)
  49. });
  50. // 添加键盘事件监听
  51. window.addEventListener("keydown", handleKeyDown);
  52. window.addEventListener("keyup", handleKeyUp);
  53. kmap.map.addInteraction(dragBox);
  54. // areaRef.value.style.cursor = 'crosshair';
  55. dragBox.on("boxend", () => {
  56. const extent = dragBox.getGeometry().getExtent();
  57. treeClusterLayer.layer.getSource().getSource().getFeatures().forEach((feature) => {
  58. const isInside = feature.getGeometry().intersectsExtent(extent);
  59. if (isInside) {
  60. // feature.setStyle(selectedStyle);
  61. feature.set("highlight", true);
  62. }
  63. });
  64. });
  65. };
  66. const handleKeyDown = (e) => {
  67. if (e.ctrlKey) {
  68. areaRef.value.style.cursor = "crosshair";
  69. }
  70. };
  71. const handleKeyUp = (e) => {
  72. if (!e.ctrlKey) {
  73. areaRef.value.style.cursor = "";
  74. }
  75. };
  76. const stopBoxSelect = () => {
  77. addCluster(mapPoint.value);
  78. kmap.map.removeInteraction(dragBox);
  79. // 移除事件监听
  80. window.removeEventListener("keydown", handleKeyDown);
  81. window.removeEventListener("keyup", handleKeyUp);
  82. };
  83. let treeClusterLayer;
  84. onMounted(() => {
  85. let level = 16;
  86. let coordinate = util.wktCastGeom(store.getters.userinfo.location).getFirstCoordinate();
  87. kmap = new KMap.Map(areaRef.value, level, coordinate[0], coordinate[1], null, 1, 22, "img", true, true);
  88. regionLayer = new RegionLayer(kmap);
  89. });
  90. let styleCache = {};
  91. let listenKey;
  92. const mapPoint = ref([]);
  93. let clusterSource;
  94. // --------聚合-----------
  95. function addCluster(treeListData, distanceVal) {
  96. if (!distanceVal) {
  97. mapPoint.value = treeListData;
  98. }
  99. clearCluster();
  100. let that = this;
  101. let features = [];
  102. // 根据状态加上对应的图标
  103. for (let item of treeListData) {
  104. let point = newPoint(item);
  105. features.push(point);
  106. console.log('item', item.icon, item);
  107. }
  108. const source = new VectorSource({
  109. features: features,
  110. });
  111. clusterSource = new Cluster({
  112. distance: distanceVal ? distanceVal : 20, // 集合范围
  113. // minDistance: 60,
  114. source: source,
  115. });
  116. treeClusterLayer = new KMap.VectorLayer("forest-cluster", 1000, {
  117. minZoom: 15,
  118. source: clusterSource,
  119. style: (feature) => {
  120. const size = feature.get("features").length;
  121. let testStyle;
  122. // 只有一个数据,不需要聚合,直接使用第一项数据的图标
  123. if (size == 1) {
  124. let featureOne = feature.get("features")[0];
  125. const key = featureOne.get('fosterStatus')+"status"
  126. // let style = styleCache[key];
  127. let style = false;
  128. if (!style) {
  129. const highlight = featureOne.get("highlight");
  130. if (highlight) {
  131. style = new Style({
  132. image: new Icon({
  133. src: require("@/assets/images/map/status/active-icon.png"),
  134. scale: 0.6,
  135. }),
  136. });
  137. } else {
  138. style = new Style({
  139. image: new Circle({
  140. radius: featureOne.get("fosterStatus") === 0 || !featureOne.get("fosterStatus") ? 10 : 12,
  141. fill: new Fill({
  142. color:
  143. featureOne.get("fosterStatus") === 0
  144. ? "#ffffff00"
  145. : featureOne.get("fosterStatus") === 1
  146. ? "#EEEEEE"
  147. : featureOne.get("fosterStatus") === 2
  148. ? "#F0AC37"
  149. : "#ffffff00",
  150. }),
  151. stroke: new Stroke({
  152. color: "#fff",
  153. width: 1,
  154. }),
  155. }),
  156. });
  157. }
  158. // styleCache[key] = style;
  159. }
  160. const imgIcon = featureOne.get('icon')
  161. console.log('imgIcon', imgIcon);
  162. if (imgIcon) {
  163. style = new Style({
  164. image: new Icon({
  165. src: require("@/assets/images/map/owner1.png"),
  166. scale: 1,
  167. }),
  168. });
  169. }
  170. return style;
  171. }
  172. // 多个点位聚合,循环处理得到图标
  173. const featureObj = feature.get("features")[0];
  174. // let pointId = featureObj.get('fosterStatus')
  175. // let style = styleCache[pointId];
  176. const imgIcon = featureObj.get('icon')
  177. console.log('imgIcon22222', imgIcon);
  178. let style = false;
  179. if (!style) {
  180. testStyle = new Style({
  181. text: new Text({
  182. text: size + "",
  183. offsetX: 0,
  184. offsetY: 0,
  185. font: "bold 10px sans-serif",
  186. fill: new Fill({ color: featureObj.get("fosterStatus") === 1 ? "#000" : "#fff" }), // 字体颜色
  187. // stroke: new Stroke({ color: "green" }), // 字体颜色
  188. }),
  189. zIndex: 3,
  190. });
  191. const highlight = featureObj.get("highlight");
  192. if (highlight) {
  193. style = new Style({
  194. image: new Icon({
  195. src: require("@/assets/images/map/status/active-icon.png"),
  196. scale: 0.6,
  197. }),
  198. });
  199. } else {
  200. // 已认养--显示图标
  201. if (imgIcon) {
  202. style = new Style({
  203. image: new Icon({
  204. src: require("@/assets/images/map/owner1.png"),
  205. scale: 1,
  206. }),
  207. zIndex: 22,
  208. });
  209. } else {
  210. style = new Style({
  211. image: new Circle({
  212. radius: featureObj.get("fosterStatus") === 0 || !featureObj.get("fosterStatus") ? 10 : 12,
  213. fill: new Fill({
  214. color:
  215. featureObj.get("fosterStatus") === 0
  216. ? "#ffffff00"
  217. : featureObj.get("fosterStatus") === 1
  218. ? "#EEEEEE"
  219. : featureObj.get("fosterStatus") === 2
  220. ? "#F0AC37"
  221. : "#ffffff00",
  222. }),
  223. stroke: new Stroke({
  224. color: "#fff",
  225. width: 1,
  226. }),
  227. }),
  228. });
  229. }
  230. }
  231. // styleCache[pointId] = style;
  232. }
  233. return [style, testStyle];
  234. },
  235. });
  236. kmap.addLayer(treeClusterLayer.layer);
  237. if(!distanceVal) {
  238. zoomToFeatures(features)
  239. }
  240. // kmap.getView().fit(this.treeClusterLayer.layer.getSource().getSource().getExtent(), { duration: 1000, padding: [120, 120, 200, 120] });
  241. // 监听聚合点位的点击,点击后缩放到范围内
  242. listenKey = kmap.on("click", (e) => {
  243. if (treeClusterLayer) {
  244. treeClusterLayer.layer.getFeatures(e.pixel).then((clickedFeatures, layer) => {
  245. let hasFeatures = false
  246. if (clickedFeatures.length) {
  247. const features = clickedFeatures[0].get("features");
  248. if (features.length > 1) {
  249. hasFeatures = true
  250. const extent = boundingExtent(features.map((r) => r.getGeometry().getCoordinates()));
  251. kmap.getView().fit(extent, { duration: 1000, padding: [250, 250, 250, 250] });
  252. const currentZoom = kmap.getView().getZoom();
  253. if (currentZoom > 17) {
  254. // this.kmap.getView().setZoom(16);
  255. // kmap.getView().animate({
  256. // zoom: 14,
  257. // duration: 0 // 动画持续时间,单位为毫秒
  258. // });
  259. kmap.getView().setZoom(17)
  260. }
  261. }
  262. if (isDrawing.value) {
  263. features[0].set("highlight", true);
  264. // features[0].setStyle(selectedStyle)
  265. } else if (!hasFeatures) {
  266. eventBus.emit("clickMapPoint", features[0])
  267. }
  268. }
  269. });
  270. }
  271. });
  272. }
  273. // 清除聚合图层,解除绑定
  274. function clearCluster() {
  275. if (treeClusterLayer) {
  276. treeClusterLayer.layer.getSource().getSource().clear();
  277. treeClusterLayer.layer.getSource().clear();
  278. treeClusterLayer = null;
  279. unByKey(listenKey);
  280. }
  281. }
  282. function zoomToFeatures(features) {
  283. const extent = boundingExtent(features.map((r) => r.getGeometry().getCoordinates()));
  284. kmap.getView().fit(extent, { duration: 500, padding: [200, 250, 120, 250] });
  285. }
  286. defineExpose({ addCluster, enableBoxSelect, stopBoxSelect, initAreaMap });
  287. // 分区
  288. let regionLayer = null;
  289. function initAreaMap(arr) {
  290. regionLayer.initData(arr);
  291. }
  292. </script>
  293. <style lang="less" scoped>
  294. .area-map {
  295. width: 100%;
  296. height: 100%;
  297. }
  298. </style>