requestLocationPermissionWeb method

Future<String> requestLocationPermissionWeb()

Implementation

Future<String> requestLocationPermissionWeb() async {
  try {
    // Check if running on HTTPS (required for geolocation)
    final protocol = web.window.location.protocol;
    final hostname = web.window.location.hostname;
    debugPrint(
      'Location permission check - Protocol: $protocol, Hostname: $hostname',
    );

    if (!protocol.startsWith('https') && hostname != 'localhost') {
      debugPrint(
        'Location permission requires HTTPS context - Current: $protocol://$hostname',
      );
      return 'denied';
    }

    final geolocation = web.window.navigator.geolocation;
    debugPrint('Geolocation: $geolocation');

    final completer = Completer<String>();

    final options = web.PositionOptions(
      enableHighAccuracy: false,
      timeout: 5000,
      maximumAge: 0,
    );

    geolocation.getCurrentPosition(
      (web.GeolocationPosition position) {
        completer.complete('granted');
      }.toJS,
      (web.GeolocationPositionError error) {
        debugPrint('Location error: ${error.code} - ${error.message}');
        if (error.code == 1) {
          // PERMISSION_DENIED
          completer.complete('denied');
        } else if (error.code == 2) {
          // POSITION_UNAVAILABLE
          completer.complete('denied');
        } else if (error.code == 3) {
          // TIMEOUT
          completer.complete('denied');
        } else {
          completer.complete('unsupported');
        }
      }.toJS,
      options,
    );

    return await completer.future;
  } catch (e) {
    debugPrint('Location permission error: $e');
    return 'error';
  }
}