crossSetionTencentmap.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <template>
  2. <div class="map-page">
  3. <div ref="mapContainer" class="map-container"></div>
  4. </div>
  5. </template>
  6. <script setup>
  7. import { ref, reactive, onMounted, onBeforeUnmount } from 'vue';
  8. import { wgs84togcj02 } from 'coordtransform';
  9. const isMapReady = ref(false)
  10. const TMap = ref(null); // 存储腾讯地图SDK实例
  11. const mapContainer = ref(null); // 地图容器DOM
  12. const state = reactive({ excelData: [] }); // 存储处理后的数据
  13. const infoWindow = ref(null); // 信息窗口实例
  14. let map = null; // 地图实例
  15. let markersLayer = null; // 标记图层实例
  16. // 腾讯地图配置
  17. const tMapConfig = reactive({
  18. key: import.meta.env.VITE_TMAP_KEY, // 必须配置环境变量(腾讯地图开发者密钥)
  19. })
  20. // 加载腾讯地图SDK
  21. const loadSDK = () => {
  22. return new Promise((resolve, reject) => {
  23. if (window.TMap) {
  24. TMap.value = window.TMap
  25. return resolve(window.TMap)
  26. }
  27. const script = document.createElement('script')
  28. script.src = `https://map.qq.com/api/gljs?v=2.exp&libraries=basic&key=${tMapConfig.key}&callback=initTMap`
  29. window.initTMap = () => {
  30. TMap.value = window.TMap
  31. resolve(window.TMap)
  32. }
  33. script.onerror = (err) => {
  34. reject(`地图加载失败: ${err.message}`)
  35. document.head.removeChild(script)
  36. }
  37. document.head.appendChild(script)
  38. })
  39. }
  40. // 初始化断面数据(直接嵌入你的Excel数据)
  41. const initData = () => {
  42. const rawData = [
  43. { "断面编号": 0, "所属河流": "浈江", "断面位置": "小古录", "所属区县": "始兴县", "经度": 114.208543, "纬度": 25.059851, "Cd(ug/L)": 0.11 },
  44. { "断面编号": 1, "所属河流": "浈江", "断面位置": "长坝", "所属区县": "仁化县", "经度": 113.692874, "纬度": 24.874845, "Cd(ug/L)": 1.116 },
  45. { "断面编号": 2, "所属河流": "浈江", "断面位置": "东河桥", "所属区县": "浈江区", "经度": 113.601631, "纬度": 24.80784, "Cd(ug/L)": 3.46 },
  46. { "断面编号": 3, "所属河流": "武江", "断面位置": "坪石", "所属区县": "乐昌市", "经度": 113.066281, "纬度": 25.274421, "Cd(ug/L)": 0.98 },
  47. { "断面编号": 4, "所属河流": "武江", "断面位置": "乐昌", "所属区县": "乐昌市", "经度": 113.338782, "纬度": 25.129212, "Cd(ug/L)": 0.11 },
  48. { "断面编号": 5, "所属河流": "武江", "断面位置": "武江桥", "所属区县": "乐昌市", "经度": 113.349815, "纬度": 25.120278, "Cd(ug/L)": 0.15 },
  49. { "断面编号": 6, "所属河流": "北江", "断面位置": "九公里", "所属区县": "浈江区", "经度": 113.580758, "纬度": 24.761299, "Cd(ug/L)": 7.83 },
  50. { "断面编号": 7, "所属河流": "北江", "断面位置": "白土", "所属区县": "曲江区", "经度": 113.531284, "纬度": 24.679958, "Cd(ug/L)": 5.94 },
  51. { "断面编号": 8, "所属河流": "浈江", "断面位置": "昆仑水站", "所属区县": "南雄市", "经度": 114.3629285, "纬度": 25.10053746, "Cd(ug/L)": 0.517 },
  52. { "断面编号": 9, "所属河流": "北江", "断面位置": "白沙", "所属区县": "曲江", "经度": 113.5707136, "纬度": 24.58139261, "Cd(ug/L)": 1.54 },
  53. { "断面编号": 10, "所属河流": "浈江", "断面位置": "周田水站", "所属区县": "仁化县", "经度": 113.8293461, "纬度": 24.97851516, "Cd(ug/L)": 0.182 },
  54. { "断面编号": 11, "所属河流": "武江", "断面位置": "坪石水站", "所属区县": "乐昌市", "经度": 113.0467854, "纬度": 25.28883459, "Cd(ug/L)": 1.071 }
  55. ];
  56. // 处理坐标(WGS84转GCJ02,腾讯地图用GCJ02)
  57. state.excelData = rawData.map(item => {
  58. const lng = Number(item.经度);
  59. const lat = Number(item.纬度);
  60. if (isNaN(lat) || isNaN(lng)) {
  61. console.error('无效经纬度:', item);
  62. return null;
  63. }
  64. const [gcjLng, gcjLat] = wgs84togcj02(lng, lat); // 坐标转换
  65. return {
  66. id: item.断面编号,
  67. river: item.所属河流,
  68. location: item.断面位置,
  69. district: item.所属区县,
  70. cdValue: item["Cd(ug/L)"],
  71. latitude: gcjLat,
  72. longitude: gcjLng,
  73. };
  74. }).filter(item => item !== null);
  75. }
  76. // 初始化地图
  77. const initMap = async () => {
  78. try {
  79. await loadSDK()
  80. // 创建地图实例(中心设为数据区域:粤北)
  81. map = new TMap.value.Map(mapContainer.value, {
  82. center: new TMap.value.LatLng(25.2, 114), //前大往下,后大往左
  83. zoom: 9.8,
  84. minZoom: 9.8,
  85. maxZoom: 14,
  86. })
  87. // 创建标记图层
  88. markersLayer = new TMap.value.MultiMarker({
  89. map: map,
  90. styles: {
  91. default: new TMap.value.MarkerStyle({
  92. width: 30,
  93. height: 30,
  94. src: 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZD0iTTAgMGgyNHYyNEgweiIgZmlsbD0ibm9uZSIvPjxwYXRoIGQ9Ik0xMiAyTDIgMjJoMjBMMTIgMnoiIGZpbGw9IiMxRTg4RTUiLz48L3N2Zz4='
  95. })
  96. }
  97. });
  98. // 绑定标记点击事件
  99. markersLayer.on('click', handleMarkerClick);
  100. // 加载数据并渲染标记
  101. initData();
  102. updateMarkers();
  103. isMapReady.value = true;
  104. } catch (err) {
  105. console.error('地图初始化失败:', err);
  106. error.value = err.message;
  107. }
  108. }
  109. // 更新标记点
  110. const updateMarkers = () => {
  111. const geometries = state.excelData.map(item => ({
  112. id: String(item.id), // 统一转字符串,避免类型错误
  113. styleId: 'default',
  114. position: new TMap.value.LatLng(item.latitude, item.longitude),
  115. properties: {
  116. title: item.location, // 断面位置作为标题
  117. }
  118. }));
  119. markersLayer.setGeometries(geometries);
  120. }
  121. // 标记点击事件(直接用本地数据)
  122. const handleMarkerClick = (e) => {
  123. const marker = e.geometry;
  124. if (!marker) return;
  125. // 查找本地数据
  126. const markerId = String(marker.id);
  127. const matchedData = state.excelData.find(item => String(item.id) === markerId);
  128. if (!matchedData) {
  129. console.error('未找到数据:', markerId);
  130. return;
  131. }
  132. // 构建信息窗口内容
  133. const content = `
  134. <div class="water-info-window">
  135. <h3 class="info-title">断面编号:${matchedData.id}</h3>
  136. <div class="info-content">
  137. <div class="info-row">
  138. <span class="info-label">所属河流:</span>
  139. <span class="info-value">${matchedData.river}</span>
  140. </div>
  141. <div class="info-row">
  142. <span class="info-label">断面位置:</span>
  143. <span class="info-value">${matchedData.location}</span>
  144. </div>
  145. <div class="info-row">
  146. <span class="info-label">所属区县:</span>
  147. <span class="info-value">${matchedData.district}</span>
  148. </div>
  149. <div class="info-row">
  150. <span class="info-label">Cd含量:</span>
  151. <span class="info-value">${matchedData.cdValue} ug/L</span>
  152. </div>
  153. </div>
  154. </div>
  155. `;
  156. // 关闭之前的信息窗口
  157. if (infoWindow.value) {
  158. infoWindow.value.close();
  159. }
  160. // 打开新信息窗口
  161. infoWindow.value = new TMap.value.InfoWindow({
  162. map: map,
  163. position: marker.position,
  164. content,
  165. offset: { x: 0, y: -32 } // 向上偏移,避免遮挡标记
  166. });
  167. infoWindow.value.open();
  168. }
  169. // 生命周期
  170. onMounted(async () => {
  171. try {
  172. await loadSDK();
  173. await initMap();
  174. q //监听窗口大小变化,调整图表
  175. window.addEventListener('resize',()=>{
  176. if(chart){
  177. chart.resize();
  178. }
  179. })
  180. } catch (err) {
  181. error.value = err.message;
  182. }
  183. })
  184. onBeforeUnmount(() => {
  185. if (markersLayer) markersLayer.setMap(null); // 销毁标记图层
  186. if (infoWindow.value) infoWindow.value.close(); // 关闭信息窗口
  187. })
  188. </script>
  189. <style scoped>
  190. .map-page {
  191. width: 100%;
  192. margin: 0 auto 24px; /* 水平居中,底部间距24px */
  193. background-color: white;
  194. border-radius: 12px;
  195. padding: 20px;
  196. box-sizing: border-box;
  197. }
  198. /* 地图容器样式 */
  199. .map-container {
  200. width: 100%;
  201. height: 550px; /* 固定高度,确保地图显示完整 */
  202. margin: 1rem auto;
  203. border-radius: 12px;
  204. }
  205. /* 表格容器 */
  206. .table-page {
  207. width: 100%;
  208. background-color: rgba(255, 255, 255, 0.85);
  209. border-radius: 12px;
  210. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  211. overflow: hidden;
  212. transition: transform 0.3s ease;
  213. }
  214. .table-page:hover {
  215. transform: translateY(-3px);
  216. }
  217. .table-container {
  218. padding: 20px;
  219. flex: 1;
  220. overflow: auto;
  221. }
  222. .table-title {
  223. text-align: left;
  224. margin: 0;
  225. padding: 15px 15px 15px 24px;
  226. position: relative;
  227. font-size: 1.5rem;
  228. font-weight: 600;
  229. color: #1e88e5;
  230. line-height: 1.2;
  231. background-color: rgba(30, 136, 229, 0.08);
  232. border-bottom: 1px solid rgba(30, 136, 229, 0.15);
  233. }
  234. /* 蓝色小方块 */
  235. .table-title::before {
  236. content: "";
  237. position: absolute;
  238. left: 8px;
  239. top: 50%;
  240. transform: translateY(-50%);
  241. width: 8px;
  242. height: 8px;
  243. background-color: #1e88e5;
  244. border-radius: 50%;
  245. }
  246. .data-table {
  247. width: 100%;
  248. border-collapse: collapse;
  249. min-width: 800px;
  250. background-color: rgba(255, 255, 255, 0.7);
  251. }
  252. .data-table th,.data-table td {
  253. padding: 12px 15px;
  254. text-align: center;
  255. border: 1px solid rgba(229, 231, 235, 0.7);
  256. }
  257. .data-table th {
  258. background-color: rgba(243, 244, 246, 0.7);
  259. font-weight: bold;
  260. color: #1f2937;
  261. }
  262. .data-table tr:nth-child(even){
  263. background-color: rgba(249, 250, 251, 0.7);
  264. }
  265. .data-table tr:hover{
  266. background-color: rgba(243, 244, 246, 0.7);
  267. }
  268. /* 信息窗口核心调整 */
  269. :v-deep(.tmap-infowindow) {
  270. padding: 20px !important;
  271. min-width: 320px !important;
  272. font-size: 1.25rem !important;
  273. box-shadow: 0 4px 12px rgba(0,0,0,0.2) !important;
  274. border-radius: 8px !important;
  275. background: rgba(255, 255, 255, 0.95) !important;
  276. backdrop-filter: blur(4px);
  277. }
  278. .water-info-window {
  279. font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
  280. }
  281. .info-title {
  282. background: linear-gradient(135deg, rgba(30, 136, 229, 0.85), rgba(21, 101, 192, 0.85));
  283. color: white;
  284. font-size: 1.15rem;
  285. font-weight: 600;
  286. padding: 16px 20px;
  287. margin: 0;
  288. position: relative;
  289. letter-spacing: 0.5px;
  290. border-radius: 6px 6px 0 0;
  291. }
  292. .info-title:after {
  293. content: "";
  294. position: absolute;
  295. bottom: 0;
  296. left: 20px;
  297. right: 20px;
  298. height: 1px;
  299. background: linear-gradient(to right, rgba(255, 255, 255, 0.15), rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0.15));
  300. }
  301. .info-row {
  302. display: flex;
  303. margin-bottom: 15px;
  304. align-items: center;
  305. position: relative;
  306. padding-left: 20px;
  307. }
  308. .info-row:last-child {
  309. margin-bottom: 0;
  310. }
  311. .info-row:before {
  312. content: "";
  313. position: absolute;
  314. left: 0;
  315. top: 50%;
  316. transform: translateY(-50%);
  317. width: 14px;
  318. height: 14px;
  319. border-radius: 50%;
  320. background-color: #e3f2fd;
  321. border: 2px solid #90caf9;
  322. }
  323. .info-row:nth-child(4):before {
  324. background-color: #ffebee;
  325. border-color: #ffcdd2;
  326. }
  327. .info-label {
  328. flex: 0 0 100px;
  329. color: #546e7a;
  330. font-size: 0.95rem;
  331. font-weight: 500;
  332. text-align: right;
  333. padding-right: 15px;
  334. position: relative;
  335. }
  336. .info-label:after {
  337. content: ":";
  338. position: absolute;
  339. right: 5px;
  340. }
  341. .info-value {
  342. flex: 1;
  343. color: #263238;
  344. font-size: 1rem;
  345. background: rgba(248, 249, 250, 0.7);
  346. padding: 10px 15px;
  347. border-radius: 6px;
  348. border-left: 3px solid #64b5f6;
  349. font-weight: 500;
  350. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.03);
  351. transition: all 0.3s ease;
  352. }
  353. .info-row:nth-child(4) .info-value {
  354. color: #e53935;
  355. border-left-color: #ef9a9a;
  356. font-weight: 600;
  357. position: relative;
  358. }
  359. .info-row:nth-child(4) .info-value:after {
  360. content: "mg/L";
  361. position: absolute;
  362. right: 15px;
  363. font-size: 0.85rem;
  364. font-weight: normal;
  365. color: #78909c;
  366. }
  367. /* 响应式调整 */
  368. @media (max-width: 768px) {
  369. .map-page {
  370. width: 96%; /* 小屏幕稍窄,避免边缘拥挤 */
  371. }
  372. .map-container {
  373. height: 300px; /* 小屏幕缩短高度 */
  374. }
  375. }
  376. </style>