Overview
SkyCast IA provides seamless location detection using the browser’s Geolocation API, with a robust city search fallback for users who prefer manual selection or have location services disabled.useLocation Hook
TheuseLocation hook manages geolocation state and handles permission requests:
Return Values
Current user coordinates or
null if not availableError message if geolocation failed, or
null on successtrue while requesting location, false when completeFunction to manually re-request user location
Geolocation Configuration
The hook uses optimized settings for accuracy and performance:Configuration Details
enableHighAccuracy: true
enableHighAccuracy: true
- Uses GPS on mobile devices for pinpoint accuracy
- Falls back to WiFi triangulation on desktops
- May consume more battery on mobile
- Provides accuracy within 10-50 meters
timeout: 15000ms
timeout: 15000ms
- Gives mobile devices enough time to acquire GPS signal
- Prevents indefinite hanging on slow connections
- Triggers error callback if exceeded
- Desktop typically responds in 2-5 seconds
maximumAge: 0
maximumAge: 0
- Forces fresh location request every time
- Prevents stale cached coordinates
- Ensures accuracy for weather data
- No performance impact on modern browsers
Error Handling
The hook provides user-friendly error messages in Spanish:Error States
| Error Code | Message | Common Causes |
|---|---|---|
| 1 | Permission denied | User blocked location access |
| 2 | Position unavailable | No GPS/WiFi signal, airplane mode |
| 3 | Timeout | Slow GPS acquisition, weak signal |
SearchCity Component
TheSearchCity component provides autocomplete city search with history:
Features
Autocomplete
Real-time city suggestions as you type
Search History
Stores last 5 searches in localStorage
Mobile Optimized
Bottom sheet modal on mobile devices
Keyboard Navigation
Arrow keys to navigate, Enter to select
City Search Implementation
SkyCast uses OpenWeather’s Geocoding API for city search:API Integration
Debouncing
Search requests are debounced to reduce API calls:The 400ms debounce delay provides a good balance between responsiveness and API efficiency
Search History
Recent searches are stored in browser localStorage:History Features
- Stores up to 5 recent searches
- Automatically deduplicates entries
- Moves recently selected cities to top
- Persists across browser sessions
- Clears on logout (if auth implemented)
Permission Handling
For users who denied location access, show clear instructions:Show Browser-Specific Instructions
Display instructions based on user agent:
- Chrome: Click lock icon → Site settings → Location → Allow
- Firefox: Click shield icon → Permissions → Location → Allow
- Safari: Preferences → Websites → Location → Allow
Mobile Considerations
iOS Safari
- Requires HTTPS for geolocation
- May ask for permission twice (Safari + System)
- GPS takes 5-10 seconds to acquire
- Location works in standalone mode (PWA)
Android Chrome
- Faster GPS acquisition (2-5 seconds)
- Single permission prompt
- Works on HTTP for localhost only
- Background location requires additional permission
Usage Example
Complete location flow with error handling:Best Practices
Request Permission Early
Ask for location on app load, not after user interaction
Explain Why
Tell users why location access improves their experience
Provide Fallback
Always offer manual city search as alternative
Cache Results
Store last known location to reduce API calls
