Google Places API Add Autocomplete To An Input Field
Tags: vuejs, beginners, tutorial, programming
Published: 2022-04-11 13:03:24

---
canonical_url: https://softauthor.com/google-places-autocomplete-api/
---

{% youtube M-ikgaToVfE %}

By the end of this tutorial, you’ll be able to:

- [Know what Autocomplete is](https://softauthor.com/google-places-autocomplete-api/#what-is-autocomplete-api)
- [Instantiate Autocomplete](https://softauthor.com/google-places-autocomplete-api/#instantiate-autocomplete)
- [Restrict Suggested Addresses to a Specific Region](https://softauthor.com/google-places-autocomplete-api/#restrict-address-to-a-specific-region)
- [Change the Drop-down List Style](https://softauthor.com/google-places-autocomplete-api/#change-dropdown-list-style)

###What is Autocomplete?

Autocomplete is a part of Google’s Places Library in the Google Map Javascript API.

We are going to discuss how to use autocomplete API to show suggested addresses in a dropdown list when users start typing their addresses in the input field.

This is very useful when a user denies sharing his or her location accidentally or when the [HTML Geolocation API](https://softauthor.com/get-user-current-location-html5-geolocation-api-javascript/#get-latitude-longitude-using-geolocation-api) does not support their browser. We can easily get this functionality working using autocomplete.

The first thing we need to do is include Google Places Library inside the index.html file before the ending body tag.

```HTML
<script src="https://maps.googleapis.com/maps/api/js?libraries=places&key=[YOUR_API_KEY]"></script>
```

>⚠️ Make sure to add your API Key in the URL
>[Google Maps API → Obtain An API Key](https://softauthor.com/google-maps-api-key/)

Make sure that Maps JavaScript API and Places API libraries are enabled in the [Google Cloud Console](https://accounts.google.com/signin/v2/identifier?service=cloudconsole&passive=1209600&osid=1&continue=https%3A%2F%2Fconsole.cloud.google.com%2F&followup=https%3A%2F%2Fconsole.cloud.google.com%2F&flowName=GlifWebSignIn&flowEntry=ServiceLogin).

As you can see, the URL is generic and I’ve added two query parameters at the end.

- key: Your API Key from Google Cloud Platform. Find how to get it here.

- libraries:  The value of this will be the library name that we want to use, in this case places.

###Instantiate Autocomplete

The next thing we want to do is instantiate the autocomplete object.

```JavaScript
new google.maps.places.Autocomplete();
```

It will take a few parameters.

The main one is the input DOM element that we want to add the auto complete functionality into.

To do that, set a **id** attribute called **autocomplete** to the input field:

```HTML
<input
    type="text"
    placeholder="Enter your address"
    id="autocomplete"
/>
```
Then inside the parenthesis, add the input DOM element as a first argument.

```JavaScript
  new google.maps.places.Autocomplete(
      document.getElementById("autocomplete");
  );

```

![alt text](https://lh6.googleusercontent.com/frmNRe_BufnORVk-A8iNrw1I_t3oVuG6rXXMuVNgxLclHq3cOg7alvoLZ5S5zibM42y_qQ2AG2hwYCl-xV-ujT6blAvviOGyexsj7BnUcrnmxXrmsKGqzSh2vKPzt_F8rlsLvllk)

>🔥 Recommended
>[Vue.js + Google Maps API for Beginners Video Course](https://www.udemy.com/course/vuejs-google-maps-api-for-beginners/?referralCode=6F952330C78C8A6F8CDD&awc=6554_1649512173_1cf455cb7d1eeb4ec60aef39ab4e3246&utm_source=Growth-Affiliate&utm_medium=Affiliate-Window&utm_campaign=Campaign-Name&utm_term=1001765&utm_content=Placement)

###Restrict Suggested Addresses to a Specific Region

Let’s see how we can show addresses from a specific region close to a user’s location so that it’s easy to find his or her address with minimal typing.

For example, I live in Canada and when I type 1 2 3 the suggested addresses are from the U.S. and Taiwan.

![alt text](https://lh4.googleusercontent.com/lj-p6ITuFb3aG_GmVP3D1a8QgSPTG9Xu9uEpn6sj8XLvtOcXXribnNX1lgYSZT1rJgkM_Sbadi47KySUxGmEr_2adhbbLKBeIy-vzPzn3_4KVTdqgf5BUBw_yapz735iDmRxJEiW)

I can still get my address but it takes longer.

Let’s say I want to show Ottawa street addresses first before showing suggestions from other cities or countries.

To do that, we need to have the geographic coordinates of the city, in this case, Ottawa. You can find that information by doing a simple search like Ottawa Ontario Geographic Coordinates… and there it is.

![alt text](https://lh4.googleusercontent.com/Jscc-JQJa6ac6zBXiDsEykS2gjaiatVbWVTr69GxGNL7_ZVenYTuMkOD-_JPQ0fS8iGSGrDw9fP-6gfEmyAWvSFywLcpU1C2IX3OAYrY-G90KlJXVeqaqLx4zFz_JH7UbA0_HNHG)

This is actually DD coordinates (decimal degree). What we want are simple standard coordinates.

Click the first link, and the simple standard is the first option.

![alt text](https://lh5.googleusercontent.com/Ontq4s70O7CdvZ5sPoZcvb1RXlUkQUshSgPFeDKjS8MUQyXEF5-O-YUqaTXQYHBsDhCkbc1Ohj0VvmVLLpaCF8KDN0M8ezlyk-hwQbtXHJSUMu3PL0Z8qR5te81rL8-F9F09F9je)

Now that we have the coordinates, to add them into the Autocomplete object we need to pass a Javascript object as a second argument.

After the comma, create a Javascript object and define a property called **bounds** inside it. Set its value to a **LatLng** object passing Ottawa coordinate values as arguments to it.

```JavaScript
new google.maps.places.Autocomplete(
   document.getElementById("autocomplete"), {
      bounds: new google.maps.LatLngBounds(45.4215296, -75.6971931)
   }
)
```

As you can see, it shows all of the Ottawa addresses first followed by other matching addresses.

![alt text](https://lh5.googleusercontent.com/l9Ni2joU9Xz92DhubEAcTHrZbRQjW1WBCpAvyzr2wY0YUq-WJikUZfWUI9rc9laEAtZwhT-GvjZX3kOfs8pr_W0UGp75I4q5waesTNs2bddCFAQdUsShVaBd9WSNrLMdBehzctGX)

###Change Drop-down List Style

Let’s change some of the style of the dropdown list to match our theme.

This image shows what CSS classes we need in order to change the list style.

![alt text](https://lh5.googleusercontent.com/zbIAMjoQMeijmxhgzun3jokGKjljypXnEJ9DkfTNgj_TXMmk0cFLOwXtkQMGAT1GLqEUvtGWT_SGL9Fg31CI0qiF3zOySxGizbfMwuxE03CNJduYOPXUC9jZZPLjEd3nuq27Gcqe)

First, get rid of the icon on the left side of each address which is the **.pac-icon** class.

Inside the .pac-icon class, add **display: none;** to hide that.

```HTML
<style>
.pac-icon {
  display: none;
}
</style>
```

![alt text](https://lh5.googleusercontent.com/lCdl8FqaeCydQ2oFR8XSD5Cz_GdGoQe9Cs9BUHKz_lXRLnC1zgWSGHFyT9tr4pVM75W5ca735OJ1sSYEZWFkS1WE_GO1YOGTfs0Lif-3FTtuNcxZpbURGQwhKEZN5AxvJwr-JSkb)

Let’s increase the padding for each item. The target CSS class for that would be pac-item. Give padding of 10 pixels to all sides.

Then, increase the font size of the text to 16 pixels and let’s change the cursor to a pointer (which is the hand symbol).

Finally, give a hover state for the pac item class with a grey background color.

```JavaScript
.pac-item {
  padding: 10px;
  font-size: 16px;
  cursor: pointer;
}

.pac-item:hover {
  background-color: #ececec;
}
```

Let’s look at the changes we made.

![alt text](https://lh3.googleusercontent.com/U3_vXxBo-1EkvifHwnLobEcc1iwb4PsQTjgW6T2_q78L4brvKy59XMvVX2Hd7CfVenSaGBdoHCGlL4yEYlQGT-u-fclIaZF2m2csj7LA1lFksHCmPVsalqLAdPy-fMASK5ye7wLM)

That looks better.

As you can see, the city, province and the country has a larger font size as expected, but the actual street name did not change.

To change the street name font size we need to use another CSS class named pac-item-query.

```JavaScript
.pac-item-query {
  font-size: 16px;
}
```

Let’s try it one more time and it looks great!

![alt text](https://lh5.googleusercontent.com/YQBFFtbUP72RAxUCzBR8v2_MiYji8Sba_RDPKHJwZ1-3bi9PcNzhfAQD3KMWt8pIkrpC5nG2UwKTZAbqpG-JZUM4jAIK1W0sPKzpQHdamCZEfhsOLLZrIuOWkr1X434IEmd9aCBT)

If you want to know more about Google Maps API, check out my course [Vue.js + Google Maps API for Beginner](https://www.udemy.com/course/vuejs-google-maps-api-for-beginners/?referralCode=6F952330C78C8A6F8CDD&awc=6554_1649513359_7f62d4ac7053c3fd5396740daa8e3a46&utm_source=Growth-Affiliate&utm_medium=Affiliate-Window&utm_campaign=Campaign-Name&utm_term=1001765&utm_content=Placement).

This will teach you everything you need to know to build location-based apps like a PRO!