无法在Flutter应用中获取用户位置

时间:2019-06-04 15:09:37

标签: android flutter

我正在尝试使用location软件包来获取用户位置,但是我无法在没有任何错误消息的情况下获取位置。代码从不通过await location.getLocation();行。

我曾尝试将flutter清理并将位置包的等级从2.3.5降级到1.4.0,但这并没有帮助我,因为该版本在过去曾起作用,但在将flutter android studio升级并升级到最新版本后,我没有做到这一点。像以前一样工作。我也尝试使用geolocator程序包,但遇到了完全相同的问题。

这是我的代码。

Future<LocationData> _getLocation() async {
Location location = new Location();
var currentLocation;
          try {
            currentLocation = await location.getLocation();
          } catch (e) {
            currentLocation = null;
          }
return currentLocation;
}

我正在使用vscode的控制台中没有错误 这是扑医生的输出。

[√] Flutter (Channel beta, v1.5.4-hotfix.1, on Microsoft Windows [Version 10.0.17134.765], locale en-US)
    • Flutter version 1.5.4-hotfix.1 at C:\Users\kawak\sdk\flutter
    • Framework revision 09cbc34a0b (5 weeks ago), 2019-04-30 15:44:27 -0700
    • Engine revision 52c7a1e849
    • Dart version 2.3.0 (build 2.3.0-dev.0.5 a1668566e5)


[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at D:\androidsdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = D:\androidsdk
    • Java binary at: D:\android\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[!] Android Studio (version 3.4)
    • Android Studio at D:\android
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[√] VS Code, 64-bit edition (version 1.34.0)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.1.0

[!] Connected device
    ! No devices available

! Doctor found issues in 2 categories.

5 个答案:

答案 0 :(得分:1)

这是我用来获取位置的方法。希望对您有所帮助。

void _getLocation() async {
    var location = new Location();
    try {
      await location.getLocation().then((onValue) {
              print(onValue.latitude.toString() + "," + onValue.longitude.toString());
      });
    } catch (e) {
      print(e);
      if (e.code == 'PERMISSION_DENIED') {
       _handlePermissionDenied();
      }
    }
  }

您也可以尝试地理定位,但是我发现location插件比geolocation插件快。

答案 1 :(得分:0)

您使用的是捕获而不显示错误。.删除它,您可能会收到一条漂亮的消息,告诉您错误是什么!

答案 2 :(得分:0)

尝试使用flutter库:location

var currentLocation = LocationData;

var location = new Location();

try {
  currentLocation = await location.getLocation();
} on PlatformException catch (e) {
  if (e.code == 'PERMISSION_DENIED') {
    error = 'Permission denied';
  } 
  currentLocation = null;
}

答案 3 :(得分:0)

Alhamdulillah发现我正在使用beta通道的解决方案需要移至稳定的通道

答案 4 :(得分:0)

使用位置 plugin 很重要。

在插件的文档中有添加一些权限的建议:

  • iOS:必须在 Info.plist (ios/Runner) 中添加 NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription。如果您要不断聆听位置信息,后者很重要。

  • Android:没有必要添加任何权限,因为插件已经在内部做了。 (如果您使用的是 Flutter 1.12+ 版本)

现在,为了请求位置,我们应该总是手动检查位置服务状态和权限状态:

Future<void> manageLocationPermission() async {
  LocationData locationData;
  final Location location = Location();
  bool serviceEnabled;
  PermissionStatus permissionGranted;

  serviceEnabled = await location.serviceEnabled();
  if (!serviceEnabled) {
    serviceEnabled = await location.requestService();
    if (!serviceEnabled) {
      return;
    }
  }

  permissionGranted = await location.hasPermission();
  if (permissionGranted == PermissionStatus.denied) {
    permissionGranted = await location.requestPermission();
    //TODO: do your logic to manage when user denies the permission
    if (permissionGranted != PermissionStatus.granted) {
      _buildMapLocatorDialog();
      return;
    }
  }
  locationData = await location.getLocation();
  //TODO: do your logic to manage when user grants the permission
  viewModel.onMapLocatorClick(locationData);
}

这个函数是在用户点击GPS图标居中/获取当前位置后调用的,但是根据你的需要,你需要在任何需要的地方调用它