locationsPermission static method
Future<String>
locationsPermission(
- RxBool isLocationEnable,
- RxDouble lat,
- RxDouble long,
- RxString address,
Implementation
/// For ios add these lines in infoplist files
/// <key>NSLocationWhenInUseUsageDescription</key>
/// <string>This app needs access to location when open.</string>
/// <key>NSLocationAlwaysUsageDescription</key>
/// <string>This app needs access to location when in the background.</string>
///
static Future<String> locationsPermission(RxBool isLocationEnable,RxDouble lat, RxDouble long,
RxString address,
) async {
LocationPermission? permission;
bool serviceEnabled;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
isLocationEnable.value=false;
CommonWidgets.toast(message: pleaseEnableLocationsStr);
//getController!.update();
return "";
}
isLocationEnable.value=true;
// update();
permission =await Geolocator.requestPermission();
if(permission==LocationPermission.always||permission==LocationPermission.whileInUse){
await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high).then((value) async {
List<Placemark> placemarks = await placemarkFromCoordinates(value.latitude,value.longitude);
Placemark place = placemarks[0];
lat.value=value.latitude;
long.value=value.longitude;
if(place.subLocality==null&&place.locality==null){
address.value='${place.name},${place.subAdministrativeArea}';
//getController!.update();
debugPrint("address is${place.toString()}");
}
else{
debugPrint("address is${place.name},${place.subAdministrativeArea}");
address.value = '${place.name} ${place.subLocality} ${place.locality},${place.subAdministrativeArea}';
// getController!.update();
}
});
return address.value;
}
if (permission == LocationPermission.denied) {
permission =await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return address.value;
}
else {
if (permission == LocationPermission.always ||
permission == LocationPermission.whileInUse) {
await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high).then((value) async {
List<Placemark> placemarks = await placemarkFromCoordinates(
value.latitude, value.longitude);
Placemark place = placemarks[0];
lat.value=value.latitude;
long.value=value.longitude;
if (place.subLocality == null && place.locality == null) {
address.value = '${place.name},${place.subAdministrativeArea}';
//update;
debugPrint("address is${place.toString()}");
}
else {
debugPrint(
"address is${place.name},${place.subAdministrativeArea}");
address.value =
'${place.name} ${place.subLocality} ${place.locality},${place
.subAdministrativeArea}';
// getController!.update();
}
});
return address.value;
}
}
}
if (permission == LocationPermission.deniedForever) {
permission =await Geolocator.requestPermission();
//getController!.update();
return address.value;
}
return address.value;
}