// 两点之间经纬度
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,'输出所有经纬度点'); // 输出所有经纬度点
JS获取经纬度之间的距离和之间所有的经纬度
最新推荐文章于 2024-08-23 15:04:43 发布
本文介绍了两个JavaScript函数:getPointsBetweenLatLong用于生成两点之间按指定数量等间距的经纬度点,getDistanceInMeters则计算两点之间的距离。通过给定起点和终点经纬度,可以获取线路上的各个点坐标。
摘要由CSDN通过智能技术生成