i
This commit is contained in:
29
src/utils/index.ts
Normal file
29
src/utils/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
// 经纬度计算距离
|
||||
export function calculateDistance(
|
||||
lat1: number,
|
||||
lon1: number,
|
||||
lat2: number,
|
||||
lon2: number
|
||||
): string {
|
||||
const R = 6371; // 地球平均半径(单位:千米)
|
||||
|
||||
const dLat = toRadians(lat2 - lat1);
|
||||
const dLon = toRadians(lon2 - lon1);
|
||||
|
||||
const a =
|
||||
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
||||
Math.cos(toRadians(lat1)) *
|
||||
Math.cos(toRadians(lat2)) *
|
||||
Math.sin(dLon / 2) *
|
||||
Math.sin(dLon / 2);
|
||||
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
|
||||
const distance = R * c;
|
||||
return distance.toFixed(2);
|
||||
}
|
||||
|
||||
// 将角度转换为弧度
|
||||
function toRadians(degrees: number): number {
|
||||
return (degrees * Math.PI) / 180;
|
||||
}
|
||||
Reference in New Issue
Block a user