gpt4 book ai didi

android - flutter geolocator 在 iOS 上使用时返回 null

转载 作者:行者123 更新时间:2023-11-29 05:12:41 30 4
gpt4 key购买 nike

我已经研究这个问题有一段时间了:我的代码可以在 android 模拟器上运行并返回正确的距离,但是当我在 iphoneXr (iOS 13.3) 或模拟器上运行该应用程序时, future (< strong>_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;
});
}

此生成器使用 _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>

最佳答案

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

我遵循了使用 Geolocator 的所有说明,但一开始,我开始出现丢失 key 错误,该错误已通过 Yudhisthir Singh 提到的内容解决。然后我得到“locationManager:didFailWithError:”这个失败在 flutter 中是无声的。

事实证明,我需要通过 IOS 模拟器添加位置。从菜单中,该菜单以前位于“调试”中,但我在“功能”->“位置”->“自定义位置”中找到了它(输入所需的地理位置,然后单击“确定”)。

希望这可以帮助遇到同样问题的人。 enter image description here

关于android - flutter geolocator 在 iOS 上使用时返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59530364/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com