gisMapBaidu.tsx 5.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 GisMapBaidu(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, 16);
  56. // 启用滚轮放大缩小
  57. mapInstance.enableScrollWheelZoom(true);
  58. // 添加控件
  59. mapInstance.setMinZoom(16);
  60. mapInstance.setMaxZoom(19);
  61. mapInstance.setMapStyleV2({
  62. styleId: '62ce43ad2a1362c23e612b783d7406e7'
  63. });
  64. // 添加标记点
  65. if (markerData?.list?.dataOne) {
  66. markerData.list.dataOne.forEach((item: any) => {
  67. const icon = new window.BMapGL.Icon(fameng.src, new window.BMapGL.Size(23, 25), {
  68. anchor: new window.BMapGL.Size(10, 25),
  69. });
  70. const markers = new window.BMapGL.Point(item.lng, item.lat);
  71. const marker = new window.BMapGL.Marker(markers, {icon});
  72. mapInstance.addOverlay(marker);
  73. // 创建信息窗口
  74. const opts = {
  75. width: 300,
  76. title: item.name
  77. };
  78. const infoWindow = new window.BMapGL.InfoWindow(
  79. "倾斜角度:188deg<br/>" +
  80. "状态:正常运行<br/>" +
  81. "信噪比:16db<br/>" +
  82. "负责人:张三<br/>" +
  83. "联系方式:121234564", opts
  84. );
  85. marker.addEventListener('click', () => {
  86. mapInstance.openInfoWindow(infoWindow, markers);
  87. });
  88. });
  89. }
  90. if (markerData?.list?.dataTwo) {
  91. markerData.list.dataTwo.forEach((item: any) => {
  92. const icon = new window.BMapGL.Icon(shuifa.src, new window.BMapGL.Size(23, 25), {
  93. anchor: new window.BMapGL.Size(20, 0),
  94. });
  95. const markers = new window.BMapGL.Point(item.lng, item.lat);
  96. const marker = new window.BMapGL.Marker(markers, {icon});
  97. mapInstance.addOverlay(marker);
  98. // 创建信息窗口
  99. const opts = {
  100. width: 300,
  101. title: item.name
  102. };
  103. const infoWindow = new window.BMapGL.InfoWindow(
  104. "倾斜角度:188deg<br/>" +
  105. "状态:正常运行<br/>" +
  106. "信噪比:16db<br/>" +
  107. "负责人:张三<br/>" +
  108. "联系方式:121234564", opts
  109. );
  110. marker.addEventListener('click', () => {
  111. mapInstance.openInfoWindow(infoWindow, markers);
  112. });
  113. });
  114. }
  115. // 添加线路图层
  116. if (window.BMapGL.LineLayer && dataJson) {
  117. const lineLayer = new window.BMapGL.LineLayer({
  118. enablePicked: true,
  119. autoSelect: true,
  120. pickWidth: 30,
  121. pickHeight: 30,
  122. opacity: 1,
  123. selectedColor: 'blue',
  124. style: {
  125. sequence: false,
  126. marginLength: 18,
  127. borderMask: true,
  128. strokeWeight: 10,
  129. strokeLineJoin: 'round',
  130. strokeLineCap: 'round',
  131. strokeTextureUrl: ['match', ['get', 'name'], 'demo1', deom1.src, 'demo2', deom2.src, Uptow.src],
  132. }
  133. });
  134. lineLayer.setData(dataJson);
  135. mapInstance.addNormalLayer(lineLayer);
  136. }
  137. setMap(mapInstance);
  138. // 添加点击地图事件
  139. mapInstance.addEventListener('click', (e: any) => {
  140. console.log('点击位置经纬度:', e.latlng?.lng, e.latlng?.lat);
  141. });
  142. };
  143. if (!isClient) {
  144. return (
  145. <div className="flex items-center justify-center h-full bg-gray-100">
  146. <div className="text-gray-500">地图加载中...</div>
  147. </div>
  148. );
  149. }
  150. return (
  151. <div className="flex flex-col font-sans" style={{height: props.height + "vh"}}>
  152. <div className="flex flex-1 overflow-hidden">
  153. <div ref={mapRef} className="flex-1 h-full"></div>
  154. </div>
  155. </div>
  156. );
  157. }
  158. export default GisMapBaidu;