如何在不进入设置页面的情况下打开位置服务?Flutter Dart

5
我们正在从 迁移到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();

  }

请查看simple_permissions包。 - soupjake
2个回答

0
在屏幕正文中添加地图。
@override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Builder(
          builder: (context) => Stack(
            children: [
              GoogleMap(
                scrollGesturesEnabled: true,
                tiltGesturesEnabled: true,
                rotateGesturesEnabled: true,
                mapToolbarEnabled: true,
                mapType: MapType.normal,
                onTap: _placeMarker,
                onMapCreated: _onMapCreated,
                markers: Set.from(myMarker),
                initialCameraPosition: CameraPosition(
                  target: _center,
                  zoom: 5.0,
                ),
              ),
              Padding(
                padding: (EdgeInsets.symmetric(horizontal: 16, vertical: 40)),
                child: Align(
                  alignment: Alignment.topRight,
                  child: Column(
                    children: [
                      CircleAvatar(
                        radius: 30,
                        child: IconButton(
                          onPressed: () {
                            navigate(context);
                          },
                          icon: Icon(
                            Icons.done,
                            color: Colors.white,
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                      CircleAvatar(
                        radius: 30,
                        child: IconButton(
                          onPressed: **getLocation**,
                          icon: Icon(
                            Icons.location_on,
                            color: Colors.white,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ),
              Padding(
                padding: (EdgeInsets.symmetric(horizontal: 16, vertical: 40)),
                child: Align(
                  alignment: Alignment.topLeft,
                  child: Column(
                    children: [
                      CircleAvatar(
                        radius: 30,
                        child: IconButton(
                          onPressed: () {
                            Navigator.pop(context);
                          },
                          icon: Icon(
                            Icons.chevron_left,
                            color: Colors.white,
                          ),
                        ),
                      ),
                      SizedBox(
                        height: 10,
                      ),
                    ],
                  ),
                ),
              )
            ],
          ),
        ));
  }


  getLocation() async {
    position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    print(position);
    mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
        target: LatLng(position.latitude, position.longitude),
        zoom: 18,
        tilt: 36)));
  }

这段代码将在主屏幕上初始化谷歌地图,如果位置已关闭,则会弹出一个警报框请求权限,然后它将自动打开。


-5

您可以使用location包,只需要在AndroidManifest.xml和Info.plist文件中添加所需的权限,系统会弹出窗口来请求权限。


启用位置权限和位置服务是两个不同的事情。 - MJ Montes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接