-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1d44d0
commit 08ac433
Showing
6 changed files
with
87 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe, expect, test } from 'bun:test'; | ||
import { getDistance } from './get-distance'; | ||
|
||
describe('misc', () => { | ||
test('getDistance', () => { | ||
expect(getDistance({ latitude: 1.1111, longitude: 1 }, { latitude: 1.1111, longitude: 1 })).toEqual(0); | ||
|
||
expect(() => { | ||
getDistance({ latitude: '11' as any, longitude: 1 }, { latitude: 1, longitude: 1 }); | ||
}).toThrow('坐标参数错误'); | ||
|
||
const distance = getDistance( | ||
{ latitude: 39.916668, longitude: 116.383331 }, | ||
{ latitude: 31.150681, longitude: 121.124176 }, | ||
); | ||
|
||
expect(distance).toEqual(1065706.56); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { number, object, safeParse } from 'valibot'; | ||
import { _exception } from '../exception'; | ||
|
||
const vPoint = object({ | ||
latitude: number(), | ||
longitude: number(), | ||
}); | ||
|
||
const deg2rad = (deg: number) => { | ||
return deg * (Math.PI / 180); | ||
}; | ||
|
||
/** | ||
* 计算两个坐标点之间距离(单位M) | ||
* https://mixbit.xyz/post/javascript-calculates-the-distance-between-two-points-of-coordinates.html | ||
* | ||
* @param {object} point1 坐标点1 | ||
* @param {string} point1.longitude 经度 | ||
* @param {string} point1.latitude 纬度 | ||
* @param {object} point2 坐标点2 | ||
* @param {string} point2.longitude 经度 | ||
* @param {string} point2.latitude 纬度 | ||
*/ | ||
export const getDistance = ( | ||
point1: { latitude: number; longitude: number }, | ||
point2: { latitude: number; longitude: number }, | ||
) => { | ||
if (!safeParse(vPoint, point1).success || !safeParse(vPoint, point2).success) { | ||
throw new _exception.BadRequestException('坐标参数错误'); | ||
} | ||
|
||
// 将两个点的纬度转换为弧度 | ||
const radLat1 = deg2rad(point1.latitude); | ||
const radLat2 = deg2rad(point2.latitude); | ||
|
||
// 计算两个点的纬度差和经度差 | ||
const latDiff = radLat1 - radLat2; | ||
const lngDiff = deg2rad(point1.longitude) - deg2rad(point2.longitude); | ||
|
||
// 使用Haversine公式计算两点间的大圆距离 | ||
let distance = | ||
2 * | ||
Math.asin( | ||
Math.sqrt( | ||
Math.pow(Math.sin(latDiff / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(lngDiff / 2), 2), | ||
), | ||
); | ||
|
||
// 将距离乘以地球半径(米) | ||
distance = distance * 6378137; | ||
// 四舍五入到小数点后两位 | ||
distance = Math.round(distance * 100) / 100; | ||
|
||
// 返回距离(米) | ||
return distance; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './get-distance'; |