Skip to content

Commit

Permalink
feat: add getDistance
Browse files Browse the repository at this point in the history
  • Loading branch information
bingtsingw committed Feb 5, 2024
1 parent c1d44d0 commit 08ac433
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 23 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"dependencies": {
"date-fns": "^2.30.0",
"date-fns-tz": "^2.0.0",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"valibot": "^0.27.1"
},
"devDependencies": {
"@babel/core": "^7.23.7",
Expand Down
30 changes: 8 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './exception';
export * from './format';
export * from './generator';
export * from './lang';
export * from './misc';
19 changes: 19 additions & 0 deletions src/misc/get-distance.spec.ts
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);
});
});
56 changes: 56 additions & 0 deletions src/misc/get-distance.ts
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;
};
1 change: 1 addition & 0 deletions src/misc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './get-distance';

0 comments on commit 08ac433

Please sign in to comment.