vue-geo-suggest v1.0.2
vue-geo-suggest
A small, renderless Vue component for finding addresses using Google Places API. The component simply provides a list of suggestions and place information as slot props in order to support custom UI and reduce size (2K gzipped). It is easily pluggable to Vuetify and other UI components.
This project was originally based on react-geosuggest and vue-google-maps.
Installation
npm install vue-geo-suggest
yarn add vue-geo-suggestApart from that, an API key is necessary for using Google Places. From the Google Developer Console's API Manager Dashboard, enable the following APIs:
Generate an API key and provide it to loadGmaps utility.
The component and utilities can be imported directly:
import { GeoSuggest, loadGmaps } from 'vue-geo-suggest'
loadGmaps('my-api-key')
Vue.component(GeoSuggest.name, GeoSuggest) // Or register locally where neededOr used as a plugin:
import GeoSuggest from 'vue-geo-suggest'
Vue.use(GeoSuggest, { apiKey: 'my-api-key' })Usage
<GeoSuggest
:search="searchInput"
:suggestion="selectedSuggestion"
@geocoded="address = $event.normalizedAddress"
>
<template v-slot="{ suggestions, loading }">
<CustomSearchInput
v-model="searchInput"
/>
<CustomSuggestDropdown
v-model="selectedSuggestion"
:items="suggestions"
:loading="loading"
/>
</template>
</GeoSuggest>import { GeoSuggest, loadGmaps } from 'vue-geo-suggest'
export default {
components: { GeoSuggest },
data() {
return {
searchInput: '', // Search text
selectedSuggestion: null, // Selected suggest from dropdown
address: null, // Information about the selected place
}
},
mounted() {
// Load API dependencies globally. This can be called any time
// before using GeoSuggest component.
// i.e. in `main.js` or directly in the view where is necessary.
loadGmaps('my-api-key')
},
}Example with Vuetify:
<GeoSuggest
v-slot="{ suggestions, loading }"
:search="searchInput"
:suggestion="selectedSuggestion"
@geocoded="address = $event.normalizedAddress"
>
<VCombobox
v-model="selectedSuggestion"
:search-input.sync="searchInput"
:loading="loading"
:items="suggestions"
item-text="description"
no-filter
clearable
/>
</GeoSuggest>API
geo-suggest
props
searchString (optional)Search string to filter places. A list of place suggestions with basic details will be provided based on this text. Example: "400 Broadway".
min-lengthNumber (optional)default: 3Minimum length of the search string to trigger a request.
suggestionObject (optional)Selected suggestion among all the provided values to show extended details about the place. This prop must be one of the elements inside the provided
suggestionslist. Containsdescription,placeId, andmatchedSubstrings.debounceFunction (optional)Called whenever the
searchprop changes with another function as a single parameter that performs the actual request. Useful for debouncing requests with a custom query delay. Works directly withlodash.debounce::debounce="fn => lodashDebounce(fn, msDelay)"typesArray (optional)Filter suggestions by type. See types supported.
locationObject (optional)Allows localizing the resulting suggestions. See
google.maps.LatLng.radiusNumber (optional)Radius in meters that defines the valid area around the provided location. Must be used with
locationprop.boundsObject (optional)Bounds for biasing the suggestions.
locationandradiusare ignored when using this. SeeLatLngBounds.countryString|Array (optional)Restricts predictions to the specified countries (ISO 3166-1 Alpha-2 country code, case insensitive) Example:
'es';['it', 'fr'].place-detail-fieldsArray (optional)List of fields that should be returned by Google Places API. Useful to reduce the size of the response and optimize billing. All the fields are returned by default.
google-mapsObject|Function (optional)Google Maps object to use in case it is not loaded globally.
data
loadingtruewhen a request to Google Places API is pending. This is provided in the default scoped slot.suggestionsList of suggestions returned by Google Places API based on
search. Each element is an object containingdescription,placeIdandmatchedSubstrings. This is provided in the default scoped slot.
events
suggestionsFired when a new list of suggestions is returned by the Google Places API.
arguments:
suggestionsArray - List of suggestions.
errorFired when Google Places API fails.
arguments:
payload.statusObject - The status returned.
geocodedFired when the selected suggestion is geocoded and all its details are available.
arguments:
payload.descriptionString - Same description string as in thesuggestionslist.payload.locationObject - Latitude (lat) and longitude (lng).payload.gmapsObject - Complete response for this suggestion. See its structure here.payload.addressComponentsMapObject - Handy structure that summarizesgmapscomponents.payload.normalizedAddressObject - Extended information based on the API result useful for shipping addresses.
Project setup for contributing
yarn # Installs dependencies
yarn dev # Compiles and hot-reloads for development
yarn build # Compiles and minifies for production
yarn lint # Lints and fixes files
yarn test:unit # Run tests
yarn doc:build # Update the API section of README.md