|
284 | 284 | } |
285 | 285 | }, 1000); |
286 | 286 |
|
287 | | - // Poll GPS data and update map |
288 | | - if (gpsUpdateInterval) { |
289 | | - clearInterval(gpsUpdateInterval); |
290 | | - } |
291 | | - gpsUpdateInterval = setInterval(() => { |
292 | | - const gps = commandCenterClient?.gpsData; |
293 | | - // console.log(`[Page] Checking GPS for rover ${roverId}, client roverId:`, commandCenterClient?.roverId, 'GPS:', gps); |
294 | | - if (gps) { |
295 | | - // console.log('GPS data available:', gps.latitude, gps.longitude); |
296 | | - if (roverMarker && map && L) { |
297 | | - roverGpsPosition = { lat: gps.latitude, lng: gps.longitude }; |
298 | | - roverMarker.setLatLng([gps.latitude, gps.longitude]); |
299 | | - map.setView([gps.latitude, gps.longitude], map.getZoom()); |
300 | | - // console.log('✓ Updated rover position on map:', gps.latitude, gps.longitude); |
301 | | - } else { |
302 | | - console.warn('Map or marker not ready:', { roverMarker: !!roverMarker, map: !!map, L: !!L }); |
| 287 | + // Poll GPS data and update map every 3 seconds |
| 288 | + if (gpsUpdateInterval) { |
| 289 | + clearInterval(gpsUpdateInterval); |
| 290 | + } |
| 291 | + gpsUpdateInterval = setInterval(() => { |
| 292 | + const gps = commandCenterClient?.gpsData; |
| 293 | + |
| 294 | + // Validate GPS data before updating |
| 295 | + if (gps && gps.latitude && gps.longitude && |
| 296 | + !isNaN(gps.latitude) && !isNaN(gps.longitude) && |
| 297 | + Math.abs(gps.latitude) <= 90 && Math.abs(gps.longitude) <= 180) { |
| 298 | + |
| 299 | + // Only update if map and marker are ready |
| 300 | + if (roverMarker && map && L) { |
| 301 | + // Update stored position |
| 302 | + roverGpsPosition = { lat: gps.latitude, lng: gps.longitude }; |
| 303 | + |
| 304 | + // Smoothly update marker position |
| 305 | + roverMarker.setLatLng([gps.latitude, gps.longitude]); |
| 306 | + |
| 307 | + // Only pan the map if the rover has moved significantly (> 0.0001 degrees ~11m) |
| 308 | + const center = map.getCenter(); |
| 309 | + const distance = Math.sqrt( |
| 310 | + Math.pow(center.lat - gps.latitude, 2) + |
| 311 | + Math.pow(center.lng - gps.longitude, 2) |
| 312 | + ); |
| 313 | + |
| 314 | + if (distance > 0.0001) { |
| 315 | + // Use panTo for smooth movement instead of setView |
| 316 | + map.panTo([gps.latitude, gps.longitude], { |
| 317 | + animate: true, |
| 318 | + duration: 0.5, |
| 319 | + noMoveStart: true |
| 320 | + }); |
303 | 321 | } |
| 322 | + } else { |
| 323 | + console.warn('Map or marker not ready:', { roverMarker: !!roverMarker, map: !!map, L: !!L }); |
304 | 324 | } |
305 | | - }, 1000); |
306 | | -
|
307 | | - const status = commandCenterClient.status; |
| 325 | + } |
| 326 | + // If GPS data is invalid or missing, keep using previous position (no update) |
| 327 | + }, 3000); const status = commandCenterClient.status; |
308 | 328 | connectionStatus = status.isConnected ? 'Connected' : 'Disconnected'; |
309 | 329 | sensorData.isConnected = status.isConnected; |
310 | 330 |
|
|
0 commit comments