【问题标题】:How to turn on device GPS in flutter?如何在颤动中打开设备 GPS?
【发布时间】:2019-01-28 05:31:19
【问题描述】:

通过使用 Dart 的 simple_permissions 和 location 包,它只要求用户允许或拒绝应用程序使用设备位置来在谷歌地图上显示用户的当前位置。 当我在我的设备上手动打开 GPS 时,我能够获取当前位置,但我如何通过对话框要求用户从应用程序本身启用 GPS,以启用 GPS,如谷歌地图?

【问题讨论】:

  • 我不认为你必须为此创建这样的对话框。但是,如果您想创建这样的功能,那么您必须创建自己的插件。并通过方法通道调用。
  • 那么有什么方法可以同时打开安卓和IOS的位置设置页面吗?
  • 也许这个链接可以帮助你pub.dartlang.org/packages/settings#-example-tab-
  • @satish 使用上述插件打开设置页面时,抛出 MissingPluginException(No implementation found for method wifi on channel plugins.ly.com/settings) 错误
  • 你可以使用this它对我有用

标签: dart flutter


【解决方案1】:

这个Plugin 显示了打开 Gps 对话以进行颤振(就像原生 android 一样)

【讨论】:

  • 你的意思是我的问题stackoverflow.com/q/58994925/5209982中显示的这种对话框吗?
  • 正是最近在插件中添加了原生对话功能,我使用它一个多月了
【解决方案2】:

如果您已经拥有 geolocator 或者如果您正在使用这两个(geolocator 和 location 包),则首先将其中一个作为别名导入,例如对于 location,将其导入为:

import 'package:location/location.dart' as loc;

loc.Location location = loc.Location();//explicit reference to the Location class
Future _checkGps() async {
if (!await location.serviceEnabled()) {
  location.requestService();
 }
}

请注意,使用 var location = Location() 或 var location = loc.Location() 将不起作用,对于上面的示例,您必须使用别名 loc 专门引用 Location 类。

然后在每次应用启动时调用如下initState中的方法进行检查,如下所示:

@Override
void initState()
{
 super.initState();//comes first for initState();
 _checkGPS();
}

【讨论】:

    【解决方案3】:

    您可以使用位置库

    var location = Location();
    
      Future checkGps() async {
        if (!await location.serviceEnabled()) {
          location.requestService();
        }
      }
    

    【讨论】:

      【解决方案4】:

      使用(android_intent: ^2.0.2),可以在android中请求GPS

      if (Platform.isAndroid) {
        Get.dialog(
          AlertDialog(
            title: Text("Can't get current location"),
            content: const Text('Please make sure you enable GPS and try again'),
            actions: <Widget>[
              TextButton(
                  child: Text('Ok'),
                  onPressed: () {
                    GeneralController.to.isPermissionAsked.value = true;
                    final AndroidIntent intent = AndroidIntent(action: 'android.settings.LOCATION_SOURCE_SETTINGS');
                    intent.launch();
                    Get.back();
                  })
            ],
          ),
          barrierDismissible: false,
        );
      }
      

      然后处理

      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
      switch (state) {
        case AppLifecycleState.resumed:
          print("app in resumed");      
          break;
        case AppLifecycleState.inactive:
          print("app in inactive");
          break;
        case AppLifecycleState.paused:
          print("app in paused");
          break;
        case AppLifecycleState.detached:
          print("app in detached");
          break;
      }
      

      }

      【讨论】:

        【解决方案5】:

        如果您使用 Geolocator 包,只需使用下面的代码,Geolocator 将打开一个定位服务供用户打开它然后重建您的小部件

        await Geolocator.openLocationSettings().then(value){
        
           //call function of whatever you need to do  
        }
        

        【讨论】:

          【解决方案6】:

          使用access_settings_menu可以打开安卓设备的位置设置页面

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-04
            • 2019-09-21
            相关资源
            最近更新 更多