【问题标题】:Turn on location services without navigating to settings page ? Flutter Dart打开定位服务而不导航到设置页面?飞镖
【发布时间】:2019-06-21 05:01:45
【问题描述】:

我们正在从 Flutter 迁移。我们用这个线程来Turn on location services without navigating to settings page

这在 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();

  }

【问题讨论】:

标签: dart geolocation flutter location


【解决方案1】:

在屏幕主体中添加地图

@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)));
  }

此代码将在主屏幕中初始化谷歌地图,如果位置被关闭,则会弹出一个警告框询问权限,然后它将自动打开

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 启用位置权限和位置服务是两件事
    猜你喜欢
    • 2016-01-19
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2016-11-21
    相关资源
    最近更新 更多