usePlacesAutocomplete
This is a React hook of Google Maps Places Autocomplete, which helps you build an UI component with the feature of place autocomplete easily! By leverage the power of Google Maps Places API, you can provide a great UX (user experience) for user interacts with your search bar or form etc. Hope you guys it.
Features
- ? Provide intelligent places suggestions powered by Google Maps Places API.
- ? Build your own customized autocomplete UI by React hook.
- ? Utility functions to do geocoding and get geographic coordinates using Google Maps Geocoding API.
- ?️ Support asynchronous Google script loading.
- ? Support TypeScript type definition.
- ⌨️ Build an UX rich component (e.g. WAI-ARIA compliant and keyword support) via comprehensive demo code.
Requirement
To use use-places-autocomplete
, you must use react@16.8.0
or greater which includes hooks.
Installation
This package is distributed via npm.
Getting Started
usePlacesAutocomplete
is based on the Places Autocomplete (or more specific docs) of Google Maps Place API. If you are unfamiliar with these APIs, we recommend you reviewing them before we start.
Setup APIs
To use this hook, there're two things we need to do:
Load the library
Use the script
tag to load the library in your project.
We also support asynchronous script loading. By doing so you need to pass the initMap
as the callbackName option.
⚠️ If you got a global function not found error. Make sure
usePlaceAutocomplete
is declared before the script is loaded.
Create the component
Now we can start to build our component. Check the API out to learn more.
Easy right? This is the magic of the usePlacesAutocomplete
✨. I just show you how does it work via the minimal example. However there're more things you can do for an UX rich autocomplete component, like WAI-ARIA compliant and keyword support as my demo (check the code), a keyword clear button, search history etc.
? react-cool-onclickoutside is my other hook library, which can help you handle the interaction of user clicks outside of the component(s).
API
Parameter object (optional)
When use usePlacesAutocomplete
you can configure the following options via the parameter.
Key | Type (all optional) | Default | Description |
---|---|---|---|
requestOptions |
object | The request options of Google Maps Places API except for input (e.g. bounds, radius etc.). |
|
googleMaps |
object | window.google.maps |
In case you want to provide your own Google Maps object, pass it in as google.maps . |
callbackName |
string | You can provide a callback name to initialize usePlacesAutocomplete after Google script is loaded. It's useful when you load the script asynchronously. |
|
debounce |
number | 200 |
Number of milliseconds to delay before making a request to Google Maps Places API. |
Return object
It's returned with the following properties.
Key | Type | Default | Description |
---|---|---|---|
ready |
boolean | false |
The ready status of usePlacesAutocomplete . |
value |
string | '' |
value for the input element. |
suggestions |
object | { loading: false, status: '', data: [] } |
See suggestions. |
setValue |
function | (value, shouldFetchData = true) => {} |
See setValue. |
clearSuggestions |
function | See clearSuggestions. |
suggestions
The search result of Google Maps Places API, which contains the following properties:
loading: boolean
- indicates the status of a request is pending or has completed. It's useful for displaying a loading indicator for user.status: string
- indicates the status of API response, which has these values. It's useful to decide whether we should display the dropdown or not.data: array
- an array of suggestion objects each contains all the data.
setValue
Set the value
of the input element. Use case as below.
In addition, the setValue
method has an extra parameter, which can be used to disable hitting Google Maps Places API.
clearSuggestions
Calling the method will clear and reset all the properties of the suggestions
object to default. It's useful for dismissing the dropdown.
Utility Functions
We provide getGeocode and getLatLng utils for you to do geocoding and get geographic coordinates when needed.
getGeocode
It helps you convert address (e.g. "Section 5, Xinyi Road, Xinyi District, Taipei City, Taiwan") into geographic coordinates (e.g. latitude 25.033976 and longitude 121.5645389) by Google Maps Geocoding API.
getGeocode
is an asynchronous function with the following API:
parameter: object
- you must supply one, only one ofaddress
orplaceId
. It'll be passed as Geocoding Requests.results: array
- an array of objects each contains all the data.error: string
- the error status of API response, which has these values (except for "OK").
getLatLng
It helps you get the lat
and lng
from the result object of getGeocode
.
getLatLng
is an asynchronous function with the following API:
parameter: object
- the result object ofgetGeocode
.latLng: object
- contains the latitude and longitude properties.