Turn on location services without navigating to settings page ? Flutter Dart

余生颓废 提交于 2019-12-10 20:13:37

问题


We are migrating from to Flutter. We used this thread to Turn on location services without navigating to settings page

How can this be achieved in Flutter?

The current temporary code that navigates to settings:

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

  }

回答1:


You can use the location package, you just have to add the required permissions in your AndroidManifest.xml and Info.plist files, permission will be asked in a pop-up.



来源:https://stackoverflow.com/questions/54397823/turn-on-location-services-without-navigating-to-settings-page-flutter-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!