A Vue.js (2.x) autosuggest component for the Google Maps Places API.
Live demo: olefirenko.github.io/vue-google-autocomplete
I have tried to use different Vue Google Autocomplete components, but did not find any that would solve my needs. So below I would like to mention what you can get from this exact component:
- Load more than one autocompletion inputs (I could not achieve this with existing vue components)
- Getting geolocation data (latitude, longitude) for found address object along with other address data (country, city, state, county, street, house number, zip code). So no need to do additional geocode request on backend side.
- No external dependencies
- You can get access to underlying PlaceResult object to get more details about found location.
- You can limit results to specific country or use users geolocation data
This component uses Google Maps Places API to get geo suggests for autocompletion, so you have to include the Google Maps Places API in the <head>
of your HTML:
<!DOCTYPE html>
<html>
<head>
…
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_HERE&libraries=places"></script>
</head>
<body>
…
</body>
</html>
To obtain API key please visit the Google Developer Console. The API's that you have to enable in your Google API Manager Dashboard are Google Maps Geocoding API, Google Places API Web Service and Google Maps Javascript API.
The easiest way to use Vue Google Autocomplete is to install it from npm or yarn.
npm install vue-google-autocomplete --save
Or
yarn add vue-google-autocomplete
The Vue Google Autocomplete works out of the box by just including it.
import VueGoogleAutocomplete from 'vue-google-autocomplete'
In your template you can use this syntax:
<vue-google-autocomplete
id="map"
classname="form-control"
placeholder="Start typing"
v-on:placechanged="getAddressData"
>
</vue-google-autocomplete>
Type: String
required
ID for the input container.
Type: String
Class to the input container.
Type: String
Default: Start typing
The input field will get this placeholder text.
Type: String
Default: address
Types supported in place autocomplete requests. More info
You may find this example helpful.
Type: String
|Array
Default: null
Option to restrict the autocomplete search to a particular country. Countries must be passed as a two-character, ISO 3166-1 Alpha-2 compatible country code (i.e. "br", "sg", "fr"). You can provide a single one, or an array of up to 5 country code strings.
Note: this is a dynamic property. You must pass it as :country=""
to your component, otherwise it won't work. For example:
<vue-google-autocomplete :country="['au', 'nz']"></vue-google-autocomplete>
will restrict the countries to Australia and New Zealand.
Type: Boolean
Default: false
Bias the search towards user current location.
Type: Object
Default: {}
Allow to configure Options for navigator.getCurrentPosition
The component emits next events, which you can listen in your application:
Gets triggered when the address data got obtained. This data is available on the returned objects:
street_number
,route
,locality
,administrative_area_level_1
,country
,postal_code
,latitude
,longitude
.place
- PlaceResult object is available as second parameter.id
a String representing the ID of the autocomplete that triggered the event.
Gets triggered when a user entered the name of a Place that was not suggested and pressed the Enter key, or the Place Details request failed.
object
an object with a key name representing the user's input.
Gets triggered when the autocomplete input field receives focus.
Gets triggered when the autocomplete input field loses focus.
Gets triggered every time autocomplete input got changed
Gets triggered when the autocomplete results got changed
Gets triggered when a key gets pressed
Gets triggered when an error is encountered
These functions are accessible by setting "ref" on the component (Refs documentation). See example below how to use these functions.
Call to clear the value of the user input.
Call focus to focus on the element
Call blur to blur (unfocus) the element
Call to update the user input with a new value
updateCoordinates(latlng)
Call to force coordinates and update the input accordingly
Call to retrieve current position from navigator
and update the input accordingly
Please note that you need to provide what method will listen (v-on:placechanged
) to an event when the address data is obtained.
<template>
<div>
<h2>Your Address</h2>
<vue-google-autocomplete
ref="address"
id="map"
classname="form-control"
placeholder="Please type your address"
v-on:placechanged="getAddressData"
country="sg"
>
</vue-google-autocomplete>
</div>
</template>
<script>
import VueGoogleAutocomplete from 'vue-google-autocomplete'
export default {
components: { VueGoogleAutocomplete },
data: function () {
return {
address: ''
}
},
mounted() {
// To demonstrate functionality of exposed component functions
// Here we make focus on the user input
this.$refs.address.focus();
},
methods: {
/**
* When the location found
* @param {Object} addressData Data of the found location
* @param {Object} placeResultData PlaceResult object
* @param {String} id Input container ID
*/
getAddressData: function (addressData, placeResultData, id) {
this.address = addressData;
}
}
}
</script>
The example below shows the correct usage of the types
parameter, when limiting the search to cities:
<vue-google-autocomplete
id="map2"
ref="toAddress"
classname="form-control"
placeholder="Start typing"
v-on:placechanged="getToData"
types="(cities)"
country="us"
>
</vue-google-autocomplete>
Sometimes you want to use the short_name
instead of the long_name
or vice-versa.
For example in the case of country
it is helpful to get the iso code (short_name
) to store it in your database.
You can do so by passing an object to the component.
Note: At this moment it is only possible to pass a complete object to the component. So it's not possible to override just specific address-components.
<vue-google-autocomplete
id="map"
ref="address"
classname="input"
placeholder="Start typing"
v-on:placechanged="getAddressData"
v-on:error="handleError"
country="sg"
v-bind:address-components="{
subpremise: 'short_name',
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
administrative_area_level_2: 'long_name',
country: 'short_name',
postal_code: 'short_name',
}"
>
</vue-google-autocomplete>