如何检查坐标是否在公里半径内?

How to check if coords are within a radius in km?

给定 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 公式,我如何检查给定 latlng 的列表是否来自json5 kmradius 范围内,使用 haversine?

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

你可以这样做来过滤城市,你提供的数据相距太远,为了在结果中得到任何东西我已经移动了 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)

只需将此添加到您的代码中就足够了。

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