|
|
|
|
@ -22,19 +22,35 @@ class LocationUtils {
|
|
|
|
|
bool isHuawei;
|
|
|
|
|
final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;
|
|
|
|
|
|
|
|
|
|
LocationUtils({required this.isShowConfirmDialog, required this.context, this.isHuawei = false, this.isShowLocationTimeoutDialog = true});
|
|
|
|
|
LocationUtils(
|
|
|
|
|
{required this.isShowConfirmDialog,
|
|
|
|
|
required this.context,
|
|
|
|
|
this.isHuawei = false,
|
|
|
|
|
this.isShowLocationTimeoutDialog = true});
|
|
|
|
|
|
|
|
|
|
void getCurrentLocation(
|
|
|
|
|
{bool isShowConfirmDialog = false,
|
|
|
|
|
bool isShowLocationTimeoutDialog = false,
|
|
|
|
|
Function(LatLng)? callBack}) async {
|
|
|
|
|
|
|
|
|
|
this.isShowConfirmDialog = isShowConfirmDialog;
|
|
|
|
|
this.isShowLocationTimeoutDialog = isShowLocationTimeoutDialog;
|
|
|
|
|
|
|
|
|
|
void getCurrentLocation({Function(LatLng)? callBack}) async {
|
|
|
|
|
if (Platform.isAndroid && isHuawei) {
|
|
|
|
|
_getHMSCurrentLocation(callBack!);
|
|
|
|
|
} else {
|
|
|
|
|
Geolocator.isLocationServiceEnabled().then((value) async {
|
|
|
|
|
if (value) {
|
|
|
|
|
await Geolocator.checkPermission().then((permission) async {
|
|
|
|
|
if (permission == LocationPermission.always || permission == LocationPermission.whileInUse) {
|
|
|
|
|
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 5)).then((value) {
|
|
|
|
|
if (permission == LocationPermission.always ||
|
|
|
|
|
permission == LocationPermission.whileInUse) {
|
|
|
|
|
Geolocator.getCurrentPosition(
|
|
|
|
|
desiredAccuracy: LocationAccuracy.medium,
|
|
|
|
|
timeLimit: Duration(seconds: 5))
|
|
|
|
|
.then((value) {
|
|
|
|
|
setLocation(value);
|
|
|
|
|
if (callBack != null) callBack(LatLng(value.latitude, value.longitude));
|
|
|
|
|
if (callBack != null)
|
|
|
|
|
callBack(LatLng(value.latitude, value.longitude));
|
|
|
|
|
}).catchError((err) {
|
|
|
|
|
print(err);
|
|
|
|
|
if (isShowConfirmDialog && isShowLocationTimeoutDialog) {
|
|
|
|
|
@ -45,17 +61,25 @@ class LocationUtils {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (permission == LocationPermission.denied || permission == LocationPermission.deniedForever) {
|
|
|
|
|
if (permission == LocationPermission.denied ||
|
|
|
|
|
permission == LocationPermission.deniedForever) {
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
Utils.showPermissionConsentDialog(context, TranslationBase.of(context).locationPermissionDialog, () async {
|
|
|
|
|
Utils.showPermissionConsentDialog(context,
|
|
|
|
|
TranslationBase.of(context).locationPermissionDialog,
|
|
|
|
|
() async {
|
|
|
|
|
final hasPermission = await _handlePermission();
|
|
|
|
|
if (hasPermission) {
|
|
|
|
|
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.medium, timeLimit: Duration(seconds: 10)).then((value) {
|
|
|
|
|
Geolocator.getCurrentPosition(
|
|
|
|
|
desiredAccuracy: LocationAccuracy.medium,
|
|
|
|
|
timeLimit: Duration(seconds: 10))
|
|
|
|
|
.then((value) {
|
|
|
|
|
setLocation(value);
|
|
|
|
|
if (callBack != null) callBack(LatLng(value.latitude, value.longitude));
|
|
|
|
|
if (callBack != null)
|
|
|
|
|
callBack(LatLng(value.latitude, value.longitude));
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
if (isShowConfirmDialog) showErrorLocationDialog(false, failureCallBack: () {});
|
|
|
|
|
if (isShowConfirmDialog)
|
|
|
|
|
showErrorLocationDialog(false, failureCallBack: () {});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
@ -63,7 +87,8 @@ class LocationUtils {
|
|
|
|
|
getCurrentLocation(callBack: callBack);
|
|
|
|
|
} else {
|
|
|
|
|
setZeroLocation();
|
|
|
|
|
if (isShowConfirmDialog) showErrorLocationDialog(false, failureCallBack: () {});
|
|
|
|
|
if (isShowConfirmDialog)
|
|
|
|
|
showErrorLocationDialog(false, failureCallBack: () {});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -71,7 +96,8 @@ class LocationUtils {
|
|
|
|
|
print(err);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
if (isShowConfirmDialog) showErrorLocationDialog(false, failureCallBack: () {});
|
|
|
|
|
if (isShowConfirmDialog)
|
|
|
|
|
showErrorLocationDialog(false, failureCallBack: () {});
|
|
|
|
|
}
|
|
|
|
|
}).catchError((err) {
|
|
|
|
|
print(err);
|
|
|
|
|
@ -134,14 +160,20 @@ class LocationUtils {
|
|
|
|
|
|
|
|
|
|
int _locationUpdateCbId = 0;
|
|
|
|
|
doIt() {
|
|
|
|
|
FusedLocationProviderClient locationService = FusedLocationProviderClient();
|
|
|
|
|
FusedLocationProviderClient locationService =
|
|
|
|
|
FusedLocationProviderClient();
|
|
|
|
|
LocationRequest locationRequest = LocationRequest();
|
|
|
|
|
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY;
|
|
|
|
|
locationRequest.interval = 1000;
|
|
|
|
|
List<LocationRequest> locationRequestList = <LocationRequest>[locationRequest];
|
|
|
|
|
LocationSettingsRequest locationSettingsRequest = LocationSettingsRequest(requests: locationRequestList);
|
|
|
|
|
List<LocationRequest> locationRequestList = <LocationRequest>[
|
|
|
|
|
locationRequest
|
|
|
|
|
];
|
|
|
|
|
LocationSettingsRequest locationSettingsRequest =
|
|
|
|
|
LocationSettingsRequest(requests: locationRequestList);
|
|
|
|
|
|
|
|
|
|
locationService.checkLocationSettings(locationSettingsRequest).then((settings) async {
|
|
|
|
|
locationService
|
|
|
|
|
.checkLocationSettings(locationSettingsRequest)
|
|
|
|
|
.then((settings) async {
|
|
|
|
|
_locationUpdateCbId = await locationService.requestLocationUpdatesCb(
|
|
|
|
|
locationRequest,
|
|
|
|
|
LocationCallback(onLocationResult: (locationResult) {
|
|
|
|
|
@ -208,7 +240,8 @@ class LocationUtils {
|
|
|
|
|
return dialog.showAlertDialog(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showErrorLocationDialog(bool isPermissionError, {Function()? failureCallBack}) {
|
|
|
|
|
showErrorLocationDialog(bool isPermissionError,
|
|
|
|
|
{Function()? failureCallBack}) {
|
|
|
|
|
setLocation(null);
|
|
|
|
|
ConfirmDialog dialog = new ConfirmDialog(
|
|
|
|
|
context: context,
|
|
|
|
|
@ -251,6 +284,7 @@ class LocationUtils {
|
|
|
|
|
var result = await [
|
|
|
|
|
Permission.location,
|
|
|
|
|
].request();
|
|
|
|
|
return (result[Permission.location]!.isGranted || result[Permission.locationAlways]!.isGranted);
|
|
|
|
|
return (result[Permission.location]!.isGranted ||
|
|
|
|
|
result[Permission.locationAlways]!.isGranted);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|