如何对Firestore中扑扑的位置列表进行排序? [英] How to sort list of locations in flutter from Firestore?

查看:55
本文介绍了如何对Firestore中扑扑的位置列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Firebase Firestore数据库中有一个名为面包房"的集合,集合中有一系列文档,每个文档都有一个geopoint字段,我在其中输入了他们的经度和纬度.为了访问它们并查看距用户最近的面包店,我创建了一个ListView.builder.但是,我正在尝试根据其相对于用户当前位置的地理位置来对akeryList进行排序,但它无法正常工作.我已经尝试过了:

I've got a collection in my firebase firestore database called 'bakeries' and inside the collection has a series of documents, each with a geopoint field, where I typed in their latitudes and longitudes. To access them and see the nearest bakeries to the users, I created a ListView.builder. But, I'm trying to sort the bakeryList by its geopoint in relation to the user's current location and it's not working. I've tried this:

bakeryList.sort((a, b){
  return a['geopoint'].compareTo(myLocation);
});

但是它没有返回最近的面包店.任何想法将不胜感激!

But it's not returning the nearest bakeries. Any ideas would be hugely appreciated!

  final userLocation = Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

StreamSubscription<QuerySnapshot> subscription;
  List <DocumentSnapshot> bakeryList;

  final Query collectionReference = Firestore.instance.collection(('bakeries'));

  @override
  void initState() {
    super.initState();

    final myLocation = Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

    subscription = collectionReference.snapshots().listen((data) {
      setState(() {
        bakeryList = data.documents;

      });
    });

  }



  @override
  Widget build(BuildContext context) {


    return bakeryList != null ?

    ListView.builder(

            itemCount: bakeryList.length,
            itemBuilder: (context, index){

          String imgPath = bakeryList[index].data['image'];
          String bakeryTextPath = bakeryList[index].data['name'];
          String locationNamePath = bakeryList[index].data['location name'];
          GeoPoint geoPointPath = bakeryList[index].data['geopoint'];
          final geolocation = Text('${geoPointPath.latitude}, ${geoPointPath.longitude}');

          return BakeryCard(
            etaText: '${geoPointPath.latitude}, ${geoPointPath.longitude}',
            locationText: locationNamePath,
            merchantText: bakeryTextPath,
            assetImage: Image.network(imgPath),

            function: (){});
        })
        : Center(child: CircularProgressIndicator(),
    );

  }

推荐答案

我认为您的问题出在您的排序函数中:

I think your problem is in your sort function:

bakeryList.sort((a, b){
  return a['geocode'].compareTo(myLocation);
});

在这里,您只是在比较第一家面包店的位置和用户的位置.我认为您真正想要的是这样的东西:

Here you are just comparing the position of the first bakery with the user's location. I think what you really want is something like:

bakeryList.sort((a, b){
  return distance(a['geocode'], myLocation).compareTo(distance(b['geocode'], myLocation));
});

其中的功能 distance 为您提供两点之间的距离.

where the function distance gives you the distance between two points.

这篇关于如何对Firestore中扑扑的位置列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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