我正在使用包 Background Location 来跟踪我的 Flutter 应用程序中的背景位置。这是我在 info.plist
中编辑的方式 <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location to assign orders to you based on your location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location to assign orders to you based on your location.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location to assign orders to you based on your location.</string>
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>location</string>
</array>
这里是如何在 dart 文件中获取位置:
Future startLoationTracking(bool start) async {
getDeviceLocationPermission();
if (start) {
var permission = await Permission.locationAlways.isGranted;
if (!permission) {
var t = await Permission.locationAlways.request();
}
print("location started");
if (defaultTargetPlatform != TargetPlatform.iOS){
BackgroundLocation.setAndroidNotification(
title: "DR Driver",
message: "Dr driver getting your location",
icon: "@mipmap/launcher_icon",
);
await BackgroundLocation.setAndroidConfiguration(1000);
}
await BackgroundLocation.startLocationService();
await BackgroundLocation.getLocationUpdates((location) {
print("${location.latitude}");
ApiServices().sendLocation(location.latitude, location.longitude);
});
} else {
BackgroundLocation.stopLocationService();
}
}
该应用从不要求位置许可。