FLutter 高精度定位包

问题描述 投票:0回答:2

为了获取用户的位置,我使用两个包:

location
geocoding
。我使用位置来检查用户是否提供了位置权限,并获取带有纬度和经度的
LocationData

但据我所知,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);
  }

到目前为止我的代码。是否有可能在

Location
中得到类似desiredAccuracy 的东西?对我来说唯一的其他解决方案是使用
geolocator
permission_handler
包,这将需要进一步的配置。

flutter geolocation location
2个回答
4
投票

我不明白你为什么不想使用地理定位器......你已经完成了艰苦的工作。 以下是如何获得所需精度的位置

 location = await Geolocator.getCurrentPosition(
  forceAndroidLocationManager: true,
  desiredAccuracy: LocationAccuracy.best
).timeout(Duration(seconds: 20));

以下是检查服务是否启用的方法

var serviceEnabled = await Geolocator.isLocationServiceEnabled();

以下是如何检查是否已授予权限

    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.

这里的文档写得很清楚 地理定位器文档


0
投票

https://devflutterdroid.blogspot.com/2023/12/how-to-get-users-current-location-also.html 在这里查看这篇文章获取用户的当前位置并在 Flutter 中强制获取用户的位置 - 地理定位器和地理编码

© www.soinside.com 2019 - 2024. All rights reserved.