Skip to main content

Overview

The getGeolocationData() utility function retrieves the user’s current geolocation coordinates.

Import

Usage

Return Value

number | null
User’s latitude coordinate
number | null
User’s longitude coordinate
string | null
Error message if geolocation access failed or was denied
boolean
Whether geolocation is currently being fetched

Example

Error Handling

The function handles various geolocation scenarios:
  • Permission denied: User denied location access
  • Timeout: Location request timed out
  • Not available: Geolocation API not available
  • Position unavailable: Could not determine position
The browser will prompt the user for location permission on first use. Make sure to handle cases where the user denies permission.