dart - 在不导航到设置页面的情况下打开位置服务? Dart

标签 dart geolocation flutter location

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

  }

最佳答案

在屏幕正文中添加 map

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

此代码将初始化主屏幕中的 Google map ,如果位置被关闭,则会弹出一个警告框,要求获得许可,然后它会自动打开

关于dart - 在不导航到设置页面的情况下打开位置服务? Dart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54397823/

相关文章:

Dart 最终函数

php - 将返回的 Twitter JSON 地理位置存储为变量

android - 如何修复没有这样的 NoSuchMethodError flutter tabView?

dart - Flutter - 如何创建显示 RenderBox 组件的小部件?

android - "flutter pub get"无法获取依赖插件

javascript - 使用computeDistanceBetween计算用户当前位置和固定点之间的距离

flutter - 看大图 onTapping in flutter

flutter - websocket 自动重新连接 flutter 和 Riverpod?

dart - Dart 和 Vuforia 的 VWS API 中的 Base64 HMAC-SHA1 和 MD5 加密

jquery - 地理定位不适用于 Windows 7/XP 上的 Safari 5.x