android - 无法通过地理定位器在 Flutter 中检索位置

标签 android ios flutter location

我正在开发一个 flutter 应用程序,我制作了一个类,该类使用 geolocator 包检索当前位置并通过 iOS 模拟器进行调试,但它不检索任何数据。定位服务的图标只是空心图标,没有接收到数据。

位置类:

import 'package:geolocator/geolocator.dart';

class LocationServices {
  double latitude;
  double longitude;

  Future<void> getCurrentLocation() async {
    try {
      Position position = await Geolocator()
          .getCurrentPosition(desiredAccuracy: LocationAccuracy.best);

      latitude = position.latitude;
      longitude = position.longitude;
      print(latitude);
      print(longitude);
    } catch (e) {
      print(e);
    }
  }
}

调用该函数的初始状态:

@override
  void initState() {
    // TODO: implement initState
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: Duration(seconds: 4),
    );
    _opacity = Tween<double>(begin: 0.0, end: 1).animate(_animationController);
    _animationController.forward();
    NetworkManager.checkConnectivity();
    // apiServices.getPrayerTimes();
    locationServices.getCurrentLocation();
  }

信息.plist文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>muslim_2b</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(FLUTTER_BUILD_NAME)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>$(FLUTTER_BUILD_NUMBER)</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>This app needs access to location when open.</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>This app needs access to location when in the background.</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>This app needs access to location when open and in the background.</string>
</dict>
</plist>

The Location icon is hollow

最佳答案

我也遇到了这个问题,我已经设置了模拟器的位置。我通过更改 Mac 上的位置首选项解决了这个问题。系统偏好设置 -> 安全和隐私 -> 启用定位服务(我为 Chrome 和 Visual Studio Code 都启用了)。

关于android - 无法通过地理定位器在 Flutter 中检索位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67870103/

相关文章:

ios - Firebase Phone Auth reCAPTCHA 验证在某些设备上不起作用

exception - 扩展面板底部溢出

flutter - 设置秒表的毫秒数

flutter - Flutter中的“自动关闭”对话框

java - 在 Android 中生成带有自定义文本的图像

java - PhoneGap : Inject JS into HTML on page load through Java on Android

android - 如何在kotlin android中使用viewmodel和数据绑定(bind)基于所有editetext启用/禁用按钮不为空

android - 如何通过音频管理器以编程方式将振动模式更改为铃声

ios - 我应该使用核心数据吗?

ios - EXC_BAD_ACCESS 在 Swift 中访问自定义 MKAnnotation 的属性时