가장 많이 참고한 블로그:
https://wildeveloperetrain.tistory.com/171
카카오 개발자 문서 (로컬 REST API)
https://developers.kakao.com/docs/latest/ko/local/dev-guide#address-coord
- 주소를 통해서 좌표를 반환하기
- 좌표를 통해서 주소를 반환하기
좌표 : [경도 langtitude, 위도 latitude]
.gitignore 처리하였음.
json으로 파싱하지 않고 응답받을 클래스를 정의해버렸습니다..
https://developers.kakao.com/docs/latest/ko/local/dev-guide#address-coord-response
https://developers.kakao.com/docs/latest/ko/local/dev-guide#coord-to-address-request
public ResponseEntity<LocationInfoRes> kakaoLocalAPI(String query){
String url = "https://dapi.kakao.com/v2/local/search/address.json?query=" + query;
RestTemplate restTemplate = new RestTemplate();
//HTTPHeader를 설정해줘야 하기때문에 생성함
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "KakaoAK "+ KakaoSecret.kakaoRestAPIKey);
headers.set("content-type", "application/json;charset=UTF-8");
HttpEntity<?> entity = new HttpEntity<>(headers);
return restTemplate.exchange(
url,
HttpMethod.GET,
entity,
LocationInfoRes.class
);
}
public ResponseEntity<LocationXYRes> kakaoXYAPI(double x, double y){
String url = "https://dapi.kakao.com/v2/local/geo/coord2address.json?"
+ "x=" + x
+ "&y=" + y
+ "&input_coord=WGS84";
RestTemplate restTemplate = new RestTemplate();
//HTTPHeader를 설정해줘야 하기때문에 생성함
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "KakaoAK "+ KakaoSecret.kakaoRestAPIKey);
headers.set("content-type", "application/json;charset=UTF-8");
HttpEntity<?> entity = new HttpEntity<>(headers);
return restTemplate.exchange(
url,
HttpMethod.GET,
entity,
LocationXYRes.class
);
}
}
public AddressNames addrName(Double longitude, Double latitude) throws BaseException{
ResponseEntity<LocationXYRes> apiResponse;
try{
apiResponse = locationValue.kakaoXYAPI(longitude, latitude);
}catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
// 카카오 위치 API 응답 실패
if (apiResponse.getStatusCode() != HttpStatus.OK){
throw new BaseException(DATABASE_ERROR);
}
try{
// 응답 값이 1이상이면 결과가 존재함
LocationXYRes currentLoc = apiResponse.getBody();
if (currentLoc.getDocuments().length == 0){
throw new BaseException(DATABASE_ERROR);
}
if (currentLoc.getDocuments()[0] != null){
String locAddress = currentLoc.getDocuments()[0].getAddress().getAddress_name();
String roadAddress = currentLoc.getDocuments()[0].getRoad_address().getAddress_name();
return new AddressNames(locAddress, roadAddress);
}
return new AddressNames("지도를 조금만 옮겨주세요", "좌표가 정확하지 않음.");
}catch(NullPointerException ne){
return new AddressNames("지도를 조금만 옮겨주세요", "좌표가 정확하지 않음.");
}catch (Exception e){
throw new BaseException(DATABASE_ERROR);
}
}
[SpringBoot] Kakao REST API 검색어로 위도 경도 좌표 받아오기
https://velog.io/@clickyour/SpringBoot-Kakao-REST-API-%EA%B2%80%EC%83%89%EC%96%B4%EB%A1%9C-%EC%9C%84%EB%8F%84-%EA%B2%BD%EB%8F%84-%EC%A2%8C%ED%91%9C-%EB%B0%9B%EC%95%84%EC%98%A4%EA%B8%B0#%EB%B0%9C%EA%B8%89%EB%B0%9B%EC%9D%80-%ED%82%A4%EB%A1%9C-api-%ED%98%B8%EC%B6%9C%ED%95%98%EA%B8%B0
[Spring Boot] 주소에서 위/경도 가져오기 (with Geocoding API)
https://furang-note.tistory.com/19
GEOCODING
https://developers.google.com/maps/documentation/geocoding/requests-geocoding?hl=ko#geocoding-lookup
역 지오코딩
공감하며 읽었습니다. 좋은 글 감사드립니다.