【问题标题】:how to get the all zipcode within the 5miles of distance如何获取5英里距离内的所有邮政编码
【发布时间】:2014-10-21 21:19:49
【问题描述】:

我想要用户输入的邮政编码 5 英里范围内的所有邮政编码。

【问题讨论】:

  • 这个不太清楚,你想得到一个给定中心点和半径的圆的所有坐标吗?
  • 如果我插入密码然后从 5 英里的距离我想要所有的邮政编码,我想检查它给猫鼬。和匹配的结果我想显示为结果
  • 您是否也在数据库中存储带有邮政编码的人的坐标?
  • 没有。在数据库中有公司详细信息和邮政编码。如果我插入邮政编码 123456,那么我想在 5 英里/5 公里距离内搜索该邮政编码附近的哪家公司
  • 为什么需要近似一个圆? $near$maxDistance 参数不是为您执行此操作吗?

标签: node.js mongodb mongoose geospatial


【解决方案1】:

看到你有圆的中心坐标和圆的半径说 5 英里,现在对于数据库中的每个坐标点,通过应用距离公式检查中心与该点之间的距离,并检查它是否小于半径, 总之,

C = point that is the center of the area
P = point we want to check for being inside the area
R = radius of the area

P is inside the area if

||P-C|| <= R

编辑-

function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {
  var R = 6371; // Radius of the earth in km
  var dLat = deg2rad(lat2-lat1);  // deg2rad below
  var dLon = deg2rad(lon2-lon1); 
  var a = 
    Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
    Math.sin(dLon/2) * Math.sin(dLon/2)
    ; 
  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
  var d = R * c; // Distance in km
  return d;
}

function deg2rad(deg) {
  return deg * (Math.PI/180)
}

【讨论】:

  • 如何在nodejs中实现。
  • @asmita 我正在用计算两个坐标之间距离的函数更新我的答案
  • 感谢阳光。但它仍然是我的问题没有解决。我如何将邮政编码与此代码进行比较?
  • 由于 MongoDB 中已经存在此功能,因此在较大的结果集上在 nodejs 中执行此操作将非常低效。
  • 阅读 $geoNear 文档。如果您仍有疑问,请在尝试过之后再回来
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多