A Vuetify ready Vue.js (2.x) autosuggest component for the Google Maps Places API.
Latest Stable: 1.1.0
Latest Dev Stable: 2.0.0-beta.6
See releases for details.
Huge thanks and credit goes to @olefirenko and contributors for creating Vue Google Autocomplete from which this Vuetify ready version was inspired.
Live Interactive demo: madimetjashika.github.io/vuetify-google-autocomplete
The easiest way to use Vuetify Google Autocomplete is to install it from npm or yarn.
npm i vuetify-google-autocomplete
Or
yarn add vuetify-google-autocomplete
Within your main.js or Vue entry point, import and initialise the component.
import Vue from 'vue';
import VuetifyGoogleAutocomplete from 'vuetify-google-autocomplete';
Vue.use(VuetifyGoogleAutocomplete, {
apiKey: '...', // Can also be an object. E.g, for Google Maps Premium API, pass `{ client: <YOUR-CLIENT-ID> }`
version: '...', // Optional
language: '...', // Optional
});
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.
Simply start using the component in your HTML.
<vuetify-google-autocomplete
id="map"
append-icon="search"
disabled="true"
placeholder="Start typing"
v-on:placechanged="getAddressData"
>
</vuetify-google-autocomplete>
The Vuetify Google Autocomplete works out of the box by just including it.
import VuetifyGoogleAutocomplete from 'vuetify-google-autocomplete'
In your template you can use this syntax:
<vuetify-google-autocomplete
id="map"
append-icon="search"
disabled="true"
classname="form-control"
placeholder="Start typing"
v-on:placechanged="getAddressData"
>
</vuetify-google-autocomplete>
Please refer to the Props API or JSDOCS.md file for a complete "living" definition of all props supported by this library.
NB: This componenent implements the same props as those specified for Vuetify Text Fields as at v1.0.3. Please see Vuetify Text Fields for a complete list of available props.
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.
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.
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
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
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>
<vuetify-google-autocomplete
ref="address"
id="map"
classname="form-control"
placeholder="Please type your address"
v-on:placechanged="getAddressData"
country="sg"
>
</vuetify-google-autocomplete>
</div>
</template>
<script>
export default {
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>
Lets make this an awesome vuetify component! Collaborations and contributions are welcome!
Have a read through the Contributor Code of Conduct. Pretty standard, nothing hectic.
Fork, then clone the repo:
git clone [email protected]:your-username/vuetify-google-autocomplete.git
Install dependencies with npm
npm install
or yarn
yarn
Make your changes, and observe them at dev-time by running
npm run dev
and going to the displayed URL to see your changes.
Then, ensure tests are written for your changes. Ensure that your changes pass all the tests:
npm run test
Ensure that your changes are documented via JSDocs standard, then run
npm run docs
to update the JSDocs.
If relevant, please ensure that you update the README and demo/example accordingly.
Push to your fork and submit a pull request.
If all is well, your changes will be merged timeously!
Feel free to also post issues for bug fixes or enhancements or anything.