Skip to content

Commit

Permalink
feat : 위도 경도 기반 거리 계산 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
wellbeing-dough committed Jan 23, 2024
1 parent 5d6e7df commit c765483
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/co/kr/jurumarble/drink/service/DrinkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,27 @@ public Slice<GetMapInDrinksResponse> getMapInDrinks(Double startX, Double startY
log.info("********************" + endY);
log.info("*******************" + (endX - startX));
log.info("*******************" + (endY - startY));
double distance = calculateDistanceInKm(startX, startY, endX, endY);
log.info("^^^^^^^^^^^^^^^^^^^^^^^^^^" + distance);
PageRequest pageRequest = PageRequest.of(page, size);
Slice<MapInDrinkData> drinkData = drinkRepository.findDrinksByCoordinate(pageRequest, startX, startY, endX, endY);
return new SliceImpl<>(getGetMapInDrinksResponses(drinkData), drinkData.getPageable(), drinkData.hasNext());
}

public double calculateDistanceInKm(double startX, double startY, double endX, double endY) {
final int R = 6371; // 지구의 반지름

double latDistance = Math.toRadians(endX - startX);
double lonDistance = Math.toRadians(endY - startY);

double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(Math.toRadians(startX)) * Math.cos(Math.toRadians(endX))
* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

return R * c;
}

private List<GetMapInDrinksResponse> getGetMapInDrinksResponses(Slice<MapInDrinkData> drinkData) {
return drinkData.stream()
.map(MapInDrinkData::toMapInDrinksResponse)
Expand Down

0 comments on commit c765483

Please sign in to comment.