【问题标题】:flutter - How to turn on or ask user to turn the location on?颤振 - 如何打开或要求用户打开位置?
【发布时间】:2019-02-03 05:04:20
【问题描述】:

您好,正如我在标题中所写,我该如何实现?我有定位权限但无法开启定位!

【问题讨论】:

    标签: android dart flutter


    【解决方案1】:

    它将打开设置。所以用户可以启用它。据我所知,这是最佳实践。

    安装插件:

    https://pub.dartlang.org/packages/android_intent#-installing-tab-

    在你的飞镖中导入:

    import 'package:android_intent/android_intent.dart';
    

    添加方法:

    void openLocationSetting() async {
        final AndroidIntent intent = new AndroidIntent(
          action: 'android.settings.LOCATION_SOURCE_SETTINGS',
        );
        await intent.launch();
      }
    

    调用它完成...

    【讨论】:

    • IOS呢?
    • 这对我不起作用。在 android 6.0 上什么都没有显示。
    • 如何开启或关闭状态?而不是调用屏幕?
    【解决方案2】:

    flutterlocation Plugin 向您显示打开 gps 的对话框,就像原生 android 一样

    【讨论】:

      【解决方案3】:

      我将使用我从 this 链接中学到的东西来指导你。

      将此依赖添加到pubspec.yaml

      dependencies:
        location: ^3.0.0
      

      现在,将以下包导入您的 .dart 文件

      import 'package:location/location.dart';
      

      现在在一个函数中,使用弹出框询问用户的位置,你可以这样做——

      Location location = new Location();
      bool _serviceEnabled;
      LocationData _locationData;
      

      以上是我们将在以下代码中使用的声明 -

      _serviceEnabled = await location.serviceEnabled();
      if (!_serviceEnabled) {
        _serviceEnabled = await location.requestService();
        if (!_serviceEnabled) {
          debugPrint('Location Denied once');
        }
      }
      

      现在,您可以根据需要使用上述 sn-p 使用弹出窗口进行位置请求调用,次数不限。

      如果用户的位置已被授予,您可以使用以下行来存储和使用位置数据。

      _locationData = await location.getLocation();
      

      如果未授予位置,则上述行将导致程序失败,因此请确保仅在用户允许从弹出窗口访问位置时才使用上述行。

      希望这会有所帮助。干杯!

      【讨论】:

        【解决方案4】:

        首先在pubspec.yaml中添加这个依赖:

        location: ^1.4.0
        

        然后,使用此函数检索您设备的当前位置:

        import 'package:location/location.dart';
         fetchCurrentLocation() async {
        
          print("STARTING LOCATION SERVICE");
          var location = Location();
          location.changeSettings(accuracy: LocationAccuracy.POWERSAVE,interval: 1000,distanceFilter: 500);
          if (!await location.hasPermission()) {
            await location.requestPermission();
          }
        
          try {
            await location.onLocationChanged().listen((LocationData currentLocation) {
              print(currentLocation.latitude);
              print(currentLocation.longitude);
              latitude = currentLocation.latitude;
              longitude = currentLocation.longitude;
            });
          } on PlatformException {
            location = null;
          }
        

        }

        【讨论】:

        • 实际上,我正在使用此依赖项来获取位置,但它没有获得权限,所以我通过简单权限手动完成。如果我关闭位置,此代码将进入 catch 块,而不要求任何打开位置或获取权限!
        • @Rachit Rawat :你给了经纬度,但质疑如何在 android 中启用 gps 或定位服务?
        • 问题是要求打开/关闭 GPS .....而不是如何获取位置.....
        【解决方案5】:

        您可以使用系统对话框并要求用户启用它。检查此link 以供参考。

        创建一个特定于平台的通道,并在用户想要启用它时调用它。

        【讨论】:

          【解决方案6】:

          我也这样做了,但有时不会出现位置许可的弹出通知。

          我将这些行添加到以下位置:
          project/android/app/src/main/AndroidManifest.xml

          【讨论】:

            猜你喜欢
            • 2020-08-03
            • 2017-08-25
            • 1970-01-01
            • 1970-01-01
            • 2022-01-03
            • 1970-01-01
            • 1970-01-01
            • 2019-05-15
            • 1970-01-01
            相关资源
            最近更新 更多