ios Flutter 中获取位置的延迟
' return await Geolocator.getCurrentPosition();'
我尝试了上面的代码,但 android 可以工作,但在 ios iPad 上不行,它显示无限期加载
'''
Future<void> getLocation() async {
setState(() {
_isLoading = true;
});
// Optimized location settings with reduced accuracy for faster fetching
LocationSettings locationSettings;
if (defaultTargetPlatform == TargetPlatform.android) {
locationSettings = AndroidSettings(
accuracy: LocationAccuracy.reduced, // Using reduced accuracy for faster results
forceLocationManager: true,
);
} else if (defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS) {
locationSettings = AppleSettings(
accuracy: LocationAccuracy.reduced, // Using reduced accuracy for faster results
activityType: ActivityType.other,
// Reduced timeout since we expect faster response
);
} else {
locationSettings = LocationSettings(
accuracy: LocationAccuracy.reduced, // Using reduced accuracy for faster results
);
}
try {
// Check if location services 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 Services Disabled',
titleTextStyle: TextStyle(
color: Color(0XFF0068B3),
fontWeight: FontWeight.bold,
fontSize: 16.sp,
),
desc: 'Please enable location services to continue.',
descTextStyle: TextStyle(
color: Color(0XFF585F65),
fontWeight: FontWeight.w500,
fontSize: 12.sp,
),
btnOkText: 'Open Settings',
buttonsTextStyle: TextStyle(
fontSize: 14.sp,
color: Colors.white,
),
btnOkColor: Colors.blue,
btnOkOnPress: () async {
await Geolocator.openLocationSettings();
},
).show();
return;
}
}
// Check 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(
color: Color(0XFF0068B3),
fontWeight: FontWeight.bold,
fontSize: 16.sp,
),
desc: 'Please grant location permission to use this feature.',
descTextStyle: TextStyle(
color: Color(0XFF585F65),
fontWeight: FontWeight.w500,
fontSize: 12.sp,
),
btnOkText: 'Request Permission',
buttonsTextStyle: TextStyle(
fontSize: 14.sp,
color: Colors.white,
),
btnOkColor: Colors.blue,
btnOkOnPress: () async {
await getLocation();
},
).show();
return;
}
}
if (permission == LocationPermission.deniedForever) {
setState(() {
_isLoading = false;
});
AwesomeDialog(
context: context,
dialogType: DialogType.error,
animType: AnimType.scale,
title: 'Location Permission Denied',
titleTextStyle: TextStyle(
color: Color(0XFF0068B3),
fontWeight: FontWeight.bold,
fontSize: 16.sp,
),
desc: 'Location permission is permanently denied. Please enable it from app settings.',
descTextStyle: TextStyle(
color: Color(0XFF585F65),
fontWeight: FontWeight.w500,
fontSize: 12.sp,
),
btnOkText: 'Open Settings',
buttonsTextStyle: TextStyle(
fontSize: 14.sp,
color: Colors.white,
),
btnOkColor: Colors.blue,
btnOkOnPress: () async {
await Geolocator.openAppSettings();
},
).show();
return;
}
// Try to get the last known location first
Position? lastKnownPosition = await Geolocator.getLastKnownPosition(
forceAndroidLocationManager: true,
);
if (lastKnownPosition != null ) {
// Use last known position if it's recent
latitude = lastKnownPosition.latitude;
longitude = lastKnownPosition.longitude;
print("lat and lon from lastknwnlocation ${latitude}${longitude}");
} else {
// Get current position with reduced accuracy settings
Position position = await Geolocator.getCurrentPosition(
locationSettings: locationSettings,
);
latitude = position.latitude;
longitude = position.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 Found',
desc: 'Unable to fetch your current location. Please try again.',
btnOkText: 'Retry',
btnOkColor: Colors.blue,
btnOkOnPress: () async {
await getLocation();
},
).show();
}
} catch (e) {
print('Error fetching location: $e');
setState(() {
_isLoading = false;
});
// More specific error handling
String errorMessage = 'Failed to fetch location. Please try again.';
if (e is TimeoutException) {
errorMessage = 'Location fetch is taking too long. Please check your GPS signal and try again.';
}
AwesomeDialog(
context: context,
dialogType: DialogType.error,
animType: AnimType.scale,
title: 'Error',
titleTextStyle: TextStyle(
color: Color(0XFF0068B3),
fontWeight: FontWeight.bold,
fontSize: 16.sp,
),
desc: errorMessage,
descTextStyle: TextStyle(
color: Color(0XFF585F65),
fontWeight: FontWeight.w500,
fontSize: 12.sp,
),
btnOkText: 'Retry',
buttonsTextStyle: TextStyle(
fontSize: 14.sp,
color: Colors.white,
),
btnOkColor: Colors.blue,
btnOkOnPress: () async {
await getLocation();
},
).show();
} finally {
setState(() {
_isLoading = false;
});
}
}
'''