我已经解决这个问题一段时间了:我的代码在android模拟器上运行并返回正确的距离,但是当我在iphoneXr(iOS 13.3)或模拟器上运行该应用程序时,将来(< 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;
});
}
updateUser()类中的用户:
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>
答案 0 :(得分:3)
使用xcode Runner运行应用程序时,实际上可以看到错误所在。
我按照使用Geolocator的所有说明进行操作,但是一开始,我开始丢失关键错误,这已由Yudhisthir Singh提到过。然后我收到“ locationManager:didFailWithError:”,这种故障在颤动中是无声的。
原来,我需要通过IOS Simulator添加位置。从菜单中,该菜单以前在Debug中,但是我在Features-> Location-> Custom Location中找到它(输入所需的地理位置,单击Ok)。
答案 1 :(得分:0)
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
在info.plist中添加以下内容以及其他两个权限。