流星计算用户之间的距离。服务器端与客户端

时间:2016-12-12 00:33:57

标签: meteor meteor-blaze

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

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

我目前正在成功推动地理定位,并在客户端运行一个基本的距离公式,以便根据请求显示雇主与每位候选人之间的距离,然后显示/隐藏候选人。

我被告知我应该在服务器端运行计算,只需按下一个数字,即。 10个代表10km,每个候选人。然后对该号码运行过滤器。

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

第二个问题是,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;
  }

});

1 个答案:

答案 0 :(得分:1)

我会在提取要显示的候选人时进行过滤。您在模板上发布/订阅时,或者当您在帮助程序中获取时:

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

如果地址是2d索引。按此顺序指定坐标:“经度,纬度。”

来自Mongodb docs:

  

$ near指定地理空间查询返回的点   距离最近的文件。 $ near运算符可以指定   GeoJSON点或传统坐标点。

$ minDistance&amp; $ maxDistance是可选的

相关问题