【问题标题】:flutter geolocator returns null when used on iOS在 iOS 上使用时,颤振地理定位器返回 null
【发布时间】:2020-04-19 04:07:44
【问题描述】:

我一直在研究这个问题一段时间:我的代码在 android 模拟器上运行并返回正确的距离,但是当我在 iphoneXr (iOS 13.3) 或模拟器上运行应用程序时,未来 (_getDistance) 只返回零。 我使用 geolocator 5.1.5 包。

根据要求,我调整了 AndroidManifest.xml 和 Info.plist(可以在这个问题的底部找到)。

我在 android 上被问到是否要允许应用访问该位置,但在 iOS 上从来没有,我在哪里可以设置?

Future<double> _getDistance() async {
    await user.updateLocation();
    var latitude = user.getLocation.latitude;
    var longitude = user.getLocation.longitude;
    return Geolocator()
        .distanceBetween(
            location.latitude, location.longitude, latitude, longitude)
        .then((dis) {
      return dis;
    });
  }

User 类中的 updateLocation():

Future<void> updateLocation() async {
    return Geolocator()
        .getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
        .then((position) {
      _location.latitude = position.latitude;
      _location.longitude = position.longitude;
    });
  }

这个 Builder 使用 _getDistance():

FutureBuilder(
            future: _getDistance(),
            builder: (ctx, snapshot) {
              if (snapshot.connectionState == ConnectionState.done) {
                var txt = _distanceConverter(snapshot.data);
                return Text(
                  txt,
                  textAlign: TextAlign.end,
                  style: TextStyle(color: Colors.white),
                );
              } else {
                return SizedBox(
                  height: 15,
                  width: 15,
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation(Colors.white),
                  ),
                );
              }
            },
          ),

我的 Info.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>join</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>
    <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>io.flutter.embedded_views_preview</key>
    <true/>
    <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/>
</dict>
</plist>

我的 AndroidManifest.xml 文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.join">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
        android:name="io.flutter.app.FlutterApplication"
        android:label="join"
        android:icon="@mipmap/ic_launcher">
        <meta-data  android:name="com.google.android.geo.API_KEY"
                    android:value="######<myworkingAPIkey>######"/>
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

【问题讨论】:

    标签: android ios flutter


    【解决方案1】:

    当您使用 xcode Runner 运行您的应用程序时,您实际上会看到错误是什么。

    我按照使用 Geolocator 的所有说明进行操作,但在开始时,我开始出现丢失关键错误,Yudhishir Singh 提到的问题已解决。然后我得到“locationManager:didFailWithError:”这个失败是无声的。

    原来我需要通过 IOS 模拟器添加位置。从以前在调试中的菜单中,但我在功能-> 位置-> 自定义位置中找到了它(输入所需的地理位置单击确定)。

    希望这对遇到同样问题的人有所帮助。

    【讨论】:

      【解决方案2】:
      <key>NSLocationWhenInUseUsageDescription</key>
      <string>This app needs access to location when open.</string>
      

      在 info.plist 中添加以下内容以及其他两个权限。

      【讨论】:

        猜你喜欢
        • 2021-09-15
        • 2023-03-13
        • 1970-01-01
        • 2022-06-18
        • 2023-03-25
        • 2021-12-09
        • 2020-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多