gisMap.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use client'
  2. declare const window: Window & typeof globalThis & {
  3. BMapGL: any;
  4. initMap:() => void;
  5. };
  6. import React, {useEffect, useRef, useState} from 'react';
  7. import deom1 from "./assets/deom1.png";
  8. import deom2 from "./assets/deom2.png";
  9. import Uptow from "./assets/up-two.png";
  10. import markerData from "./assets/markerData.json";
  11. import fameng from "./assets/fameng.png";
  12. import shuifa from "./assets/shuifa.png";
  13. import dataJson from "./assets/index.json";
  14. import "./assets/BaiduMapPage.css";
  15. function GisMap(props:{height:string}) {
  16. // const BaiduMapPage = () => {
  17. const mapRef = useRef(null);
  18. const [map, setMap] = useState<{ destroy: () => void}>();
  19. var lineLayer: {
  20. setData: (arg0: {
  21. type: string;
  22. features: {
  23. type: string;
  24. properties: { name: string; };
  25. geometry: { type: string; coordinates: number[][]; };
  26. }[];
  27. }) => void;
  28. };
  29. var icon1 = deom1.src;
  30. var icon2 = deom2.src;
  31. var upTwo = Uptow.src;
  32. // 初始化地图
  33. useEffect(() => {
  34. // 检查是否已加载百度地图 GL JS API
  35. if (!window.BMapGL) {
  36. // 动态加载百度地图 GL JS API
  37. const script = document.createElement('script');
  38. script.src = `https://api.map.baidu.com/api?v=2.0&ak=jWDCUpsk33htQsF6IEwk4ctkERTOFFH0&type=webgl`;
  39. script.async = true;
  40. document.head.appendChild(script);
  41. window.initMap = () => {
  42. initializeMap();
  43. };
  44. } else {
  45. initializeMap();
  46. }
  47. return () => {
  48. // 清理工作
  49. if (map) {
  50. map.destroy();
  51. }
  52. };
  53. }, []);
  54. const initializeMap = () => {
  55. // 创建地图实例
  56. const mapInstance = new window.BMapGL.Map(mapRef.current);
  57. // 设置中心点坐标(北京)
  58. const point = new window.BMapGL.Point(110.39573035064166, 28.44628067667752);
  59. mapInstance.centerAndZoom(point, 16);
  60. // 启用滚轮放大缩小
  61. mapInstance.enableScrollWheelZoom(true);
  62. // 添加控件
  63. mapInstance.setMinZoom(16);
  64. mapInstance.setMaxZoom(19);
  65. mapInstance.setMapStyleV2({
  66. styleId: '62ce43ad2a1362c23e612b783d7406e7'
  67. });
  68. markerData.list.dataOne.forEach(item => {
  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, {
  74. icon: icon
  75. });
  76. mapInstance.addOverlay(marker);
  77. // 创建信息窗口
  78. let opts = {
  79. width: 300,
  80. title: item.name
  81. };
  82. let infoWindow = new window.BMapGL.InfoWindow("倾斜角度:" + '188deg' + "<br/>" +
  83. "状态:" + '正常运行' + "<br/>" +
  84. "信噪比:" + '16db' + "<br/>" +
  85. "负责人:" + '张三' + "<br/>" +
  86. "联系方式:" + '121234564', opts);
  87. // 点标记添加点击事件
  88. marker.addEventListener('click', function () {
  89. mapInstance.openInfoWindow(infoWindow, markers); // 开启信息窗口
  90. });
  91. })
  92. markerData.list.dataTwo.forEach(item => {
  93. const icon = new window.BMapGL.Icon(shuifa.src, new window.BMapGL.Size(23, 25), {
  94. anchor: new window.BMapGL.Size(20, 0),
  95. });
  96. const markers = new window.BMapGL.Point(item.lng, item.lat);
  97. const marker = new window.BMapGL.Marker(markers, {
  98. icon: icon
  99. });
  100. mapInstance.addOverlay(marker);
  101. // 创建信息窗口
  102. let opts = {
  103. width: 300,
  104. title: item.name
  105. };
  106. let infoWindow = new window.BMapGL.InfoWindow("倾斜角度:" + '188deg' + "<br/>" +
  107. "状态:" + '正常运行' + "<br/>" +
  108. "信噪比:" + '16db' + "<br/>" +
  109. "负责人:" + '张三' + "<br/>" +
  110. "联系方式:" + '121234564', opts);
  111. // 点标记添加点击事件
  112. marker.addEventListener('click', function () {
  113. mapInstance.openInfoWindow(infoWindow, markers); // 开启信息窗口
  114. });
  115. })
  116. if (!lineLayer) {
  117. 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, // 是否采用间隔填充纹理,默认false
  126. marginLength: 18, // 间隔距离,默认16,单位像素
  127. // borderColor: '#999',
  128. borderMask: true, // 是否受内部填充区域掩膜,默认true,如果存在borderWeight小于0,则自动切换false
  129. // borderWeight: ['match', ['get', 'name'], 'demo1', 2, 0], // 描边宽度,可以设置负值
  130. strokeWeight: 10, // 描边线宽度,默认0
  131. strokeLineJoin: 'round',//描边线连接处类型, 可选'miter', 'round', 'bevel'
  132. strokeLineCap: 'round',// 描边线端头类型,可选'round', 'butt', 'square',默认round
  133. // 填充纹理图片地址,默认是空。图片需要是竖向表达,在填充时会自动横向处理。
  134. strokeTextureUrl: ['match', ['get', 'name'], 'demo1', icon1, 'demo2', icon2, upTwo],
  135. }
  136. });
  137. }
  138. lineLayer.setData(dataJson);
  139. mapInstance.addNormalLayer(lineLayer);
  140. setMap(mapInstance);
  141. // 添加点击地图事件
  142. mapInstance.addEventListener('click', (e: any) => {
  143. console.log('点击位置经纬度:', e.latlng.lng, e.latlng.lat);
  144. });
  145. // };
  146. };
  147. return (
  148. <>
  149. <div className="flex flex-col font-sans" style={{height: props.height+"vh"}}>
  150. <div className="flex flex-1 overflow-hidden">
  151. <div ref={mapRef} className="flex-1 h-full"></div>
  152. </div>
  153. </div>
  154. </> );
  155. }
  156. export default GisMap;