打开定位服务而不浏览设置页面?扑镖

时间:2019-01-28 08:12:37

标签: 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();

  }

2 个答案:

答案 0 :(得分: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)));
  }

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

答案 1 :(得分:-3)

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