使用权限获取当前位置不起作用
Fetching Current Location with Permission not working
我正在使用
地理定位器:'^3.0.1'
permission_handler: '^3.0.0'
现在我想获取用户的当前位置,并在用户打开地图时将其显示在地图上。
所以我的代码是:
Future<void> requestPermission() async {
PermissionHandler()
.checkPermissionStatus(PermissionGroup.location)
.then((PermissionStatus permissionStatus) async {
print("Checking Permission " + permissionStatus.toString());
if (permissionStatus == PermissionStatus.granted) {
_getCurrentLocation();
} else {
print("Asking Permission " + permissionStatus.toString());
final List<PermissionGroup> permissions = <PermissionGroup>[
PermissionGroup.locationWhenInUse
];
final Map<PermissionGroup, PermissionStatus> permissionRequestResult =
await PermissionHandler().requestPermissions(permissions);
if (PermissionStatus.granted ==
permissionRequestResult[PermissionGroup.locationWhenInUse]) {
print("Permission Granted " + permissionStatus.toString());
_getCurrentLocation();
}
}
});
}
android 和 IOS 的 info.list 的清单中定义了权限。
现在的问题是当我 运行 这个函数和它调用 requestPermission
函数时,它显示弹出窗口请求权限和
当我 allow the permission
应用程序因错误而崩溃时 :
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions: ... java.lang.IllegalStateException: Reply already submitted
并且权限的结果也是 Permission.disabled
尽管我在应用程序设置中允许了权限并且在权限中它显示位置是允许的。但我尝试多次打开应用程序,它显示 Permission.disabled.
即使我 deny
应用程序崩溃并出现相同的错误。
所以我得出的结论是:
如果我 allow
或 deny
它会崩溃,因为它请求了多次,即使我允许结果也是 Permission.disabled.
Link 视频:https://youtu.be/A1DKkw6u4HI
谁能帮我解决这个问题?
Or please tell me how to take the current location map easily
please tell me how to take the current location map easily
如果您只需要轻松获取当前位置并且只需要位置权限,那么:
你可以在 flutter 中使用 location
插件 :
在你的 pubspec.yml
中:location : ^2.3.0
然后获取当前位置:
导入位置包
import 'package:location/location.dart' as locationPackage;
将此添加到您的州
locationPackage.Location _locationService = new locationPackage.Location();
bool _permission = false;
在 initState 或任何需要当前位置的时候调用此函数
fetchCurrentLocation() async {
await _locationService.changeSettings(
accuracy: locationPackage.LocationAccuracy.HIGH, interval: 1000);
locationPackage.LocationData location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
bool serviceStatus = await _locationService.serviceEnabled();
print("Service status: $serviceStatus");
if (serviceStatus) {
_permission = await _locationService.requestPermission();
print("Permission: $_permission");
if (_permission) {
location = await _locationService.getLocation();
print("Location: ${location.latitude}");
}
} else {
bool serviceStatusResult = await _locationService.requestService();
print("Service status activated after request: $serviceStatusResult");
if (serviceStatusResult) {
fetchCurrentLocation();
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
//error = e.message;
} else if (e.code == 'SERVICE_STATUS_ERROR') {
//error = e.message;
}
location = null;
}
}
我正在使用 地理定位器:'^3.0.1' permission_handler: '^3.0.0'
现在我想获取用户的当前位置,并在用户打开地图时将其显示在地图上。
所以我的代码是:
Future<void> requestPermission() async {
PermissionHandler()
.checkPermissionStatus(PermissionGroup.location)
.then((PermissionStatus permissionStatus) async {
print("Checking Permission " + permissionStatus.toString());
if (permissionStatus == PermissionStatus.granted) {
_getCurrentLocation();
} else {
print("Asking Permission " + permissionStatus.toString());
final List<PermissionGroup> permissions = <PermissionGroup>[
PermissionGroup.locationWhenInUse
];
final Map<PermissionGroup, PermissionStatus> permissionRequestResult =
await PermissionHandler().requestPermissions(permissions);
if (PermissionStatus.granted ==
permissionRequestResult[PermissionGroup.locationWhenInUse]) {
print("Permission Granted " + permissionStatus.toString());
_getCurrentLocation();
}
}
});
}
android 和 IOS 的 info.list 的清单中定义了权限。
现在的问题是当我 运行 这个函数和它调用 requestPermission
函数时,它显示弹出窗口请求权限和
当我 allow the permission
应用程序因错误而崩溃时 :
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions: ... java.lang.IllegalStateException: Reply already submitted
并且权限的结果也是 Permission.disabled
尽管我在应用程序设置中允许了权限并且在权限中它显示位置是允许的。但我尝试多次打开应用程序,它显示 Permission.disabled.
即使我 deny
应用程序崩溃并出现相同的错误。
所以我得出的结论是:
如果我 allow
或 deny
它会崩溃,因为它请求了多次,即使我允许结果也是 Permission.disabled.
Link 视频:https://youtu.be/A1DKkw6u4HI
谁能帮我解决这个问题?
Or please tell me how to take the current location map easily
please tell me how to take the current location map easily
如果您只需要轻松获取当前位置并且只需要位置权限,那么:
你可以在 flutter 中使用 location
插件 :
在你的 pubspec.yml
中:location : ^2.3.0
然后获取当前位置:
导入位置包
import 'package:location/location.dart' as locationPackage;
将此添加到您的州
locationPackage.Location _locationService = new locationPackage.Location();
bool _permission = false;
在 initState 或任何需要当前位置的时候调用此函数
fetchCurrentLocation() async {
await _locationService.changeSettings(
accuracy: locationPackage.LocationAccuracy.HIGH, interval: 1000);
locationPackage.LocationData location;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
bool serviceStatus = await _locationService.serviceEnabled();
print("Service status: $serviceStatus");
if (serviceStatus) {
_permission = await _locationService.requestPermission();
print("Permission: $_permission");
if (_permission) {
location = await _locationService.getLocation();
print("Location: ${location.latitude}");
}
} else {
bool serviceStatusResult = await _locationService.requestService();
print("Service status activated after request: $serviceStatusResult");
if (serviceStatusResult) {
fetchCurrentLocation();
}
}
} on PlatformException catch (e) {
print(e);
if (e.code == 'PERMISSION_DENIED') {
//error = e.message;
} else if (e.code == 'SERVICE_STATUS_ERROR') {
//error = e.message;
}
location = null;
}
}