我想为我们的一个内部 flutter 应用程序实现实时位置跟踪。这是一个内部团队应用程序,我们应该能够以 30 秒的频率跟踪团队成员的位置并将其推送到我们的后端服务器。即使应用程序进入后台,这也应该可用。需要免费的解决方案。
提前谢谢您
我尝试了一些 flutter 插件,例如 location、background_location、geolocation,但没有成功
使用此包位置:^5.0.3并尝试使用此代码:
import 'package:location/location.dart';
Future<LocationData?> getCurrentLocation() async {
logI(message: 'CHKI: getCurrentLocation call');
Location location = Location();
bool serviceEnabled;
PermissionStatus permissionStatus;
try {
serviceEnabled = await location.serviceEnabled();
if (!serviceEnabled) {
serviceEnabled = await location.requestService();
if (!serviceEnabled) {
// Service not enabled, handle accordingly
return null;
}
}
permissionStatus = await location.hasPermission();
if (permissionStatus == PermissionStatus.denied) {
permissionStatus = await location.requestPermission();
if (permissionStatus != PermissionStatus.granted) {
// Permission not granted, handle accordingly
return null;
}
}
return await location.getLocation();
} catch (e) {
logE(message: 'Error getting current location: $e');
return null;
}
}