【问题标题】:how to set distance between users using Geolocator in flutter如何在颤振中使用地理定位器设置用户之间的距离
【发布时间】:2020-04-15 13:35:47
【问题描述】:

当用户在我确定的位置半径 x 米的范围内时,我目前正在尝试授予用户执行某些操作的权限。所以,我的意思是..我声明了一个 x 纬度和 y 经度的位置.. 然后如果用户的位置距离我声明的位置 500 米,他们可以访问其他东西......有没有办法做到这一点?这是我的代码的一部分

Position _mine;
  Future _searchMe() async {
    if ((await Geolocator().isLocationServiceEnabled())) {
      final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
      geolocator
          .getCurrentPosition(desiredAccuracy: LocationAccuracy.high)
          .then((Position position) {
        setState(() {
          _mine = position;
        });
        print(_mine.latitude);
      }).catchError((err) {
        print(err);
      });
    } else {
      print("ok");
      showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              content:
                  const Text('Make sure your GPS is on !'),
              actions: <Widget>[
                FlatButton(
                    child: Text('ok'),
                    onPressed: () {
                      Navigator.of(context, rootNavigator: true).pop();
                    })
              ],
            );
          });
    }
  }

【问题讨论】:

  • 你能澄清你的问题吗
  • Geolocator 包提供了一个 distanceBetween 方法。您可以将您确定位置的经度和设备当前位置的经度传递给它,它将返回两点之间的距离(以米为单位)。见:pub.dev/packages/geolocator#calculate-distance
  • 您觉得 Distance().as() 有帮助吗?
  • 嗨对不起,我忘记投票了

标签: flutter location maps position


【解决方案1】:

您可能可以执行以下操作:

double _countDistance(double userLatitude, double userLongitude) {
    return Distance().as(
      LengthUnit.Kilometer,
      LatLng(declaredLocation.latitude, declaredLocation.longitude),
      LatLng(userLatitude, userLongitude),
    );
  }

解释:

Distance().as 方法采用三个参数: A. 单位公里、米等 B. 第一经纬度 C. 秒经纬度

上面的函数会计算以公里为单位的距离。您可以在您的代码中添加此代码,并在用户在 500 米内时执行某些操作。

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 2021-09-15
    • 1970-01-01
    • 2011-12-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    相关资源
    最近更新 更多