Skip to content

Commit d8fdede

Browse files
committed
Enhance map texture loading and error handling logic.
1 parent 27c782e commit d8fdede

2 files changed

Lines changed: 48 additions & 24 deletions

File tree

client/src/components/Map2D.jsx

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,44 @@ import * as THREE from 'three';
33

44
const API_KEY = import.meta.env.VITE_GOOGLE_MAPS_API_KEY;
55

6+
// Debug environment variable loading on initial load only
7+
if (!window._mapDebugLogged) {
8+
console.log('Environment check:', {
9+
hasAPIKey: !!API_KEY,
10+
apiKeyLength: API_KEY ? API_KEY.length : 0
11+
});
12+
window._mapDebugLogged = true;
13+
}
14+
615
const Map2D = ({ onMapLoaded }) => {
716
const [mapTexture, setMapTexture] = useState(null);
817
const [loading, setLoading] = useState(false);
918
const [error, setError] = useState(null);
10-
const retryCountRef = useRef(0);
11-
const maxRetries = 3;
19+
const [hasAttempted, setHasAttempted] = useState(false);
20+
const onMapLoadedRef = useRef(onMapLoaded);
21+
22+
// Keep the ref updated
23+
useEffect(() => {
24+
onMapLoadedRef.current = onMapLoaded;
25+
}, [onMapLoaded]);
1226

1327
const loadMapTexture = useCallback(() => {
28+
// Prevent multiple attempts
29+
if (hasAttempted || loading || mapTexture) {
30+
return;
31+
}
32+
1433
if (!API_KEY) {
15-
console.warn('Google Maps API key not found');
34+
console.error('VITE_GOOGLE_MAPS_API_KEY environment variable not found');
35+
setError(new Error('Google Maps API key not configured'));
36+
setHasAttempted(true);
1637
return;
1738
}
1839

40+
console.log('Loading Google Maps texture...');
1941
setLoading(true);
2042
setError(null);
43+
setHasAttempted(true);
2144

2245
// San Francisco bounds
2346
const SF_CENTER = { lat: 37.7749, lng: -122.4194 };
@@ -52,6 +75,8 @@ const Map2D = ({ onMapLoaded }) => {
5275

5376
const mapUrl = `https://maps.googleapis.com/maps/api/staticmap?center=${SF_CENTER.lat},${SF_CENTER.lng}&zoom=${zoom}&size=${size}&maptype=${mapType}&${darkStyles}&key=${API_KEY}`;
5477

78+
// Map URL ready for loading
79+
5580
const textureLoader = new THREE.TextureLoader();
5681

5782
textureLoader.load(
@@ -63,7 +88,6 @@ const Map2D = ({ onMapLoaded }) => {
6388
texture.minFilter = THREE.LinearFilter;
6489
setMapTexture(texture);
6590
setLoading(false);
66-
retryCountRef.current = 0;
6791

6892
// Simple coordinate transformation for 2D mapping
6993
const latLngToECEF = (lat, lng, alt = 0) => {
@@ -92,37 +116,35 @@ const Map2D = ({ onMapLoaded }) => {
92116
};
93117

94118
// Only call onMapLoaded after texture is successfully loaded
95-
if (onMapLoaded) {
96-
onMapLoaded({ latLngToECEF, ecefToLatLng });
119+
if (onMapLoadedRef.current) {
120+
onMapLoadedRef.current({ latLngToECEF, ecefToLatLng });
97121
}
98122
},
99123
// onProgress - Progress callback
100124
(progress) => {
101125
// Optional: handle loading progress
102126
},
103-
// onError - Error callback
104-
(error) => {
105-
console.error('Error loading Google Maps texture:', error);
106-
setLoading(false);
107-
setError(error);
108-
109-
// Retry logic
110-
if (retryCountRef.current < maxRetries) {
111-
retryCountRef.current++;
112-
console.log(`Retrying map load... (${retryCountRef.current}/${maxRetries})`);
113-
setTimeout(() => loadMapTexture(), 1000 * retryCountRef.current); // Exponential backoff
114-
} else {
115-
console.error('Max retries reached for Google Maps texture loading');
116-
}
117-
}
127+
// onError - Error callback
128+
(error) => {
129+
console.error('Error loading Google Maps texture:', error);
130+
console.error('This could be due to:');
131+
console.error('1. Invalid or missing VITE_GOOGLE_MAPS_API_KEY');
132+
console.error('2. API key not enabled for Static Maps API');
133+
console.error('3. Network connectivity issues');
134+
console.error('4. CORS restrictions');
135+
console.error('Please check your VITE_GOOGLE_MAPS_API_KEY and ensure Static Maps API is enabled');
136+
137+
setLoading(false);
138+
setError(error);
139+
}
118140
);
119-
}, [onMapLoaded]);
141+
}, []); // Empty dependency array to prevent recreation
120142

121143
useEffect(() => {
122-
if (API_KEY) {
144+
if (API_KEY && !hasAttempted) {
123145
loadMapTexture();
124146
}
125-
}, [loadMapTexture]);
147+
}, [API_KEY, hasAttempted, loadMapTexture]);
126148

127149
return (
128150
<group>

client/src/examples/SimCity.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ export default function SimCityExample() {
593593
<SceneContent
594594
state={state}
595595
coordinateTransformer={coordinateTransformer}
596+
mapLoaded={mapLoaded}
596597
setMapLoaded={setMapLoaded}
597598
setCoordinateTransformer={setCoordinateTransformer}
598599
/>
@@ -652,6 +653,7 @@ export default function SimCityExample() {
652653
const SceneContent = ({
653654
state,
654655
coordinateTransformer,
656+
mapLoaded,
655657
setMapLoaded,
656658
setCoordinateTransformer,
657659
}) => {

0 commit comments

Comments
 (0)