getLatLng static method
Implementation
static Future<LocationResult> getLatLng({
int retryCount = 2,
Duration timeout = const Duration(seconds: 3),
}) async {
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return const LocationResult(
error: LocationErrorType.serviceDisabled,
message: 'Location service is disable',
);
}
int attempt = 0;
while (attempt <= retryCount) {
try {
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
}
if (permission == LocationPermission.denied) {
return const LocationResult(
error: LocationErrorType.permissionDenied,
message: 'Location permission denied',
);
}
if (permission == LocationPermission.deniedForever) {
return const LocationResult(
error: LocationErrorType.permissionDeniedForever,
message: 'Location permission permanently denied',
);
}
if (kIsWeb) {
return _getWebPosition(timeout: timeout, retryCount: retryCount);
}
final position = await Geolocator.getCurrentPosition(
locationSettings: LocationSettings(accuracy: LocationAccuracy.low),
).timeout(timeout);
return LocationResult(position: position);
} on TimeoutException {
if (attempt == retryCount) {
return const LocationResult(
error: LocationErrorType.timeout,
message: 'Location request timed out',
);
}
} catch (e) {
if (attempt == retryCount) {
return LocationResult(
error: LocationErrorType.unknown,
message: e.toString(),
);
}
}
attempt++;
}
return const LocationResult(
error: LocationErrorType.unknown,
message: 'Unknown location error',
);
}