|
1 | 1 | import thermal from '@/utils/MapCompute/ThermalMapData.json'; |
2 | 2 | import { iconData } from '@/utils/MapCompute/dataEnd'; |
3 | 3 | import { ProCard } from '@ant-design/pro-components'; |
4 | | -import { message } from 'antd'; |
| 4 | +import { Button, message } from 'antd'; |
5 | 5 | import * as Cesium from 'cesium'; |
6 | 6 | import 'cesium/Build/Cesium/Widgets/widgets.css'; |
7 | 7 | import { useEffect, useState } from 'react'; |
@@ -140,10 +140,79 @@ const HaiAirPosture = () => { |
140 | 140 | }; |
141 | 141 | }, []); |
142 | 142 |
|
| 143 | + const handleLonLat = () => { |
| 144 | + function convertDMSToDD(dmsStr: string) { |
| 145 | + try { |
| 146 | + // 移除空格,保留数字、正负号和度分秒符号 |
| 147 | + const cleanStr = dmsStr.trim(); |
| 148 | + |
| 149 | + // 获取正负号 |
| 150 | + const sign = cleanStr.startsWith('-') ? -1 : 1; |
| 151 | + |
| 152 | + // 提取数字部分 |
| 153 | + const matches = cleanStr.match(/([+-]?\d+)˚(\d+)'(\d+)''/); |
| 154 | + if (!matches) { |
| 155 | + console.error('Invalid DMS format:', dmsStr); |
| 156 | + return null; |
| 157 | + } |
| 158 | + |
| 159 | + // 解析度分秒 |
| 160 | + const degrees = parseFloat(matches[1]); |
| 161 | + const minutes = parseFloat(matches[2]); |
| 162 | + const seconds = parseFloat(matches[3]); |
| 163 | + |
| 164 | + // 验证数值 |
| 165 | + console.log('Parsed values:', { |
| 166 | + degrees, |
| 167 | + minutes, |
| 168 | + seconds, |
| 169 | + sign, |
| 170 | + }); |
| 171 | + |
| 172 | + // 转换为十进制度数 |
| 173 | + const decimal = sign * (Math.abs(degrees) + minutes / 60 + seconds / 3600); |
| 174 | + |
| 175 | + console.log('Converted decimal:', decimal); |
| 176 | + |
| 177 | + return decimal; |
| 178 | + } catch (error) { |
| 179 | + console.error('Conversion error:', error); |
| 180 | + return null; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + let longitude = "+115˚12'266''"; |
| 185 | + let latitude = "-29˚33'123''"; |
| 186 | + |
| 187 | + const lonDD = convertDMSToDD(longitude); |
| 188 | + const latDD = convertDMSToDD(latitude); |
| 189 | + |
| 190 | + console.log('Final coordinates:', { lonDD, latDD }); |
| 191 | + |
| 192 | + // 确保坐标有效再创建位置 |
| 193 | + if (lonDD !== null && latDD !== null && !isNaN(lonDD) && !isNaN(latDD)) { |
| 194 | + const position = Cesium.Cartesian3.fromDegrees(lonDD, latDD); |
| 195 | + |
| 196 | + viewer.entities.add({ |
| 197 | + position: position, |
| 198 | + billboard: { |
| 199 | + image: require('@/assets/Detection.png'), |
| 200 | + verticalOrigin: Cesium.VerticalOrigin.BOTTOM, |
| 201 | + scale: 0.6, |
| 202 | + }, |
| 203 | + }); |
| 204 | + } else { |
| 205 | + console.error('Invalid coordinates calculated'); |
| 206 | + } |
| 207 | + }; |
| 208 | + |
143 | 209 | return ( |
144 | 210 | <> |
145 | 211 | <ProCard> |
146 | 212 | {contextHolder} |
| 213 | + <Button className="mb-2" onClick={() => handleLonLat()}> |
| 214 | + 经纬度 |
| 215 | + </Button> |
147 | 216 | <div id="cesiumContainer" /> |
148 | 217 | </ProCard> |
149 | 218 | </> |
|
0 commit comments