homeMap.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <div id="map-container"></div>
  3. </template>
  4. <script setup>
  5. import { ref, onMounted, onUnmounted } from "vue";
  6. import AMapLoader from "@amap/amap-jsapi-loader";
  7. import eventBus from "@/api/eventBus";
  8. import { convertPointToArray } from "@/utils/index";
  9. import { deepClone } from "@/common/commonFun";
  10. import wellknown from 'wellknown';
  11. import {base_img_url,base_img_url3} from "@/api/config";
  12. const map = ref(null);
  13. const mouseTool = ref(null);
  14. const selectedArea = ref(null);
  15. const selectedPoints = ref([]);
  16. const allPoints = ref([]);
  17. const defalutAllPoints = ref([])
  18. const pointList = ref([])
  19. const mapEventType = ref('rectangle')
  20. // 自定义圆点样式
  21. const createMarkerContent = (color = 'rgba(0, 0, 0, 0.4)') => {
  22. return `
  23. <div style="
  24. width: 15px;
  25. height: 15px;
  26. background-color: ${color};
  27. border: 1px solid #fff;
  28. border-radius: 50%;
  29. ">
  30. </div>
  31. `;
  32. }
  33. //高亮样式
  34. const createMarkerImg = () => {
  35. return `
  36. <img style="width: 38px;height: 38px;" src="${require('@/assets/images/map/status/active-icon.png')}">
  37. `;
  38. }
  39. // 初始化地图
  40. const initMap = async () => {
  41. try {
  42. const AMap = await AMapLoader.load({
  43. key: "41769169f0f157e13a197e7eb0dd7b5b", // 替换为你的高德地图 Key
  44. version: "2.0", // SDK 版本
  45. plugins: ["AMap.MouseTool","AMap.MarkerCluster"], // 加载 MouseTool 插件
  46. }).then(async (AMap) => {
  47. // 创建地图实例
  48. map.value = new AMap.Map("map-container", {
  49. zoom: 18, // 初始缩放级别
  50. center: [111.01055729572, 21.7743543],
  51. layers: [
  52. // 天地图卫星
  53. new AMap.TileLayer({
  54. tileSize: 256, // 瓦片大小
  55. getTileUrl: function(x, y, z) {
  56. // 天地图影像 WMTS 地址
  57. const s = Math.floor(Math.random() * 8); // 随机选择服务器节点
  58. return `https://t${s}.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX=${z}&TILEROW=${y}&TILECOL=${x}&tk=e95115c454a663cd052d96019fd83840`;
  59. },
  60. }),
  61. //天地图路网
  62. new AMap.TileLayer({
  63. tileSize: 256, // 瓦片大小
  64. getTileUrl: function(x, y, z) {
  65. // 天地图影像 WMTS 地址
  66. const s = Math.floor(Math.random() * 8); // 随机选择服务器节点
  67. return `https://t${s}.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX=${z}&TILEROW=${y}&TILECOL=${x}&tk=e95115c454a663cd052d96019fd83840`;
  68. },
  69. }),
  70. //正射影像
  71. new AMap.TileLayer({
  72. tileUrl: base_img_url + 'map/lby/[z]/[x]/[y].png',
  73. zIndex:2
  74. }),
  75. new AMap.TileLayer({
  76. tileUrl: base_img_url3 + 'map/lby/[z]/[x]/[y].png',
  77. zIndex:2
  78. })
  79. ],
  80. });
  81. // // 初始化点聚合
  82. // const cluster = new AMap.MarkerCluster(map, markers, {
  83. // gridSize: 60, // 聚合网格像素大小
  84. // maxZoom: 15, // 最大聚合级别
  85. // renderClusterMarker: customizeCluster // 自定义聚合样式
  86. // });
  87. // 初始化 MouseTool
  88. mouseTool.value = new AMap.MouseTool(map.value);
  89. // startRectangleSelection()
  90. // 监听框选完成事件
  91. mouseTool.value.on("draw", (event) => {
  92. if(mapEventType.value==='click') return
  93. const { obj} = event;
  94. if (obj instanceof AMap.Rectangle) {
  95. selectedArea.value = obj;
  96. map.value.remove(selectedArea.value);
  97. checkPointsInArea(); // 检查框选区域内的点位
  98. }
  99. });
  100. });
  101. } catch (error) {
  102. console.error("地图加载失败:", error);
  103. }
  104. };
  105. //重置数据
  106. const resetData = () =>{
  107. allPoints.value = defalutAllPoints.value
  108. allPoints.value.forEach(item =>{
  109. item.icon.setContent(createMarkerContent(item.color))
  110. item.icon.setOffset(new window.AMap.Pixel(-7, -7))
  111. item.type = 'defalutIcon'
  112. })
  113. }
  114. //初始化数据
  115. const initData = (data) =>{
  116. allPoints.value = data
  117. defalutAllPoints.value = []
  118. totalList.value.forEach(item => map.value.remove(item.icon))
  119. totalList.value = []
  120. allPoints.value.forEach(item =>{
  121. item.icon = new window.AMap.Marker({
  122. position: item.lnglat,
  123. map: map.value,
  124. content: createMarkerContent(item.color),
  125. offset: new window.AMap.Pixel(-7, -7),
  126. extData: { id: item.id, priority: item.status > item.noImg > item.wys } // 标记优先级
  127. });
  128. item.type = 'defalutIcon'
  129. // 绑定点击事件
  130. item.icon.on('click', () => {
  131. mapEventType.value = 'click'
  132. if(item.type === 'defalutIcon'){
  133. if (startDraw.value) {
  134. item.icon.setContent(createMarkerImg());
  135. item.icon.setOffset(new window.AMap.Pixel(-18, -18))
  136. item.type = 'activeIcon'
  137. const arr = pointList.value.filter(ele => ele.id === item.id)
  138. if(arr.length===0){
  139. pointList.value.push(item)
  140. }
  141. }
  142. }else{
  143. item.icon.setContent(createMarkerContent(item.color));
  144. item.icon.setOffset(new window.AMap.Pixel(-7, -7))
  145. item.type = 'defalutIcon'
  146. }
  147. console.log('item', item);
  148. eventBus.emit("clickMapPoint", item)
  149. setTimeout(()=>{
  150. mapEventType.value = 'draw'
  151. },100)
  152. });
  153. defalutAllPoints.value.push(item)
  154. })
  155. // 初始化点聚合
  156. // console.log('data',data);
  157. // const cluster = new window.AMap.MarkerCluster(map.value, data, {
  158. // gridSize: 30, // 聚合网格像素大小
  159. // // maxZoom: 15, // 最大聚合级别
  160. // // renderClusterMarker: customizeCluster // 自定义聚合样式
  161. // });
  162. setTimeout(()=>{
  163. map.value.setFitView(null, false, [0, 0, 0, 0]);
  164. },100)
  165. // 范围
  166. // const p1 = [
  167. // [111.00745562137854, 21.77564355219471],
  168. // [111.00982318507914, 21.77557930674749],
  169. // [111.01133652227907, 21.776531091149934],
  170. // [111.0125762214629, 21.775945743742568],
  171. // [111.01290458708189, 21.775241423284797],
  172. // [111.01341617119812, 21.773759019078113],
  173. // [111.01320915809049, 21.77329502418189],
  174. // [111.01209794980082, 21.772538355582128],
  175. // [111.01117233946951, 21.772414623609905],
  176. // [111.00909744947245, 21.772878618505956],
  177. // [111.00744610353439, 21.774094523079953],
  178. // [111.00745562137854, 21.77564355219471]
  179. // ]
  180. // addPolygon(p1)
  181. }
  182. const startDraw = ref(false)
  183. // 开始框选
  184. const startRectangleSelection = () => {
  185. if (mouseTool.value) {
  186. startDraw.value = true
  187. mouseTool.value.rectangle({
  188. strokeColor: "#ffffff", // 框选区域边框颜色
  189. strokeOpacity: 1, // 边框透明度
  190. strokeWeight: 2, // 边框宽度
  191. fillColor: "#000000", // 填充颜色
  192. fillOpacity: 0.5, // 填充透明度
  193. }); // 启用矩形框选工具
  194. map.value.setDefaultCursor("crosshair");
  195. }
  196. };
  197. // 停止绘制矩形
  198. const stopDrawRectangle = () => {
  199. mouseTool.value.close(); // 关闭 mouseTool
  200. map.value.setDefaultCursor("default");
  201. };
  202. // 清除框选
  203. const clearSelection = () => {
  204. if (selectedArea.value) {
  205. map.value.remove(selectedArea.value); // 移除框选区域
  206. selectedArea.value = null;
  207. }
  208. resetData()
  209. selectedPoints.value = []; // 清空选中的点位
  210. };
  211. // 检查框选区域内的点位
  212. const checkPointsInArea = () => {
  213. if (!selectedArea.value) return;
  214. const bounds = selectedArea.value.getBounds(); // 获取框选区域的边界
  215. selectedPoints.value = allPoints.value.filter((point) => {
  216. return bounds.contains(point.lnglat); // 判断点位是否在框选区域内
  217. });
  218. selectedPoints.value.forEach(item =>{
  219. if(item.type==='defalutIcon'){
  220. item.icon.setContent(createMarkerImg())
  221. item.icon.setOffset(new window.AMap.Pixel(-18, -18))
  222. item.type = 'activeIcon'
  223. }else{
  224. item.icon.setContent(createMarkerContent(item.color))
  225. item.icon.setOffset(new window.AMap.Pixel(-7, -7))
  226. item.type = 'defalutIcon'
  227. }
  228. })
  229. };
  230. const totalList = ref([])
  231. function getSelectPoint(){
  232. totalList.value = selectedPoints.value.concat(pointList.value)
  233. const arr = totalList.value.filter(item =>item.type==='activeIcon')
  234. return arr
  235. }
  236. function getRegionList(farmId) {
  237. VE_API.region.list({farmId}).then(({data}) =>{
  238. data.map(item => {
  239. addPolygon(wktToAmapPolygon(item.wkt))
  240. })
  241. })
  242. }
  243. function addPolygon(data) {
  244. console.log('dddddd', data);
  245. let polygon = new window.AMap.Polygon({
  246. path: data,
  247. fillColor: '#FFE17E',
  248. strokeOpacity: 1,
  249. fillOpacity: 0.1,
  250. strokeColor: 'rgba(255, 255, 255, 0.3)',
  251. strokeWeight: 1,
  252. strokeStyle: 'solid',
  253. // strokeDasharray: [5, 5],
  254. });
  255. map.value.add(polygon)
  256. }
  257. defineExpose({getSelectPoint,initData,startRectangleSelection,stopDrawRectangle,clearSelection, getRegionList})
  258. // 组件挂载时初始化地图
  259. onMounted(() => {
  260. initMap()
  261. });
  262. onUnmounted(()=>{
  263. map.value.destroy()// 销毁地图实例以释放资源
  264. })
  265. function wktToAmapPolygon(wkt) {
  266. const geoJSON = wellknown.parse(wkt);
  267. if (!geoJSON || geoJSON.type !== 'MultiPolygon') {
  268. console.error('Invalid WKT or not a MULTIPOLYGON');
  269. return [];
  270. }
  271. // MULTIPOLYGON 的坐标结构:[[[ [环1], [环2], ... ]]],取第一个环
  272. return geoJSON.coordinates[0][0].map(coord => [coord[0], coord[1]]);
  273. }
  274. </script>
  275. <style scoped>
  276. #map-container {
  277. width: 100%;
  278. height: 100%;
  279. }
  280. </style>