펫클리닉과 함께하는 카카오맵 API #5

이전 게시물

2023-03-15 – (프로젝트 개요/펫종합(캣독포레스트) 솔루션) – 카카오맵 API를 이용한 펫병원#4

@SpringBootTest
@Slf4j
public class KakaoMapTest {

    @Autowired
    GoverMentService goverMentService;

    @Test
    @DisplayName("return location input lat, lon")
    public void getLocationTest() throws Exception {
        Double x = 190562.90915523;
        Double y = 189333.017062573;
        /*
        String url = "https://dapi.kakao.com/v2/local/geo/coord2regioncode.json";
        double x = 126.9863309;
        double y = 37.563398;
        */
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://dapi.kakao.com/v2/local/geo/coord2regioncode.json";
        UriComponents uri = UriComponentsBuilder.newInstance()
                .fromHttpUrl(url)
                .queryParam("x",x)
                .queryParam("y",y)
                .queryParam("input_coord","TM")
                .build();

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", String.format("KakaoAK APP_KEY"));

        // when
        HttpEntity requestMessage = new HttpEntity(httpHeaders);
        ResponseEntity response = restTemplate.exchange(
                uri.toUriString(),
                HttpMethod.GET,
                requestMessage,
                String.class);
        // then
        // 해당 JObject와 Response 객체간의 매핑
        Gson gson = new Gson();
        KaKaoMapResponse mapped_data = gson.fromJson(response.getBody().toString(),KaKaoMapResponse.class);
        String firstName = mapped_data.documents.get(0).address_name;
        String secondName = mapped_data.documents.get(1).address_name;
        String target = mapped_data.documents.get(0).region_type;
        System.out.println(firstName);
        System.out.println(secondName);
        System.out.println(target);
        assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
    }


    @Test
    @DisplayName("bring goverment data")
    public void govermentDBTest() {
        // goverMentService.getAll();
        Assert.assertNotNull(goverMentService.getAll());
        List<GovermentDTO> all = goverMentService.getAll();

        IntStream.rangeClosed(1, 10).forEach(count ->{
            System.out.println("longitude : " + all.get(count).getLongitude() + " latitude : "+ all.get(count).getLatitude());
        });
    }

    @Test
    @Transactional
    @DisplayName("convert goverment data to kakao location")
    public void convertDBTest() {

        RestTemplate restTemplate = new RestTemplate();
        String url = "https://dapi.kakao.com/v2/local/geo/coord2regioncode.json";

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", String.format("KakaoAK APP_KEY"));

        List<GovermentDTO> all = goverMentService.getAll();
        int bCnt = 0;
        int hCnt = 0;
        for(int idx = 0; idx < all.size(); idx++){
            Double x = all.get(idx).getLongitude();
            Double y = all.get(idx).getLatitude();

            UriComponents uri = UriComponentsBuilder.newInstance()
                    .fromHttpUrl(url)
                    .queryParam("x",x)
                    .queryParam("y",y)
                    .queryParam("input_coord","TM")
                    .build();

            // when
            HttpEntity requestMessage = new HttpEntity(httpHeaders);
            ResponseEntity response = restTemplate.exchange(
                    uri.toUriString(),
                    HttpMethod.GET,
                    requestMessage,
                    String.class);

            // then
            // 해당 JObject와 Response 객체간의 매핑
            Gson gson = new Gson();
            KaKaoMapResponse mapped_data = gson.fromJson(response.getBody().toString(),KaKaoMapResponse.class);
            String firstName = mapped_data.documents.get(0).address_name;
            String secondName = mapped_data.documents.get(1).address_name;
            String target = mapped_data.documents.get(0).region_type;


            if (mapped_data.documents.get(0).region_type.equals("H")) {
                hCnt++;
            } else {
                bCnt++;
            }
            if(mapped_data.documents.get(1).region_type.equals("H")){
                hCnt++;
            }else {
                bCnt++;
            }
            assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
        }
        log.info("bCnt : "+bCnt);
        log.info("hCnt : "+hCnt);
    }
}

코코아 coord2regioncode API를 사용하여 EPSG:2097 지도 데이터를 WGS84 형식으로 변환하고 테스트하여 지역 정보를 얻습니다.

공개 데이터 가져오기 테스트 가져온 모든 공개 데이터를 변환하기 위한 테스트가 생성되었습니다.

이제 변환된 데이터를 DB에 넣고 병원 url에 push만 하면 됩니다.

모든 데이터를 REST API 형식으로 변환하고 마커 배열에 넣고 클러스터로 출력합니다.

그리고 지리적 위치로 현재 좌표를 지정하고 현재 좌표와 지도 크기를 비교하여 지도 모서리에 있는 마커를 목록으로 표시(최대 5개 항목)

다음 위치로 정렬

그리고 마커를 클릭하면 자세한 정보가 표시되는 페이지를 만들어야 합니다.

마커의 region_depth 값과 함께 setText() 함수를 사용하여 클러스터의 텍스트를 변경해야 하는데 관련 예제를 찾고 있습니다.