【问题标题】:Meteor calculate distance between users. Server side vs Client Side流星计算用户之间的距离。服务器端与客户端
【发布时间】:2016-12-13 18:58:30
【问题描述】:

我有一种情况需要计算用户之间的距离。在这种特殊情况下,我有:

雇主地理位置 - 一个用户一个位置。 候选人地理位置 - 每个用户一个位置,但当雇主生成候选人列表时,会有多个候选人。

我目前正在成功地将地理位置推送到网络上,并在客户端运行一个基本的距离公式,以即时计算出雇主与每位候选人之间的距离,然后根据请求显示/隐藏候选人。

有人告诉我,我应该在服务器端运行计算并按下一个数字,即。 10代表10公里,每个候选人。然后对该号码运行过滤器。

到目前为止,我只推送了集合和字段。是否可以在服务器端运行下面​​的公式并只传递一个数字并将其“附加”给用户?

第二个问题是,Meteor 的最佳实践是什么?

我还在学习编码,如果这是一个非常明显的问题,请道歉。

客户端

路径:List.js

specialisations = specialisations.filter(function(element){
  let distance = Template.instance().distanceFromEmployerFilter.get();
  let user = Meteor.users.findOne({_id: element.candidateUserId});
  let candidateLat = user && user.profile && user.profile.address && user.profile.address.latitude;
  let candidateLong = user && user.profile && user.profile.address && user.profile.address.longitude;
  let company = CompanyDetails.findOne({employerUserId:  Meteor.userId()});
  let companyLat = company && company.latitude;
  let companyLong = company && company.longitude;

  var R = 6371; // Radius of the earth in km
  var dLat = (companyLat-candidateLat) * (Math.PI/180);
  var dLon = (companyLong-candidateLong) * (Math.PI/180);
  var a =
  Math.sin(dLat/2) * Math.sin(dLat/2) +
  Math.cos((candidateLat) * (Math.PI/180)) * Math.cos((companyLat) * (Math.PI/180)) *
  Math.sin(dLon/2) * Math.sin(dLon/2)
  ;
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  var distanceInKM = R * c; // Distance in km


  if (distanceInKM <= distance) {
    return true;
  } else {
    return false;
  }

});

【问题讨论】:

  • 如果您尝试按与某个点的距离对人员进行过滤和排序,那么您最好的选择可能是在服务器上使用$near 进行地理空间 MongoDB 查询。跨度>

标签: meteor meteor-blaze


【解决方案1】:

我会对要显示的候选人的提取进行过滤。无论是在您的模板上发布/订阅,还是在您的助手中获取时:

Meteor.users.find({
  "profile.address" : {
  $near: {
     $geometry: {
        type: "Point" ,
        coordinates: [ <Employerlongitude> , <Employerlatitude> ]
     },
     $maxDistance: <distance in meters>,
     $minDistance: <distance in meters>
  }}}).fetch();

如果地址是二维索引。按以下顺序指定坐标:“经度,纬度”。

来自 Mongodb 文档:

$near 指定地理空间查询返回的点 从最近到最远的文件。 $near 运算符可以指定 GeoJSON 点或旧坐标点。

$minDistance 和 $maxDistance 是可选的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2011-02-10
    • 2016-02-16
    • 2023-03-12
    相关资源
    最近更新 更多