JS获取经纬度之间的距离和之间所有的经纬度

// 两点之间经纬度
function getPointsBetweenLatLong(lat1, lon1, lat2, lon2, numPoints) {
  const points = [];
  const dLat = (lat2 - lat1) / numPoints;
  const dLon = (lon2 - lon1) / numPoints;
  
  for (let i = 0; i <= numPoints; i++) {
    const lat = lat1 + i * dLat;
    const lon = lon1 + i * dLon;
    points.push({ latitude: lat, longitude: lon });
  }
  
  return points;
}

// 两点之间距离
function getDistanceInMeters(lat1, lon1, lat2, lon2) {
  const rad = Math.PI / 180;
  const a = 0.5 - Math.cos((lat2 - lat1) * rad) / 2 + Math.sin(lat1 * rad) * Math.sin(lat2 * rad) / 2;
  const b = Math.cos(lat1 * rad) * Math.sin((lat2 - lat1) * rad) / 2;
  const c = lon1 * lon2 * b;
  const d = 2 * a + c;
  const R = 6371000; // 地球平均半径,单位为米
  return R * d; // 返回距离,单位为米
}

const startPoint = { latitude: 40.7128, longitude: -74.0060 }; // 起点经纬度
const endPoint = { latitude: 51.5074, longitude: -0.1278 }; // 终点经纬度
const numPoints = getDistanceInMeters(startPoint.latitude, startPoint.longitude, endPoint.latitude, endPoint.longitude); // 你想获取的点数

const points = getPointsBetweenLatLong(startPoint.latitude, startPoint.longitude, endPoint.latitude, endPoint.longitude, numPoints);
console.log(points,'输出所有经纬度点'); // 输出所有经纬度点

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值