[React] λ€μ μ£Όμ API νμ©νκΈ°
νλ‘κ·Έλλ° μΈκ³λ₯Ό νꡬν©μλ€.
λ€μ μ£Όμ 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λ₯Ό νμ©νμ¬ νλ‘μ νΈμ μμ±λλ₯Ό λμΌ μ μμ΅λλ€.