๊ฐœ๋ฐœ์ผ์ง€-25 [๐Ÿ‘ฑโ€โ™‚๏ธ๊ตฌ๋งค์ž ๋„๋ฉ”์ธ: ์ฃผ์†Œ ์„ค์ •]

๊น€์„ฑ์ธยท2023๋…„ 9์›” 22์ผ
0

๊ตฌ๋งค์ž ๋„๋ฉ”์ธ ๊ธฐ๋Šฅ ์ •์˜ ํฌ์ŠคํŒ…


์นด์นด์˜ค ์ง€๋„ API ์„œ๋น„์Šค

๊ด€๋ จ ํฌ์ŠคํŒ…:
https://velog.io/@99_insung/Kakao-%EC%9C%84%EB%8F%84-%EA%B2%BD%EB%8F%84


์ฃผ์†Œ ์„ค์ • ํ™”๋ฉด


1. [GET] /jat/app/users/address

ํ˜„์žฌ ์ฃผ์†Œ ๋ฐ ์ง€์ • ์ฃผ์†Œ ์„ค์ •

  • ์œ„์น˜์ •๋ณด๋Š” ์ง€๋„ ๋‚ด์—์„œ ๋ฒ”์œ„์•ˆ์— ์กด์žฌํ•˜๋Š” ๊ฐ€๊ฒŒ๋ฅผ ์กฐํšŒํ•˜๊ธฐ์œ„ํ•ด ๋‚˜์ค‘์— ์“ฐ์ธ๋‹ค.
  • ํ”„๋ก ํŠธ์—์„œ ์„œ๋ฒ„๋กœ ์œ„์น˜์ •๋ณด๋ฅผ ์ „์†กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘๊ฐ€์ง€
    • ์›ํ•˜๋Š” ์œ„์น˜์˜ ์ฃผ์†Œ๋ฅผ ๋ณด๋‚ด๊ธฐ (๋„๋กœ๋ช…/์ง€๋ฒˆ ์ƒ๊ด€ X)
    • ํ˜„์žฌ ์ขŒํ‘œ ์œ„์น˜๋ฅผ ๋ณด๋‚ด๊ธฐ
  • ๋‘ ์ •๋ณด๋ฅผ Controller ์—์„œ ํ™•์ธํ•˜์—ฌ ์š”์ฒญ์— ๋งž๋Š” ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
    • ์ขŒํ‘œ ์ „์†ก -> ํ•ด๋‹น ์œ„์น˜์˜ ์ฃผ์†Œ๋ฅผ ์‘๋‹ต
    • ์ฃผ์†Œ ์ „์†ก -> ํ•ด๋‹น ์œ„์น˜์˜ ์ขŒํ‘œ๋ฅผ ์‘๋‹ต
  • ์‘๋‹ต ๋ฐ”๋””
  • ์ปจํŠธ๋กค๋Ÿฌ
location: ์ฃผ์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ขŒํ‘œ ์œ„์น˜ ์ •๋ณด๋ฅผ ์‘๋‹ต
longitude, latitude: ๊ฒฝ๋„/์œ„๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ฃผ์†Œ ์œ„์น˜ ์ •๋ณด๋ฅผ ์‘๋‹ต
  • ์„œ๋น„์Šค
      1. ์ฃผ์†Œ๋ฅผ ์š”์ฒญํ•ด์„œ ์ขŒํ‘œ ์ •๋ณด๋ฅผ ์‘๋‹ตํ• ๋•Œ
      • ์นด์นด์˜ค API๋ฅผ ์ด์šฉํ•˜์—ฌ ์‘๋‹ต๊ฐ’์„ ๋ฐ›๋Š”๋‹ค. ์™ธ๋ถ€ API ์š”์ฒญ์— ์‹คํŒจํ•  ๊ฒฝ์šฐ์— ์˜ˆ์™ธ์ฒ˜๋ฆฌ,
    public AddressXY userAddress(String query) throws BaseException{
        ResponseEntity<LocationInfoRes> apiResponse;
        try{
            apiResponse = locationValue.kakaoLocalAPI(query);
        }catch (Exception e) {
            throw new BaseException(DATABASE_ERROR);
        }

        // ์นด์นด์˜ค ์œ„์น˜ API ์‘๋‹ต ์‹คํŒจ
        if (apiResponse.getStatusCode() != HttpStatus.OK){
            throw new BaseException(DATABASE_ERROR);
        }

        // ์‘๋‹ต ๊ฐ’์ด 1์ด์ƒ์ด๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ์กด์žฌํ•จ
        LocationInfoRes currentLoc = apiResponse.getBody();
        if (currentLoc.getDocuments().length == 0){
            throw new BaseException(DATABASE_ERROR);
        }

        // ํ˜„์žฌ ์œ„/๊ฒฝ๋„ ๊ฐ’
        String locAddress = currentLoc.getDocuments()[0].getAddress().getAddress_name();
        String roadAddress = currentLoc.getDocuments()[0].getRoad_address().getAddress_name();
        double nowX = currentLoc.getDocuments()[0].getX();
        double nowY = currentLoc.getDocuments()[0].getY();

        return new AddressXY(locAddress, roadAddress, nowX, nowY);
    }

    1. ์ขŒํ‘œ๋ฅผ ์š”์ฒญํ•ด์„œ ์ฃผ์†Œ ์ •๋ณด๋ฅผ ์‘๋‹ตํ• ๋•Œ
    • ์นด์นด์˜ค API๋ฅผ ์ด์šฉํ•˜์—ฌ ์‘๋‹ต๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.
    • ํ”„๋ก ํŠธ์—์„œ ์ง€๋„์˜ ์ค‘์•™ ์œ„์น˜ ์ขŒํ‘œ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์„œ๋ฒ„์— ์š”์ฒญํ•˜๊ฒŒ๋œ๋‹ค.
    • ํ•œ๋ฒˆ์”ฉ ์ขŒํ‘œ๊ฐ€ ์ •ํ™•ํ•˜์ง€ ์•Š์„๋•Œ ์ฃผ์†Œ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์กฐํšŒ๋˜์ง€ ์•Š๋Š”๋ฐ ๊ทธ๋–„๋Š” ์ขŒํ‘œ๋ฅผ ์กฐ๊ธˆ ์˜ฎ๊ฒจ์„œ ๋‹ค์‹œ ์š”์ฒญํ•˜๋ฉด ์ •์ƒ ์ž‘๋™ํ•˜๊ฒŒ๋จ.
    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);
        }
    }

ํ…Œ์ŠคํŠธ

1. ์ฃผ์†Œ๋กœ ์š”์ฒญ(์šธ์‚ฐ๋Œ€ํ•™๊ต ์ฃผ์†Œ)

2. ์ขŒํ‘œ๋กœ ์š”์ฒญ(์ฃผ์†Œ๋กœ ์š”์ฒญํ•œ ์ขŒํ‘œ๋ฅผ ํ†ตํ•ด์„œ)

ํ™”๋ฉด ์ ์šฉ

  1. ๋กœ๊ทธ์ธ ํ›„ ์ดˆ๊ธฐ ํ™”๋ฉด
  2. ์ƒ๋‹จ ์ฃผ์†Œ ๋ฐ” ํด๋ฆญ
  3. ์ฃผ์†Œ ์ž…๋ ฅ ํ›„ ์ฃผ์†Œ ์„ค์ •
  4. ํ•ด๋‹น ์ฃผ์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง€๋„ ํ™”๋ฉด ์ด๋™
profile
๊ฐœ๋ฐœ์ž๊ฐ€ ๊ฟˆ์ธ 25์‚ด ๋Œ€ํ•™์ƒ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€