返回 AiToEarn
map.ts
1 /*
2 * @Author: nevin
3 * @Date: 2024-07-29 11:14:20
4 * @LastEditTime: 2024-07-29 11:17:33
5 * @LastEditors: nevin
6 * @Description: 地图
7 */
8 export function isWithinMeters(
9 locus1: number[],
10 locus2: number[],
11 distanceInMeters: number, // 千米
12 ) {
13 const [lat1, lon1] = locus1;
14 const [lat2, lon2] = locus2;
15
16 const R = 6371; // 地球平均半径,单位为公里
17 const dLat = deg2rad(lat2 - lat1);
18 const dLon = deg2rad(lon2 - lon1);
19 const a =
20 Math.sin(dLat / 2) * Math.sin(dLat / 2) +
21 Math.cos(deg2rad(lat1)) *
22 Math.cos(deg2rad(lat2)) *
23 Math.sin(dLon / 2) *
24 Math.sin(dLon / 2);
25 const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
26 const distance = R * c; // 距离,单位为公里
27
28 return distance <= distanceInMeters; // 判断距离是否小于等于500米
29 }
30
31 // 辅助函数,将角度转换为弧度
32 function deg2rad(deg: number) {
33 return deg * (Math.PI / 180);
34 }
35
35 lines TYPESCRIPT