9oj0e / pathorder_server

3 stars 4 forks source link

feat: 거리 계산(매장 목록, 디테일, 좋아요 리스트) #83

Closed Hyeonjeong-JANG closed 1 month ago

Hyeonjeong-JANG commented 1 month ago

⚒️ 수정사항: 가게 목록보기 가까운 거리순으로 정렬, distance를 String으로 반환하던 것 int로 변경

✔️ 손님-가게 거리 계산

🐯 DistanceUtil

거리 계산하는 유틸이 필요함. 두 지점의 위도와 경도를 입력받아, 구면 삼각법(Haversine 공식)을 적용하여 두 지점 사이의 거리를 계산하고, 그 거리를 미터 단위로 반환함.

private static final double EARTH_RADIUS_KM = 6371.0;

    public static int calculateDistance(double lat1, double lon1, double lat2, double lon2) {
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);

        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                   + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
                     * Math.sin(dLon / 2) * Math.sin(dLon / 2);

        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distanceInKm = EARTH_RADIUS_KM * c;
        double distanceInMeters = distanceInKm * 1000;
        return (int) Math.round(distanceInMeters); // 미터로 변환 후 반올림
    }

🐯 StoreService.getStoreInfo()

public StoreResponse.StoreInfoDTO getStoreInfo(int userId, int storeId, double customerLatitude, double customerLongitude) {
        Store store = storeRepository.findById(storeId)
                .orElseThrow(() -> new Exception404("찾을 수 없는 매장입니다."));

       ...생략
        int distance = DistanceUtil.calculateDistance(customerLatitude, customerLongitude, store.getLatitude(), store.getLongitude());

        return new StoreResponse.StoreInfoDTO(store, likeCount, isLiked, reviewCount, distance);
    }