如何在Flutter中使用Geolocator设置用户之间的距离 [英] how to set distance between users using Geolocator in flutter

查看:81
本文介绍了如何在Flutter中使用Geolocator设置用户之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently trying to give access to users to do something when users are in radius x meters from position that I have determined. So, what I mean is.. I declare a position with x latitude and y longitude.. and then if users position is 500 meters from position that I have declare, they can access something else... is there a way to do that? here is part of my code

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();
                    })
              ],
            );
          });
    }
  }

推荐答案

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

You can probably do something like below :

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

排布:

Distance().as方法采用三个参数:A.单位公里,米等B.第一经纬C.第二个拉特龙

The Distance().as method takes three parameters : A. unit e.g. kilometer, meter etc. B. first lat-long C. second lat-long

以上功能将以km为单位计算距离.您可以将此代码添加到自己的代码中,并在用户位于500米以内时执行某些操作.

The above function would calculate the distance in km. You can add this code in yours and do something when the user is within 500 meters.

这篇关于如何在Flutter中使用Geolocator设置用户之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆