【发布时间】: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>
【问题讨论】: