๐Ÿ˜Ž Google Maps Autocomplete API ๊ตฌํ˜„ | Vue.js

4461 ๋‹จ์–ด vuejavascript
์ด ํŠœํ† ๋ฆฌ์–ผ์—์„œ๋Š” vue.js์—์„œ Google Maps API๋กœ Places Autocomplete Service๋ฅผ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

NOTE: the picture is fancy because I use VUETIFY. This tutorial just explains how Google Places API work, with no CSS! ๐Ÿ˜





All we need is a vue component and a plugin called vue-meta (I already use it for SEO)



๋จผ์ € Places.vue๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.




// Places.vue

<template>
  <div>
    <input type="text" v-model="location">
    <ul>
      <li v-for="(result, i) in searchResults" :key="i">
        {{ result }} // list of all places
      </li>
    </ul>
  </div>
</template>


์ด์ œ ๋™์ผํ•œ ํŒŒ์ผ์— script ํƒœ๊ทธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

// Places.vue

<script>
  export default {
    data: () => ({
      location: '',
      searchResults: [],
      service: null // will reveal this later ๐Ÿ•ต๏ธ
    })
  }
</script>



์„ค์น˜vue-meta ํ”Œ๋Ÿฌ๊ทธ์ธ โ€” ์„ค๋ช…์„œ๊ฐ€ ๋งค์šฐ ์‰ฝ๊ณ  ๋‹ค๋ฅธ vue ํ”Œ๋Ÿฌ๊ทธ์ธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ”Œ๋Ÿฌ๊ทธ์ธ์ด ์„ค์น˜๋ฉ๋‹ˆ๋‹ค.

Places.vue์— metaInfo hook ๋„ฃ๊ธฐ -

// Places.vue

<script>
  export default {
    data, // already wrote above
    metaInfo () {
      return {
        script: [{
          src: `https://maps.googleapis.com/maps/api/js?key=<YOUR_API_KEY>&libraries=places`,
          async: true,
          defer: true,
          callback: () => this.MapsInit() // will declare it in methods
        }]
      }
    }
  }
</script>



์—ฌ๊ธฐ์„œ ๋‚ด๊ฐ€ metaInfo๋ฅผ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ์šฐ๋ฆฌ๊ฐ€ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

  • ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋ Œ๋”๋ง๋  ๋•Œ๋งŒ ์ด๋™ ์ค‘์— ์™ธ๋ถ€ JS ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.
  • JS ํŒŒ์ผ์ด ๋‹ค์šด๋กœ๋“œ๋  ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค! โ€” ๐Ÿ˜Ž

  • ์ด์ œ ๊ณ„์† ์ง„ํ–‰ํ•˜์—ฌ ๊ฐ€์žฅ ๊ธฐ๋‹ค๋ ค์˜จmethods ํ›„ํฌ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    // Places.vue
    
    <script>
      export default {
        data // defined already,
        metaInfo // defined already,
    
        methods: {
          MapsInit () {
            this.service = new window.google.maps.places.AutocompleteService()
          },
          displaySuggestions (predictions, status) {
            if (status !== window.google.maps.places.PlacesServiceStatus.OK) {
              this.searchResults = []
              return
            }
            this.searchResults = predictions.map(prediction => prediction.description) 
          }
        }
    
      }
    </script>
    
    



    MapsInit ๋ฐ displaySuggestions ํ•จ์ˆ˜๊ฐ€ ์ˆ˜ํ–‰ํ•˜๋Š” ์ž‘์—…์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    MapInit()โ€Š-โ€Šthe function that is called when the JS file is loaded.



    ์—ฌ๊ธฐ์—์„œ ์šฐ๋ฆฌ๋Š” - AutocompleteService๋ผ๋Š” Google ์žฅ์†Œ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค(์ง€๊ธˆ ๊ท€์ฐฎ๊ฒŒ ํ•˜์ง€ ๋งˆ์„ธ์š”! ๊ทธ๊ฒƒ ์—†์ด ์‚ด ์ˆ˜ ์—†๋‹ค๋ฉด google ์„ค๋ช…์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š” ๐Ÿคท).

    ๋‚˜์ค‘์— ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ด AutocompleteService()๋ฅผ ๋ฐ์ดํ„ฐ ์†์„ฑ 'service'์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.

    displaySuggestions(predictions, status)โ€Š-โ€Šexplained a little later.



    ๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ์ด ํผ์ฆ์˜ ๋งˆ์ง€๋ง‰ ์กฐ๊ฐ์ž…๋‹ˆ๋‹ค. watcher ์†์„ฑ์˜ location
    // Places.vue
    
    <script>
      export default {
        data // already defined,
        methods // already defined,
        metaInfo // already defined,
        // the magical part
        watch: {
          location (newValue) {
            if (newValue) {
              this.service.getPlacePredictions({
                input: this.location,
                types: ['(cities)']
              }, this.displaySuggestions)
            }
          }
        }
      }
    </script>
    


    ๋”ฐ๋ผ์„œ ์ž…๋ ฅ ํ•„๋“œ์— ๋ฌด์—‡์„ ์ž…๋ ฅํ•˜๋“  ์œ„์น˜ ์†์„ฑ์ด ๋ณ€๊ฒฝ๋˜๊ณ  ์œ„์น˜ ์†์„ฑ์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์„œ๋น„์Šค ์†์„ฑ์— ์—ฐ๊ฒฐ๋œ getPlacePredictions ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

    getPlacePredictions๋Š” ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค.
  • ๋งŽ์€ ํ•ญ๋ชฉ์ด ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์—์„œ๋Š” ์ž…๋ ฅ๊ณผ ์œ ํ˜•๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ์ฒด์ž…๋‹ˆ๋‹ค.
    ใ…. ์ž…๋ ฅ - ์˜ˆ์ธกํ•  ์ฟผ๋ฆฌ(์ด ๊ฒฝ์šฐ this.locations).
    ๋น„. ์œ ํ˜• - ๊ฒฐ๊ณผ ์œ ํ˜•.
  • ๋ฉ”์„œ๋“œ ํ›„ํฌ์—์„œ ์œ„์—์„œ ์„ ์–ธํ•œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” - displaySuggestions์ž…๋‹ˆ๋‹ค.

  • ๋ชจ๋‘ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค!

    ์žฌ๋ฏธ์žˆ๊ฒŒ ์ฝ์œผ์‹œ๊ณ  ์กฐ๊ธˆ์ด๋‚˜๋งˆ ๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด
    Consider Buying me a coffee?

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ