如何根据距离对用户进行排序,即让他们拥有最近的用户?我可以将用户带到一定半径范围内,而无需进行排序

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

我能够将半径一定范围内的用户带到当前用户(即10公里范围内的用户)。现在,我想根据它们的距离对它们进行排序,并在最前面列出最接近的那些。为了解决这个问题,我尝试了以下代码,但出现了错误:

错误:收到此错误:“未处理的异常:PlatformException(错误,无效的查询。'in'过滤器不能在值数组中包含'null'。null)” –

详细代码:

临时模型:(用于存储用户标识和地理区域的集合)

 class certainrangeusers {
 final String id;//users ids
 final double away;//to store distance
 final GeoPoint userpoint;//other users geopoints
  certainrangeusers({this.id, this.away, this.userpoint});
  }

变量:

  List<certainrangeusers> info= [];

code:

       getUsers() async
         {
          double radius = 0.3;
            String field = "GeoPosition";
             print(currentUser.point.longitude);
                GeoFirePoint center = geo.point(latitude: currentUser.point.latitude,
     longitude: currentUser.point.longitude); // Here to calculate the geopositon, geohash and geopoint of the current user

   var collectionRef = Firestore.instance.collection('user_locations').document(currentUser.pincode)
     .collection('Users_comp  lete_address_and_geopoint');

      this.stream =  geo.collection(collectionRef: collectionRef)
          .within(center: center, radius: radius, field: field, strictMode: false);

        Future<List<certainrangeusers>> users1 =  stream.first.then((documents) => documents.map((doc) => 
      certainrangeusers(
     id: doc['userPhone'],
    userpoint : doc['GeoPosition']['geopoint'], // here other users geopoints who are in 300mteres range are stored
   )
    ).toList());

      users1.then((val) async{
       for(var value in val){ // this loop is to calculate the distance of the current user to the other users 
        info.add(certainrangeusers(
        away: await Geolocator().distanceBetween(currentUser.point.latitude,currentUser.point.longitude, value.userpoint.latitude, value.userpoint.longitude),
          //away: center.distance(lat: value.userpoint.latitude,lng: value.userpoint.longitude),
                     ));
              info.sort((a,b) => a.away.compareTo(b.away));//here we r sorting them
            }
          List ids = [];
          for(var value in info){
         ids.add(value.id);// adding them to empty list in order to bring them according to their ids sorted
             }
        QuerySnapshot snapshot = await Firestore.instance.collection('users').where('id', whereIn: ids).getDocuments(); // here we are passing the above list of ids
       List<User> users = snapshot.documents.map((doc) => User.fromDocument(doc)).toList();
        setState(() {
        this.users = users;
             });
          });
      }

屏幕截图: [在粉红色框区域中,将完成整个计算并分配ID,并且仅分配那些从firebase用户集合中带来的文档]

Click here for the screenshot of the above, where specific place, I was struck

firebase flutter google-cloud-firestore geolocation geofire
1个回答
0
投票

这可以通过在for循环本身中添加“ id”来轻松解决,如下图所示:

       for(var value in val){ // this loop is to calculate the distance of the current user to the other users 
              info.add(certainrangeusers(
                   away: await Geolocator().distanceBetween(currentUser.point.latitude,currentUser.point.longitude,value.userpoint.latitude, value.userpoint.longitude),
                   id: value.id,
                 ));
          info.sort((a,b) => a.away.compareTo(b.away));//here we r sorting them
        }
© www.soinside.com 2019 - 2024. All rights reserved.