BLCAN'S LAB

๋ฐ˜์‘ํ˜•

์›น ๊ฐœ๋ฐœ์„ ํ•˜๊ฒŒ ๋˜๋ฉด ํšŒ์›๊ฐ€์ž…์ด๋‚˜ ์ฃผ์†Œ๊ด€๋ฆฌ ๋“ฑ ์ฃผ์†Œ ํ˜น์€ ์šฐํŽธ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด ์žˆ๋‹ค. ๋‹จ์ˆœํžˆ ์ด์šฉ์ž๊ฐ€ ๋ชจ๋“  ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๊ธ€์„ ์ฝ๊ฒŒ ๋  ์‚ฌ๋žŒ๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๊ฒฝํ—˜ํ•ด ๋ดค์„ ๋“ฏ์ด ๋Œ€๋‹ค์ˆ˜์˜ ์„œ๋น„์Šค๋“ค์€ ์šฐํŽธ๋ฒˆํ˜ธ ๊ฒ€์ƒ‰ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•ด ์ด์šฉ์ž๋“ค์—๊ฒŒ ํŽธ๋ฆฌํ•จ์„ ์ œ๊ณตํ•œ๋‹ค.

 

์ฒ˜์Œ์œผ๋กœ ์›นํŽ˜์ด์ง€๋ฅผ ์ œ์ž‘ํ•˜๊ณ  ์žˆ๋Š” ๋ณธ์ธ์ด ํ–ˆ๋˜ ์‚ฝ์งˆ๋“ค์„ ๊ณต์œ ํ•˜๋ฉด์„œ ์นด์นด์˜ค ์šฐํŽธ๋ฒˆํ˜ธ API ์ด์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐœ๋ฐœ์ž๋ถ„๋“ค์—๊ฒŒ ํ™œ์šฉ๋ฒ•์„ ๊ณต์œ ํ•˜๊ณ ์ž ํ•œ๋‹ค.

 

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

 

Daum ์šฐํŽธ๋ฒˆํ˜ธ ์„œ๋น„์Šค

์šฐํŽธ๋ฒˆํ˜ธ ๊ฒ€์ƒ‰๊ณผ ๋„๋กœ๋ช… ์ฃผ์†Œ ์ž…๋ ฅ ๊ธฐ๋Šฅ์„ ๋„ˆ๋ฌด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•. Daum ์šฐํŽธ๋ฒˆํ˜ธ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•ด๋ณด์„ธ์š”. ์–ด๋Š ์‚ฌ์ดํŠธ์—์„œ๋‚˜ ๋ฌด๋ฃŒ๋กœ ์ œ์•ฝ์—†์ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ต๋‹ˆ๋‹ค.

postcode.map.daum.net

 

์˜ˆ์ œ ์ฝ”๋“œ๋„ ์นœ์ ˆํ•˜๊ฒŒ ์„ค๋ช…๋˜์–ด ์žˆ๊ณ  ์‰ฝ๊ฒŒ ๋”ฐ๋ผํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜์ง€๋งŒ,

์ฒ˜์Œ์œผ๋กœ ์ ์šฉ์„ ํ•ด์•ผํ•˜๋Š” ์ž…์žฅ์—์„œ๋Š” ๋ณด๊ณ ๋„ ๋ง‰๋ง‰ํ•จ์ด ๋” ํด ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.(๋ณธ์ธ๋„ ๊ทธ๋žฌ๊ธฐ ๋•Œ๋ฌธ)

 

์‚ฌ์‹ค js๋กœ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์•ž์„œ ๋งํ–ˆ๋“ฏ์ด ๋”ฐ๋ผํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ํฌ๊ฒŒ ์–ด๋ ค์›€์ด ์—†๋‹ค. ํ•˜์ง€๋งŒ Vue๋ฅผ ์ ์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•ฝ๊ฐ„์˜ ๋ณ€๊ฒฝ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ๊ฒฝํ—˜์„ ๊ณต์œ ํ•˜๊ฒ ๋‹ค.

 

๋ฐ˜์‘ํ˜•

Daum postcode API ํ™œ์šฉ

<์‚ฌ์šฉ์ „>

<script src="//t1.daumcdn.net/mapjsapi/bundle/postcode/prod/postcode.v2.js"></script>

public ํŒจํ‚ค์ง€์— index.htmlํŒŒ์ผ์— ์ถ”๊ฐ€

 

<ํ…œํ”Œ๋ฆฟ>

<input type="text" v-model="postcode" placeholder="์šฐํŽธ๋ฒˆํ˜ธ">
<input type="button" @click="execDaumPostcode()" value="์šฐํŽธ๋ฒˆํ˜ธ ์ฐพ๊ธฐ"><br>
<input type="text" id="address" placeholder="์ฃผ์†Œ"><br>
<input type="text" id="detailAddress" placeholder="์ƒ์„ธ์ฃผ์†Œ">
<input type="text" id="extraAddress" placeholder="์ฐธ๊ณ ํ•ญ๋ชฉ">

์ฃผ์†Œ์ฐฝ์„ ํ†ตํ•ด ์ž…๋ ฅ ๋ฐ›์„ ์šฐํŽธ๋ฒˆํ˜ธ, ์ฃผ์†Œ, ์ฐธ๊ณ ํ•ญ๋ชฉ ๋“ฑ์„ v-model, ์ฃผ์†Œ์ฐฝ์„ ๋„์šธ click ์ด๋ฒคํŠธ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค.

 

<์Šคํฌ๋ฆฝํŠธ>

export default {
  data() {
    return {
      postcode: "",
      address: "",
      extraAddress: "",
    };
  },
  methods: {
    execDaumPostcode() {
      new window.daum.Postcode({
        oncomplete: (data) => {
          if (this.extraAddress !== "") {
            this.extraAddress = "";
          }
          if (data.userSelectedType === "R") {
            // ์‚ฌ์šฉ์ž๊ฐ€ ๋„๋กœ๋ช… ์ฃผ์†Œ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ
            this.address = data.roadAddress;
          } else {
            // ์‚ฌ์šฉ์ž๊ฐ€ ์ง€๋ฒˆ ์ฃผ์†Œ๋ฅผ ์„ ํƒํ–ˆ์„ ๊ฒฝ์šฐ(J)
            this.address = data.jibunAddress;
          }

          // ์‚ฌ์šฉ์ž๊ฐ€ ์„ ํƒํ•œ ์ฃผ์†Œ๊ฐ€ ๋„๋กœ๋ช… ํƒ€์ž…์ผ๋•Œ ์ฐธ๊ณ ํ•ญ๋ชฉ์„ ์กฐํ•ฉํ•œ๋‹ค.
          if (data.userSelectedType === "R") {
            // ๋ฒ•์ •๋™๋ช…์ด ์žˆ์„ ๊ฒฝ์šฐ ์ถ”๊ฐ€ํ•œ๋‹ค. (๋ฒ•์ •๋ฆฌ๋Š” ์ œ์™ธ)
            // ๋ฒ•์ •๋™์˜ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๊ฐ€ "๋™/๋กœ/๊ฐ€"๋กœ ๋๋‚œ๋‹ค.
            if (data.bname !== "" && /[๋™|๋กœ|๊ฐ€]$/g.test(data.bname)) {
              this.extraAddress += data.bname;
            }
            // ๊ฑด๋ฌผ๋ช…์ด ์žˆ๊ณ , ๊ณต๋™์ฃผํƒ์ผ ๊ฒฝ์šฐ ์ถ”๊ฐ€ํ•œ๋‹ค.
            if (data.buildingName !== "" && data.apartment === "Y") {
              this.extraAddress +=
                this.extraAddress !== ""
                  ? `, ${data.buildingName}`
                  : data.buildingName;
            }
            // ํ‘œ์‹œํ•  ์ฐธ๊ณ ํ•ญ๋ชฉ์ด ์žˆ์„ ๊ฒฝ์šฐ, ๊ด„ํ˜ธ๊นŒ์ง€ ์ถ”๊ฐ€ํ•œ ์ตœ์ข… ๋ฌธ์ž์—ด์„ ๋งŒ๋“ ๋‹ค.
            if (this.extraAddress !== "") {
              this.extraAddress = `(${this.extraAddress})`;
            }
          } else {
            this.extraAddress = "";
          }
          // ์šฐํŽธ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
          this.postcode = data.zonecode;
        },
      }).open();
    },
  },
};

๊ธฐ์กด ์ฝ”๋“œ๋ฅผ Vue ์ฝ”๋“œ์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•˜์˜€๋‹ค.


์ฃผ์†Œ ๊ฒ€์ƒ‰๊ธฐ๋Šฅ์‚ฌ์šฉ์€ ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ๊ธฐํšŒ์—๋Š” ์žฌ์‚ฌ์šฉ์„ ์œ„ํ•œ ๋ชจ๋“ˆํ™”๋ฅผ ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ค€๋น„ํ•ด ๋ณผ๊นŒ ํ•œ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•