代码之家  ›  专栏  ›  技术社区  ›  TSR

打开位置服务而不导航到“设置”页?飞镖

  •  0
  • TSR  · 技术社区  · 7 年前

    Turn on location services without navigating to settings page

    如何在颤振中实现这一点?

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

      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();
    
      }
    
    0 回复  |  直到 7 年前
        1
  •  -5
  •   C. Grd    7 年前

    你可以用 location

        2
  •  0
  •   Niroop Nife    6 年前

    在屏幕主体中添加地图

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

    此代码将初始化主屏幕中的谷歌地图,如果该位置被打开,则会弹出一个警报框,请求许可,然后它将自动打开