<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Siddharth Arora</title>
    <description>The latest articles on DEV Community by Siddharth Arora (@arora).</description>
    <link>https://dev.to/arora</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F399181%2Feb54200b-d41d-4cfd-bc54-2b7ed63b6b89.png</url>
      <title>DEV Community: Siddharth Arora</title>
      <link>https://dev.to/arora</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arora"/>
    <language>en</language>
    <item>
      <title>😎 Implement Google Maps Autocomplete API | Vue.js</title>
      <dc:creator>Siddharth Arora</dc:creator>
      <pubDate>Thu, 29 Oct 2020 10:22:27 +0000</pubDate>
      <link>https://dev.to/arora/places-autocomplete-service-by-google-maps-api-vue-js-js-client-side-d29</link>
      <guid>https://dev.to/arora/places-autocomplete-service-by-google-maps-api-vue-js-js-client-side-d29</guid>
      <description>&lt;p&gt;This tutorial explains how you can easily implement a &lt;strong&gt;Places Autocomplete Service&lt;/strong&gt; by &lt;strong&gt;Google Maps API&lt;/strong&gt; in &lt;code&gt;vue.js&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: the picture is fancy because I use VUETIFY. This tutorial just explains how Google Places API work, with no CSS! 😐&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7ajjfzv0gk7ir8qoc3md.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7ajjfzv0gk7ir8qoc3md.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=HXEC0jy9S2I" rel="noopener noreferrer"&gt;Checkout this video for a demo&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All we need is a vue component and a plugin called vue-meta (I already use it for SEO)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  First let’s create a file called &lt;code&gt;Places.vue&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Places.vue

&amp;lt;template&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;input type="text" v-model="location"&amp;gt;
    &amp;lt;ul&amp;gt;
      &amp;lt;li v-for="(result, i) in searchResults" :key="i"&amp;gt;
        {{ result }} // list of all places
      &amp;lt;/li&amp;gt;
    &amp;lt;/ul&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add a &lt;code&gt;script&lt;/code&gt; tag in the same file and add the following -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Places.vue

&amp;lt;script&amp;gt;
  export default {
    data: () =&amp;gt; ({
      location: '',
      searchResults: [],
      service: null // will reveal this later 🕵️
    })
  }
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install &lt;a href="https://www.npmjs.com/package/vue-meta" rel="noopener noreferrer"&gt;vue-meta&lt;/a&gt; plugin — the documentation is very easy and the plugin is installed like any other vue plugin.&lt;/p&gt;

&lt;p&gt;Putting the &lt;code&gt;metaInfo hook&lt;/code&gt; in Places.vue -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Places.vue

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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the reason I used metaInfo here is because we can -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Download external JS&lt;/strong&gt; files on the go, only when the component is rendered.&lt;/li&gt;
&lt;li&gt;It gives us the power of the &lt;strong&gt;callback function&lt;/strong&gt;, called when the JS file is downloaded! — 😎&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now let’s move ahead and create the most awaited &lt;code&gt;methods&lt;/code&gt; hook -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Places.vue

&amp;lt;script&amp;gt;
  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 =&amp;gt; prediction.description) 
      }
    }

  }
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Let's see what these functions &lt;strong&gt;MapsInit&lt;/strong&gt; and &lt;strong&gt;displaySuggestions&lt;/strong&gt; do.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;MapInit()&lt;/em&gt;&lt;/strong&gt; - the function that is called when the JS file is loaded.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this we use a google places service called - &lt;em&gt;AutocompleteService (Don't bother right now! Check google documentation if you can't live without it 🤷).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;we assign this AutocompleteService() to our data property &lt;strong&gt;'service'&lt;/strong&gt; so that we can use it again later.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;displaySuggestions(predictions, status)&lt;/em&gt;&lt;/strong&gt; - explained a little later.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this is the last piece of the puzzle. The &lt;code&gt;watcher&lt;/code&gt; on the &lt;code&gt;location&lt;/code&gt; property&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Places.vue

&amp;lt;script&amp;gt;
  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)
        }
      }
    }
  }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, whatever you type in the input field changes the &lt;strong&gt;location&lt;/strong&gt; property, and whenever the location property is changed, a &lt;strong&gt;getPlacePredictions&lt;/strong&gt; function is called that is attached to the &lt;strong&gt;service&lt;/strong&gt; property.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;getPlacePredictions&lt;/strong&gt; receives two parameters-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;An Object that has many things but we only use &lt;strong&gt;input&lt;/strong&gt; and &lt;strong&gt;types&lt;/strong&gt; here-&lt;br&gt;
a. &lt;strong&gt;input&lt;/strong&gt; - the query to be predicted (&lt;code&gt;this.locations&lt;/code&gt; in our case).&lt;br&gt;
b. &lt;strong&gt;types&lt;/strong&gt; - the type of result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the callback function we declared above in methods hook which is - &lt;strong&gt;displaySuggestions&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All done!&lt;/p&gt;

&lt;p&gt;If you enjoyed reading this and found it a little helpful,&lt;br&gt;
&lt;a href="https://www.buymeacoffee.com/arora" rel="noopener noreferrer"&gt;&lt;strong&gt;Consider Buying me a coffee?&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
