【发布时间】:2021-10-05 15:31:12
【问题描述】:
我一直在一个使用 Flutter 开发应用程序的团队中工作。我们需要使用 GPS 跟踪行进的距离,但挑战在于 Flutter 不再允许前景和背景位置跟踪(即使是粗略的准确度)。
根据对最新 Google 指南的审核,Google 似乎应该允许我们的应用在设备处于后台时对其进行跟踪,但事实并非如此。事实上,打开前台和后台定位服务似乎在一两个月前就开始起作用了。我们尝试了location 和geolocation 软件包,但均无济于事。当应用程序在后台时,它们中的任何一个都会立即停止跟踪,而当应用程序返回到前台时,我们的位置会发生很大的跳跃。
这是我们尝试过的方法
AndroidManifest.xml
...
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
...
使用地理位置的应用代码
...
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('Location permissions are denied');
}
}
//Permission returns denied Location
//this is due to the background permission in the android manifest
//turn off background permission and the permission is allowed
if (permission == LocationPermission.deniedForever)
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
...
使用位置的应用代码
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
print('Service could not be enabled');
return false;
}
// This code throws and exception even if I respond to the
// Google prompts and I select allow location service and all the time access
try {
await location.enableBackgroundMode(enable: true);
} catch(error) {
print("Can't set background mode");
}
任何关于如何实现这一目标的指导将不胜感激。
【问题讨论】:
标签: android ios flutter geolocation location