1- import React , { useState , useEffect } from 'react' ;
1+ import React , { useState , useEffect , useRef } from 'react' ;
22import { Typography , Space , Button } from 'antd' ;
33import styled from 'styled-components' ;
44import { WindDataAtLonLat , WindLayer } from 'cesium-wind-layer' ;
@@ -140,6 +140,7 @@ export const SpeedQuery: React.FC<SpeedQueryProps> = ({ windLayer, viewer }) =>
140140 const [ queryResult , setQueryResult ] = useState < WindData | null > ( null ) ;
141141 const [ location , setLocation ] = useState < { lon : number ; lat : number } | null > ( null ) ;
142142 const [ showInterpolated , setShowInterpolated ] = useState ( true ) ;
143+ const lastLocationRef = useRef < { lon : number ; lat : number } | null > ( null ) ;
143144
144145 const calculateWindDirection = ( u : number , v : number ) : number => {
145146 // 使用 atan2 计算角度,注意参数顺序:atan2(y, x)
@@ -162,20 +163,12 @@ export const SpeedQuery: React.FC<SpeedQueryProps> = ({ windLayer, viewer }) =>
162163 } ;
163164
164165 useEffect ( ( ) => {
165- if ( ! viewer || ! windLayer ) return ;
166+ if ( ! windLayer ) return ;
166167
167- const handler = new ScreenSpaceEventHandler ( viewer . scene . canvas ) ;
168- const handleClick = ( movement : any ) => {
169- const cartesian = viewer . camera . pickEllipsoid ( movement . position ) ;
170- if ( cartesian ) {
171- const cartographic = Cartographic . fromCartesian ( cartesian ) ;
172- const lon = CesiumMath . toDegrees ( cartographic . longitude ) ;
173- const lat = CesiumMath . toDegrees ( cartographic . latitude ) ;
174-
168+ const handleDataChange = ( ) => {
169+ if ( lastLocationRef . current ) {
175170 try {
176- const result = windLayer . getDataAtLonLat ( lon , lat ) ;
177- setLocation ( { lon, lat } ) ;
178-
171+ const result = windLayer . getDataAtLonLat ( lastLocationRef . current . lon , lastLocationRef . current . lat ) ;
179172 if ( result ) {
180173 const data = showInterpolated ? result . interpolated : result . original ;
181174 const direction = calculateWindDirection ( data . u , data . v ) ;
@@ -190,13 +183,56 @@ export const SpeedQuery: React.FC<SpeedQueryProps> = ({ windLayer, viewer }) =>
190183 }
191184 } ;
192185
186+ // Add event listener for data changes
187+ windLayer . addEventListener ( 'dataChange' , handleDataChange ) ;
188+
189+ return ( ) => {
190+ // Remove event listener when component unmounts or windLayer changes
191+ windLayer . removeEventListener ( 'dataChange' , handleDataChange ) ;
192+ } ;
193+ } , [ windLayer , showInterpolated ] ) ;
194+
195+ useEffect ( ( ) => {
196+ if ( ! location || ! windLayer ) return ;
197+
198+ lastLocationRef . current = location ;
199+
200+ try {
201+ const result = windLayer . getDataAtLonLat ( location . lon , location . lat ) ;
202+ if ( result ) {
203+ const data = showInterpolated ? result . interpolated : result . original ;
204+ const direction = calculateWindDirection ( data . u , data . v ) ;
205+ setQueryResult ( { ...result , direction } ) ;
206+ } else {
207+ setQueryResult ( null ) ;
208+ }
209+ } catch ( error ) {
210+ console . error ( 'Failed to get wind data:' , error ) ;
211+ setQueryResult ( null ) ;
212+ }
213+ } , [ windLayer , location , showInterpolated ] ) ;
214+
215+ useEffect ( ( ) => {
216+ if ( ! viewer || ! windLayer ) return ;
217+
218+ const handler = new ScreenSpaceEventHandler ( viewer . scene . canvas ) ;
219+ const handleClick = ( movement : any ) => {
220+ const cartesian = viewer . camera . pickEllipsoid ( movement . position ) ;
221+ if ( cartesian ) {
222+ const cartographic = Cartographic . fromCartesian ( cartesian ) ;
223+ const lon = CesiumMath . toDegrees ( cartographic . longitude ) ;
224+ const lat = CesiumMath . toDegrees ( cartographic . latitude ) ;
225+ setLocation ( { lon, lat } ) ;
226+ }
227+ } ;
228+
193229 handler . setInputAction ( handleClick , ScreenSpaceEventType . LEFT_CLICK ) ;
194230 handler . setInputAction ( handleClick , ScreenSpaceEventType . LEFT_DOUBLE_CLICK ) ;
195231
196232 return ( ) => {
197233 handler . destroy ( ) ;
198234 } ;
199- } , [ viewer , windLayer , showInterpolated ] ) ;
235+ } , [ viewer , windLayer ] ) ;
200236
201237 const currentData = queryResult ? ( showInterpolated ? queryResult . interpolated : queryResult . original ) : null ;
202238
0 commit comments