-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #154 from IT-Cotato/feature/153-implement-find-roa…
…d-by-google-map-api Feature/153 implement find road by google map api
- Loading branch information
Showing
16 changed files
with
461 additions
and
41 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/middle_point_search/backend/common/properties/GoogleProperties.java
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,27 @@ | ||
package middle_point_search.backend.common.properties; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@ConfigurationProperties(prefix = "google") | ||
public class GoogleProperties { | ||
|
||
private String key; | ||
private String keyName; | ||
private String baseUrl; | ||
private Map map; | ||
|
||
@Getter | ||
@Setter | ||
public static class Map { | ||
|
||
private String distanceMatrixUrl; | ||
private String origin; | ||
private String destination; | ||
private String placeIdUrl; | ||
} | ||
} |
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
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
43 changes: 43 additions & 0 deletions
43
.../src/main/java/middle_point_search/backend/domains/google/dto/DistanceMatrixResponse.java
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,43 @@ | ||
package middle_point_search.backend.domains.google.dto; | ||
|
||
import java.util.List; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class DistanceMatrixResponse extends GoogleApiResponse { | ||
|
||
private List<String> destination_addresses; | ||
private List<String> origin_addresses; | ||
private List<Row> rows; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class Row { | ||
private List<Element> elements; | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class Element { | ||
private Distance distance; | ||
private Duration duration; | ||
private String status; | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class Distance { | ||
private String text; | ||
private int value; | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public static class Duration { | ||
private String text; | ||
private int value; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/middle_point_search/backend/domains/google/dto/GoogleApiResponse.java
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,8 @@ | ||
package middle_point_search.backend.domains.google.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public abstract class GoogleApiResponse { | ||
private String status; | ||
} |
61 changes: 61 additions & 0 deletions
61
.../src/main/java/middle_point_search/backend/domains/google/dto/ReverseGeocodeResponse.java
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,61 @@ | ||
package middle_point_search.backend.domains.google.dto; | ||
|
||
import java.util.List; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class ReverseGeocodeResponse extends GoogleApiResponse { | ||
private PlusCode plus_code; | ||
private List<Result> results; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public static class PlusCode { | ||
private String compound_code; | ||
private String global_code; | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public static class Result { | ||
private List<AddressComponent> address_components; | ||
private String formatted_address; | ||
private Geometry geometry; | ||
private String place_id; | ||
private List<String> types; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public static class AddressComponent { | ||
private String long_name; | ||
private String short_name; | ||
private List<String> types; | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public static class Geometry { | ||
private Location location; | ||
private String location_type; | ||
private Viewport viewport; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public static class Location { | ||
private Double lat; | ||
private Double lng; | ||
} | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public static class Viewport { | ||
private Location northeast; | ||
private Location southwest; | ||
} | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
backend/src/main/java/middle_point_search/backend/domains/google/service/GoogleService.java
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,88 @@ | ||
package middle_point_search.backend.domains.google.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import middle_point_search.backend.common.exception.CustomException; | ||
import middle_point_search.backend.common.exception.errorCode.UserErrorCode; | ||
import middle_point_search.backend.common.properties.GoogleProperties; | ||
import middle_point_search.backend.common.webClient.util.WebClientUtil; | ||
import middle_point_search.backend.domains.google.dto.DistanceMatrixResponse; | ||
import middle_point_search.backend.domains.google.dto.GoogleApiResponse; | ||
import middle_point_search.backend.domains.google.dto.ReverseGeocodeResponse; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class GoogleService { | ||
|
||
private final WebClientUtil webClientUtil; | ||
private final GoogleProperties googleProperties; | ||
|
||
// 이동 시간 조회 | ||
public DistanceMatrixResponse findTravelTimes(String destPlaceId, List<String> originPlaceIds) { | ||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); | ||
params.add(googleProperties.getMap().getOrigin(), makePlaceIdsQuery(originPlaceIds)); | ||
params.add(googleProperties.getMap().getDestination(), makePlaceIdQuery(destPlaceId)); | ||
params.add("language", "ko"); | ||
params.add("mode", "transit"); | ||
params.add("region", "KR"); | ||
|
||
DistanceMatrixResponse response = webClientUtil.getGoogle(googleProperties.getMap().getDistanceMatrixUrl(), | ||
params, DistanceMatrixResponse.class); | ||
|
||
// 상태코드 체크 | ||
checkGoogleApiResponseStatus(response); | ||
|
||
return response; | ||
} | ||
|
||
// id들을 |로 구분하여 query문을 만들어줌 | ||
private String makePlaceIdsQuery(List<String> placeIds) { | ||
StringBuilder query = new StringBuilder(); | ||
// coordinate 사이에 |를 넣어줌, 마지막에 | 없음 | ||
for (int i = 0; i < placeIds.size(); i++) { | ||
query.append("place_id:").append(placeIds.get(i)); | ||
if (i != placeIds.size() - 1) { | ||
query.append("|"); | ||
} | ||
} | ||
return query.toString(); | ||
} | ||
|
||
// 장소 ID 쿼리 파라미터 생성 | ||
private String makePlaceIdQuery(String placeId) { | ||
return "place_id:" + placeId; | ||
} | ||
|
||
// 구글 placeId 찾기 | ||
public String findGooglePlaceId(Double latitude, Double longitude) { | ||
MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); | ||
params.add("latlng", latitude + "," + longitude); | ||
params.add("language", "ko"); | ||
|
||
ReverseGeocodeResponse response = webClientUtil.getGoogle( | ||
googleProperties.getMap().getPlaceIdUrl(), | ||
params, | ||
ReverseGeocodeResponse.class | ||
); | ||
|
||
// 상태코드 체크 | ||
checkGoogleApiResponseStatus(response); | ||
|
||
return response.getResults().get(0).getPlace_id(); | ||
} | ||
|
||
// 상태코드 확인 | ||
public void checkGoogleApiResponseStatus(Object response) { | ||
GoogleApiResponse googleApiResponse = (GoogleApiResponse) response; | ||
if (!googleApiResponse.getStatus().equals("OK")) { | ||
throw CustomException.from(UserErrorCode.API_INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.