计算两个经纬度坐标之间的距离

这里的每个坐标值都是弧度制!弧度制啊弧度制

  const coord1 = [lat1 / 180 * Math.PI, lon1 / 180 * Math.PI]
  const coord2 = [lat2 / 180 * Math.PI, lon2 / 180 * Math.PI]
/**
 * 计算两个经纬度坐标对之间的距离,单位: km
 * @param {Array} coord1 第一个点的经纬度 [lat, lon]
 * @param {Array} coord2 第二个点的经纬度 [lat, lon]
 * @returns 两个坐标对之间的距离,保留两位小数,单位 km
 */
function calcDisByLatLon(coord1, coord2) {
  const R = 6378.137
  const a = coord1[0] - coord2[0]
  const b = coord1[1] - coord2[1]
  const result = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(coord1[0]) * Math.cos(coord2[0]) * Math.pow(Math.sin(b / 2), 2))) * R;
  return Math.round(result * 100) / 100
}
posted @ 2023-02-16 16:53  echo_lovely  阅读(72)  评论(0编辑  收藏  举报