【发布时间】: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