在不导航到设置页面的情况下打开位置服务?飞镖

TSR*_*TSR 5 location geolocation dart flutter

我们正在迁移到 Flutter。我们使用此线程打开位置服务而无需导航到设置页面

如何在 Flutter 中实现这一点?

导航到设置的当前临时代码:

  Future _getCurrentLocation() async {
    Position position;
    try {
      position = await Geolocator().getCurrentPosition(
          desiredAccuracy: LocationAccuracy.bestForNavigation);
    } on PlatformException catch(e){
      print(e.code);
      if(e.code =='PERMISSION_DISABLED'){
        print('Opening settings');
        await openLocationSetting();
        try {
          position = await Geolocator().getCurrentPosition(
              desiredAccuracy: LocationAccuracy.bestForNavigation);
        } on PlatformException catch(e){
          print(e.code);
        }
      }
    }

    setState(() {
//      _center = LatLng(currentLocation['latitude'], currentLocation['longitude']);
      _center = LatLng(position.latitude, position.longitude);
    });
  }
  Future openLocationSetting() async {
    final AndroidIntent intent = new AndroidIntent(
      action: 'android.settings.LOCATION_SOURCE_SETTINGS',
    );
    await intent.launch();

  }
Run Code Online (Sandbox Code Playgroud)

小智 -5

您可以使用位置包,只需在 AndroidManifest.xml 和 Info.plist 文件中添加所需的权限,系统会在弹出窗口中询问权限。