[英]FLutter high accuracy with location package
For getting the user's location I am using two packages: location
and geocoding
.为了获取用户的位置,我使用了两个包:
location
和geocoding
。 I use location to check if the user has provided location permission or not and to get the LocationData
which carries the latitude and longitude.我使用位置来检查用户是否提供了位置权限,并获取带有经纬度的
LocationData
数据。
But as far I have seen location does not provide the property called desiredAccuracy
, which is much needed for my work.但据我所知,location 没有提供名为
desiredAccuracy
的属性,这对我的工作来说非常需要。
static void getCurrentLocation() async {
Location location = new Location();
bool _serviceEnabled;
PermissionStatus _permissionGranted;
LocationData _locationData;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return;
}
}
_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
_locationData = await location.getLocation();
double latitude = _locationData.latitude!;
double longitude = _locationData.longitude!;
List<geocoding.Placemark> placeMark =
await geocoding.placemarkFromCoordinates(latitude, longitude);
String country = placeMark.first.country!;
String isoCountryCode = placeMark.first.isoCountryCode!;
print("Country: $country and ISO Country Code: $isoCountryCode");
print(_locationData);
}
My code till now.我的代码到现在。 Is it possible to get something like desiredAccuracy in
Location
?是否有可能在
Location
中获得类似 desiredAccuracy 的信息? The only other solution for me would be to use geolocator
and permission_handler
packages which will require further configurations.对我来说,唯一的其他解决方案是使用需要进一步配置的
geolocator
和permission_handler
包。
I don't see why you don't want to use geolocator... you've already done the hard work.我不明白你为什么不想使用地理定位器......你已经完成了艰苦的工作。 Here's how you get the location with desired accuracy
以下是您如何以所需的精度获取位置
location = await Geolocator.getCurrentPosition(
forceAndroidLocationManager: true,
desiredAccuracy: LocationAccuracy.best
).timeout(Duration(seconds: 20));
Here's how to check if service is enabled这是检查服务是否已启用的方法
var serviceEnabled = await Geolocator.isLocationServiceEnabled();
And here's how to check if permission is granted这是检查是否授予权限的方法
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// Permissions are denied, next time you could try
// requesting permissions again (this is also where
// Android's shouldShowRequestPermissionRationale
// returned true. According to Android guidelines
// your App should show an explanatory UI now.
Its clearly written in the documentation here geolocatordoc它在此处的文档中清楚地写在geolocatordoc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.