IT/React

[React] λ‹€μŒ μ£Όμ†Œ API ν™œμš©ν•˜κΈ°

ITyranno 2024. 2. 29. 12:55
728x90
λ°˜μ‘ν˜•

 

 

 

 

 

 

 

ν”„λ‘œκ·Έλž˜λ° 세계λ₯Ό νƒκ΅¬ν•©μ‹œλ‹€.

 

 

 

 

 

 

 

 

 

λ‹€μŒ μ£Όμ†Œ API ν™œμš©ν•˜κΈ°

 

 

λ‹€μŒ μ£Όμ†Œ APIλ₯Ό ν™œμš©ν•˜μ—¬ μ‚¬μš©μžμ˜ μ£Όμ†Œ μž…λ ₯ 방식을 νŽΈλ¦¬ν•˜κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.

 

 

 

 

곡식 λ‹€μŒ μ£Όμ†Œ API μ‚¬μš©μ„€λͺ…μ„œ

 

https://postcode.map.daum.net/guide

 

Daum 우편번호 μ„œλΉ„μŠ€

우편번호 검색과 λ„λ‘œλͺ… μ£Όμ†Œ μž…λ ₯ κΈ°λŠ₯을 λ„ˆλ¬΄ κ°„λ‹¨ν•˜κ²Œ μ μš©ν•  수 μžˆλŠ” 방법. Daum 우편번호 μ„œλΉ„μŠ€λ₯Ό μ΄μš©ν•΄λ³΄μ„Έμš”. μ–΄λŠ μ‚¬μ΄νŠΈμ—μ„œλ‚˜ 무료둜 μ œμ•½μ—†μ΄ μ‚¬μš© κ°€λŠ₯ν•˜λ‹΅λ‹ˆλ‹€.

postcode.map.daum.net

 

 

 

 

 

 

 

 

λ‹€μŒ μ£Όμ†Œ API ν™œμš© νŠΉμ§•

 

 

1. API ν‚€λ₯Ό λ°œκΈ‰λ°›μ„ ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€.

2. μ‚¬μš©λŸ‰μ— λŒ€ν•œ μ œν•œμ΄ μ—†μŠ΅λ‹ˆλ‹€.

3. κΈ°μ—…μš©μ΄λ“  μƒμ—…적 μš©λ„이든 μƒκ΄€μ—†μ΄ λ¬΄λ£Œλ‘œ μ‚¬μš© κ°€λŠ₯ν•©λ‹ˆλ‹€.

4. κΈ°μ΄ˆκ΅¬μ—­λ²ˆν˜Έκ°€ λ°œκΈ‰λœ λ„λ‘œλͺ… μ£Όμ†Œ, μ˜λ¬Έ μ£Όμ†Œλ₯Ό ν™•인 κ°€λŠ₯ν•©λ‹ˆλ‹€.

5. ν–‰μ •μ•ˆμ „λΆ€μ—μ„œ μ œκ³΅ν•˜λŠ” "λ„λ‘œλͺ… μ£Όμ†Œ" DBλ₯Ό μ§μ ‘ μ—…λ°μ΄νŠΈ λ°›κ³  μžˆμœΌλ―€λ‘œ κ°€μž₯ μ΅œμ‹ μ˜ λ°μ΄ν„°λ₯Ό μ΄μš©ν•˜μ‹€ μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

 

ν•˜λ‹¨ λ‘œκ³ λ₯Ό μž„μ˜λ‘œ κ°€λ¦΄ κ²½μš°, μ‚¬μš©μ— μ œμ•½μ΄ λ°œμƒν•  μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

 

 

 

 

 

λ‹€μŒ μ£Όμ†Œ API ν™œμš© 방법

 

 

Reactμ—μ„œ νŽΈλ¦¬ν•˜κ²Œ μ‚¬μš©ν•˜λŠ” 방법은 λ‹€μŒ μ£Όμ†Œ κ΄€λ ¨ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ§Œλ“œλŠ” κ²ƒμž…λ‹ˆλ‹€.

μ €λŠ” DaumPost.jsx νŒŒμΌμ„ λ§Œλ“€μ–΄ ν•„μš”ν•œ νŽ˜μ΄μ§€μ—μ„œ < DaumPost /> 와 같은 ν˜•μ‹μœΌλ‘œ μž„ν¬νŠΈν•˜μ—¬ μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

 

 

곡식 λ¬Έμ„œμ˜ μ‚¬μš© μ˜ˆμ‹œ

<script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>
<script>
    new daum.Postcode({
        oncomplete: function(data) {
            // νŒμ—…μ—μ„œ 검색결과 ν•­λͺ©μ„ ν΄λ¦­ν–ˆμ„λ•Œ μ‹€ν–‰ν•  μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” λΆ€λΆ„μž…λ‹ˆλ‹€.
            // 예제λ₯Ό μ°Έκ³ ν•˜μ—¬ λ‹€μ–‘ν•œ ν™œμš©λ²•μ„ 확인해 λ³΄μ„Έμš”.
        }
    }).open();
</script>

 

 

DaumPost.jsx

 

제 ν”„λ‘œμ νŠΈμ—μ„œ μž‘μ„±ν•œ μ½”λ“œμž…λ‹ˆλ‹€.

λ²„νŠΌκΉŒμ§€ λͺ¨λ‘ ν¬ν•¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

import React from 'react';
import style from './DaumPost.module.css';
import { useDaumPostcodePopup } from 'react-daum-postcode'; // Daum μ£Όμ†Œ 검색 κ΄€λ ¨ hook
//μ£Όμ†Œ api

const DaumPost = ({ setAddress }) => {
    const postcodeScriptUrl = 'https://t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js';
    const open = useDaumPostcodePopup(postcodeScriptUrl);

    const handleComplete = (data) => {
        let fullAddress = data.address;
        let extraAddress = '';
        let localAddress = data.sido + ' ' + data.sigungu;

        if (data.addressType === 'R') {
            if (data.bname !== '') {
                extraAddress += data.bname;
            }
            if (data.buildingName !== '') {
                extraAddress += (extraAddress !== '' ? `, ${data.buildingName}` : data.buildingName);
            }
            fullAddress = fullAddress.replace(localAddress, '');
            fullAddress += (extraAddress !== '' ? ` (${extraAddress})` : '');
        }

        setAddress(fullAddress); // setAddressλ₯Ό ν˜ΈμΆœν•˜μ—¬ λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈμ˜ μƒνƒœλ₯Ό μ—…λ°μ΄νŠΈ
    };

    const handleClick = () => {
        open({ onComplete: handleComplete });
    };

    return <div className={style.find_address_button} type="button" onClick={handleClick}>μ£Όμ†Œκ²€μƒ‰</div>;
};

export default DaumPost;

 

 

DaumPost.module.css

 

.find_address_button {
    font-size: 1.2vh;
    color: white;
    background-color: #333;
    /* λ²„νŠΌμ˜ λ°°κ²½ 색상 */
    border: none;
    border-radius: 0.5vh;
    cursor: pointer;
    margin-left: 0.5vw;
    /* μž…λ ₯ ν•„λ“œμ™€ λ²„νŠΌ μ‚¬μ΄μ˜ 간격 */
    transition: background-color 0.3s;
    width: 10vh;
    height: 4.2vh;
    font-size: 1.5vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.find_address_button:hover {
    background-color: #5593ff;
    /* λ²„νŠΌμ— 마우슀λ₯Ό μ˜¬λ Έμ„ λ•Œμ˜ λ°°κ²½ 색상 λ³€κ²½ */
}

@media all and (min-width:1024px) and (max-width:1279px) {}

@media all and (min-width:768px) and (max-width:1023px) {}

@media all and (min-width:480px) and (max-width:767px) {}

@media all and (min-width:200px) and (max-width:479px) {}

 

 

 

 

μœ„μ™€ 같은 λ°©μ‹μœΌλ‘œ λ‹€μŒ μ£Όμ†Œ APIλ₯Ό ν™œμš©ν•˜μ—¬ ν”„λ‘œμ νŠΈμ˜ 완성도λ₯Ό 높일 수 μžˆμŠ΅λ‹ˆλ‹€.

728x90
λ°˜μ‘ν˜•