”’
Future getLocation() async {
setState(() {
_isLoading = true;
});
// Optimized location settings with decreased accuracy for sooner fetching
LocationSettings locationSettings;
if (defaultTargetPlatform == TargetPlatform.android) {
locationSettings = AndroidSettings(
accuracy: LocationAccuracy.decreased, // Utilizing decreased accuracy for sooner outcomes
forceLocationManager: true,
);
} else if (defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS) {
locationSettings = AppleSettings(
accuracy: LocationAccuracy.decreased, // Utilizing decreased accuracy for sooner outcomes
activityType: ActivityType.different,
// Lowered timeout since we anticipate sooner response
);
} else {
locationSettings = LocationSettings(
accuracy: LocationAccuracy.decreased, // Utilizing decreased accuracy for sooner outcomes
);
}
strive {
// Verify if location companies are enabled
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
serviceEnabled = await Geolocator.openLocationSettings();
if (!serviceEnabled) {
setState(() {
_isLoading = false;
});
AwesomeDialog(
context: context,
dialogType: DialogType.error,
animType: AnimType.scale,
title: 'Location Providers Disabled',
titleTextStyle: TextStyle(
coloration: Shade(0XFF0068B3),
fontWeight: FontWeight.daring,
fontSize: 16.sp,
),
desc: 'Please allow location companies to proceed.',
descTextStyle: TextStyle(
coloration: Shade(0XFF585F65),
fontWeight: FontWeight.w500,
fontSize: 12.sp,
),
btnOkText: 'Open Settings',
buttonsTextStyle: TextStyle(
fontSize: 14.sp,
coloration: Colours.white,
),
btnOkColor: Colours.blue,
btnOkOnPress: () async {
await Geolocator.openLocationSettings();
},
).present();
return;
}
}
// Verify and request permissions
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
setState(() {
_isLoading = false;
});
AwesomeDialog(
context: context,
dialogType: DialogType.warning,
animType: AnimType.scale,
title: 'Location Permission Required',
titleTextStyle: TextStyle(
coloration: Shade(0XFF0068B3),
fontWeight: FontWeight.daring,
fontSize: 16.sp,
),
desc: 'Please grant location permission to make use of this characteristic.',
descTextStyle: TextStyle(
coloration: Shade(0XFF585F65),
fontWeight: FontWeight.w500,
fontSize: 12.sp,
),
btnOkText: 'Request Permission',
buttonsTextStyle: TextStyle(
fontSize: 14.sp,
coloration: Colours.white,
),
btnOkColor: Colours.blue,
btnOkOnPress: () async {
await getLocation();
},
).present();
return;
}
}
if (permission == LocationPermission.deniedForever) {
setState(() {
_isLoading = false;
});
AwesomeDialog(
context: context,
dialogType: DialogType.error,
animType: AnimType.scale,
title: 'Location Permission Denied',
titleTextStyle: TextStyle(
coloration: Shade(0XFF0068B3),
fontWeight: FontWeight.daring,
fontSize: 16.sp,
),
desc: 'Location permission is completely denied. Please allow it from app settings.',
descTextStyle: TextStyle(
coloration: Shade(0XFF585F65),
fontWeight: FontWeight.w500,
fontSize: 12.sp,
),
btnOkText: 'Open Settings',
buttonsTextStyle: TextStyle(
fontSize: 14.sp,
coloration: Colours.white,
),
btnOkColor: Colours.blue,
btnOkOnPress: () async {
await Geolocator.openAppSettings();
},
).present();
return;
}
// Attempt to get the final recognized location first
Place? lastKnownPosition = await Geolocator.getLastKnownPosition(
forceAndroidLocationManager: true,
);
if (lastKnownPosition != null ) {
// Use final recognized place if it is current
latitude = lastKnownPosition.latitude;
longitude = lastKnownPosition.longitude;
print("lat and lon from lastknwnlocation ${latitude}${longitude}");
} else {
// Get present place with decreased accuracy settings
Place place = await Geolocator.getCurrentPosition(
locationSettings: locationSettings,
);
latitude = place.latitude;
longitude = place.longitude;
print("lat and lon from currentlocation ${latitude}${longitude}");
}
if (latitude != null && longitude != null) {
await getCurrentPlace();
} else {
setState(() {
_isLoading = false;
});
AwesomeDialog(
context: context,
dialogType: DialogType.error,
animType: AnimType.scale,
title: 'Location Not Discovered',
desc: 'Unable to fetch your present location. Please strive once more.',
btnOkText: 'Retry',
btnOkColor: Colours.blue,
btnOkOnPress: () async {
await getLocation();
},
).present();
}
} catch (e) {
print('Error fetching location: $e');
setState(() {
_isLoading = false;
});
// Extra particular error dealing with
String errorMessage="Didn't fetch location. Please strive once more.";
if (e is TimeoutException) {
errorMessage="Location fetch is taking too lengthy. Please verify your GPS sign and check out once more.";
}
AwesomeDialog(
context: context,
dialogType: DialogType.error,
animType: AnimType.scale,
title: 'Error',
titleTextStyle: TextStyle(
coloration: Shade(0XFF0068B3),
fontWeight: FontWeight.daring,
fontSize: 16.sp,
),
desc: errorMessage,
descTextStyle: TextStyle(
coloration: Shade(0XFF585F65),
fontWeight: FontWeight.w500,
fontSize: 12.sp,
),
btnOkText: 'Retry',
buttonsTextStyle: TextStyle(
fontSize: 14.sp,
coloration: Colours.white,
),
btnOkColor: Colours.blue,
btnOkOnPress: () async {
await getLocation();
},
).present();
} lastly {
setState(() {
_isLoading = false;
});
}
}
”’