如何检查坐标是否在以km为单位的半径内?

时间:2019-03-01 23:28:02

标签: javascript jquery haversine

使用坐标json

var centerLat = 51.6000;
var centerLng = 12.8000;

var posts = [
  {
    name: 'PostA',
    latitude: '52.5167',
    longitude: '13.3833',
  },
  {
    name: 'PostB',
    latitude: '52.9667',
    longitude: '13.7167',
  },
  {
    name: 'PostC',
    latitude: '26.7767',
    longitude: '18.4567',
  }
];

我在this link上找到了 haversine 公式,如何检查从lat得到的给定lngjson的列表使用 haversine 5公里radius范围内?

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)
}

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作以过滤城市,即您提供的数据相距太远,以获取结果,我将centerLatcenterLng移动到了最近的数组中末尾的城市。

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)
}

var centerLat = 52.5167;
var centerLng = 13.3933;

var posts = [{
    name: 'PostA',
    latitude: '52.5167',
    longitude: '13.3833',
  },
  {
    name: 'PostB',
    latitude: '52.9667',
    longitude: '13.7167',
  },
  {
    name: 'PostC',
    latitude: '26.7767',
    longitude: '18.4567',
  }
];

let closePosts = [];

posts.forEach((post) => {
  if (getDistanceFromLatLonInKm(centerLat, centerLng, post.latitude, post.longitude) < 5) {
    closePosts.push(post);
  }
});

console.log(closePosts)

答案 1 :(得分:1)

只需将其添加到您的代码中即可。

posts.forEach(post => {
  const distance = getDistanceFromLatLonInKm(centerLat, centerLng, post.latitude, post.longitude);
  if (distance <= 200) {
    console.log(`Distance to ${post.name}: ${distance} km`);
  }
});

但是我建议多清理一些代码。我使用200而不是5,因为您的示例无法产生结果。

这是重构的代码段。

const posts = [
  {
    name: 'Berlin',
    latitude: '52.520008',
    longitude: '13.404954',
  },
  {
    name: 'Hamburg',
    latitude: '53.551086',
    longitude: '9.993682',
  },
  {
    name: 'München',
    latitude: '48.135124',
    longitude: '11.581981',
  },
  {
    name: 'Lübeck',
    latitude: '53.865467',
    longitude: '10.686559',
  },
  {
    name: 'Schwerin',
    latitude: '53.635502',
    longitude: '11.401250',
  },
];

function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
  const R = 6371; // Radius of the earth in km
  const dLat = deg2rad(lat2-lat1); // deg2rad below
  const dLon = deg2rad(lon2-lon1);
  const 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)
      ;
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  const d = R * c; // Distance in km
  return d;
}

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

function findClosePosts(location, radius, posts) {
  return posts.filter((post) =>
    // find close points within the radius of the location, but exclude the location itself from results
    getDistanceFromLatLonInKm(location.latitude, location.longitude, post.latitude, post.longitude) <= radius && location !== post);
}

function findLocationByName(name, posts) {
  return posts.find((post) => post.name === name);
}

const hamburg = findLocationByName('Hamburg', posts);
const closePosts = findClosePosts(hamburg, 200, posts);
console.log(closePosts);