如何请求用户精确位置?

问题描述 投票:0回答:1

使用 Geolocator 包,当请求位置权限时,用户可以选择大致位置。或者用户也可以从设置中关闭精确定位。我们可以从 -> (LocationAccuracyStatus.reduced || LocationAccuracyStatus.precise) 中获取用户选择的位置精度,但如何再次请求用户获取精确位置权限?

因此,最初如果授予了近似位置权限,则在检查 LocationAccuracyStatus 值减少后,我再次调用 Geolocator.requestPermission() 但它没有再次显示系统 ui 弹出窗口。

 LocationPermission permission;
  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
          MainUtils.showTopSnackbar(
           "Location Permissions are required to use the app",
           duration: const Duration(seconds: 5));
           return;
       }
 }

//CHECKING LOCATION ACCURACY
var accuracy = await Geolocator.getLocationAccuracy();
if (accuracy == LocationAccuracyStatus.reduced) {
   permission = await Geolocator.requestPermission(); //REQUESTING PERMISSION AGAIN
}
https://developer.android.com/develop/sensors-and-location/location/permissions#upgrade-to-precise

参考图

flutter flutter-dependencies android-permissions geolocator
1个回答
0
投票

我认为您无法以编程方式请求从近似位置升级到精确位置。一旦用户选择了大致位置,他们必须在系统设置中手动更改此位置。但是,您可以将用户重定向到应用程序的系统权限设置

  if (accuracy == LocationAccuracyStatus.reduced) {
  final bool shouldOpenSettings = await showDialog(
    context: context,
    builder: (context) => AlertDialog(
      title: Text('Precise Location Needed'),
      content: Text(
        'This app requires precise location to function properly. '
        'Would you like to change this in settings?'
      ),

可以使用方法通道调用原生代码

private fun openAppSettings() {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
val uri = Uri.fromParts("package", packageName, null)
intent.data = uri
startActivity(intent)}

方法通道

 import 'package:flutter/services.dart';

class NativeSettingsHandler {
  static const platform = MethodChannel('com.yourapp/settings');

  Future<void> openAppSettingsNative() async {
    try {
      await platform.invokeMethod('openAppSettings');
    } on PlatformException catch (e) {
      print("Failed to open app settings: '${e.message}'.");
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.