Overview
The geolocation API provides functions to automatically detect a user’s location or resolve city/country queries. These functions are useful for providing location-based defaults in your application.Auto-detect Location
guessLocation
Automatically detect the user’s location based on their IP address.Promise<GeoLocation | null>
Location data or null if detection fails
How It Works
TheguessLocation function tries multiple free IP geolocation services in sequence:
- ip-api.com - First attempt
- ipapi.co - Fallback if first fails
- ipwho.is - Final fallback
Resolve City Query
guessCityCountry
Resolve a city name or query string to get coordinates and timezone.string
required
City name or search query (e.g., “London”, “New York”, “Tokyo”)
Promise<CityCountryGuess | null>
City data or null if not found
Geocoding Service
This function uses the Open-Meteo Geocoding API to resolve city queries. The API:- Returns the first (most relevant) match for ambiguous queries
- Supports international city names
- Provides accurate coordinates and timezone data
- Is free and requires no API key
Usage Examples
Auto-detect and Save Location
Search for City
Fallback Chain
Validate User Input
Get Timezone for Prayer Times
Integration with Prayer Times API
Error Handling
Both functions returnnull on failure rather than throwing errors. Always check the return value:
TypeScript Types
Privacy Considerations
IP geolocation APIs typically collect:
- User’s IP address
- Request timestamp
- User agent information
Performance
guessLocation: ~500ms (tries up to 3 services)guessCityCountry: ~200ms (single API call)