gisMapJinggai.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use client'
  2. import React, {useEffect, useRef, useState} from 'react';
  3. import deom1 from "../assets/deom1.png";
  4. import deom2 from "../assets/deom2.png";
  5. import Uptow from "../assets/up-two.png";
  6. import markerData from "../assets/markerData.json";
  7. import fameng from "../assets/fameng.png";
  8. import shuifa from "../assets/shuifa.png";
  9. import dataJson from "../assets/index.json";
  10. import "../assets/BaiduMapPage.css";
  11. // 仅在浏览器环境中声明 window 类型
  12. declare global {
  13. interface Window {
  14. BMapGL: any;
  15. initMap: () => void;
  16. }
  17. }
  18. function GisMapJinggai(props: {height: string}) {
  19. const mapRef = useRef<HTMLDivElement>(null);
  20. const [map, setMap] = useState<any>(null);
  21. const [isClient, setIsClient] = useState(false);
  22. // 标记是否在浏览器环境
  23. useEffect(() => {
  24. setIsClient(true);
  25. }, []);
  26. // 初始化地图
  27. useEffect(() => {
  28. if (!isClient || !mapRef.current) return;
  29. // 检查是否已加载百度地图 GL JS API
  30. if (!window.BMapGL) {
  31. // 动态加载百度地图 GL JS API
  32. const script = document.createElement('script');
  33. script.src = `https://api.map.baidu.com/api?v=2.0&ak=jWDCUpsk33htQsF6IEwk4ctkERTOFFH0&type=webgl`;
  34. script.async = true;
  35. script.onload = () => {
  36. initializeMap();
  37. };
  38. document.head.appendChild(script);
  39. } else {
  40. initializeMap();
  41. }
  42. return () => {
  43. // 清理工作
  44. if (map && map.destroy) {
  45. map.destroy();
  46. }
  47. };
  48. }, [isClient]);
  49. const initializeMap = () => {
  50. if (!window.BMapGL || !mapRef.current) return;
  51. // 创建地图实例
  52. const mapInstance = new window.BMapGL.Map(mapRef.current);
  53. // 设置中心点坐标
  54. const point = new window.BMapGL.Point(110.39573035064166, 28.44628067667752);
  55. mapInstance.centerAndZoom(point, 15);
  56. // 启用滚轮放大缩小
  57. mapInstance.enableScrollWheelZoom(true);
  58. // 设置初始倾斜角度为30度(范围:0-45度)
  59. mapInstance.setTilt(45);
  60. // 添加控件
  61. mapInstance.setMinZoom(15);
  62. mapInstance.setMaxZoom(19);
  63. mapInstance.setMapStyleV2({
  64. styleId: '62ce43ad2a1362c23e612b783d7406e7'
  65. });
  66. // 添加标记点
  67. if (markerData?.list?.dataOne) {
  68. markerData.list.dataOne.forEach((item: any) => {
  69. const icon = new window.BMapGL.Icon(fameng.src, new window.BMapGL.Size(23, 25), {
  70. anchor: new window.BMapGL.Size(10, 25),
  71. });
  72. const markers = new window.BMapGL.Point(item.lng, item.lat);
  73. const marker = new window.BMapGL.Marker(markers, {icon});
  74. mapInstance.addOverlay(marker);
  75. // 创建信息窗口
  76. const opts = {
  77. width: 300,
  78. title: item.name
  79. };
  80. const infoWindow = new window.BMapGL.InfoWindow(
  81. "倾斜角度:188deg<br/>" +
  82. "状态:正常运行<br/>" +
  83. "信噪比:16db<br/>" +
  84. "负责人:张三<br/>" +
  85. "联系方式:121234564", opts
  86. );
  87. marker.addEventListener('click', () => {
  88. mapInstance.openInfoWindow(infoWindow, markers);
  89. });
  90. });
  91. }
  92. if (markerData?.list?.dataTwo) {
  93. markerData.list.dataTwo.forEach((item: any) => {
  94. const icon = new window.BMapGL.Icon(shuifa.src, new window.BMapGL.Size(23, 25), {
  95. anchor: new window.BMapGL.Size(20, 0),
  96. });
  97. const markers = new window.BMapGL.Point(item.lng, item.lat);
  98. const marker = new window.BMapGL.Marker(markers, {icon});
  99. mapInstance.addOverlay(marker);
  100. // 创建信息窗口
  101. const opts = {
  102. width: 300,
  103. title: item.name
  104. };
  105. const infoWindow = new window.BMapGL.InfoWindow(
  106. "倾斜角度:188deg<br/>" +
  107. "状态:正常运行<br/>" +
  108. "信噪比:16db<br/>" +
  109. "负责人:张三<br/>" +
  110. "联系方式:121234564", opts
  111. );
  112. marker.addEventListener('click', () => {
  113. mapInstance.openInfoWindow(infoWindow, markers);
  114. });
  115. });
  116. }
  117. // 添加线路图层
  118. if (window.BMapGL.LineLayer && dataJson) {
  119. const lineLayer = new window.BMapGL.LineLayer({
  120. enablePicked: true,
  121. autoSelect: true,
  122. pickWidth: 30,
  123. pickHeight: 30,
  124. opacity: 1,
  125. selectedColor: 'blue',
  126. style: {
  127. sequence: false,
  128. marginLength: 18,
  129. borderMask: true,
  130. strokeWeight: 10,
  131. strokeLineJoin: 'round',
  132. strokeLineCap: 'round',
  133. strokeTextureUrl: ['match', ['get', 'name'], 'demo1', deom1.src, 'demo2', deom2.src, Uptow.src],
  134. }
  135. });
  136. lineLayer.setData(dataJson);
  137. mapInstance.addNormalLayer(lineLayer);
  138. }
  139. setMap(mapInstance);
  140. // 添加点击地图事件
  141. mapInstance.addEventListener('click', (e: any) => {
  142. console.log('点击位置经纬度:', e.latlng?.lng, e.latlng?.lat);
  143. });
  144. };
  145. if (!isClient) {
  146. return (
  147. <div className="flex items-center justify-center h-full bg-gray-100">
  148. <div className="text-gray-500">地图加载中...</div>
  149. </div>
  150. );
  151. }
  152. return (
  153. <div className="flex flex-col font-sans" style={{height: props.height + "vh"}}>
  154. <div className="flex flex-1 overflow-hidden">
  155. <div ref={mapRef} className="flex-1 h-full"></div>
  156. </div>
  157. </div>
  158. );
  159. }
  160. export default GisMapJinggai;