React Places Autocomplete

7.3.0 · maintenance · verified Sun Apr 19

react-places-autocomplete is a React component designed to integrate Google Maps Places Autocomplete functionality into web applications, allowing users to easily select addresses. The current stable version is 7.3.0, released in January 2024. The library offers a highly customizable user interface, utility functions for geocoding addresses, and retrieving latitude and longitude coordinates using the Google Maps Geocoding API. Key differentiators include full control over rendering, mobile-friendly user experience, WAI-ARIA compliance, and support for asynchronous loading of the Google Maps JavaScript API. The project is actively seeking new maintainers, which may impact its long-term development cadence and responsiveness to new features or complex issues. Despite recent minor updates, significant feature development might be slow without additional community contributions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic React class component that integrates `react-places-autocomplete`. It shows how to initialize the component, handle input changes, and process selected addresses to retrieve their latitude and longitude using the provided utility functions. It assumes the Google Maps JavaScript API is loaded externally.

import React from 'react';
import PlacesAutocomplete, {
  geocodeByAddress,
  getLatLng,
} from 'react-places-autocomplete';

// Ensure you load Google Maps JavaScript API in your HTML:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places"></script>

class LocationSearchInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = { address: '' };
  }

  handleChange = address => {
    this.setState({ address });
  };

  handleSelect = address => {
    geocodeByAddress(address)
      .then(results => getLatLng(results[0]))
      .then(latLng => console.log('Success', latLng))
      .catch(error => console.error('Error during geocoding:', error));
  };

  render() {
    return (
      <PlacesAutocomplete
        value={this.state.address}
        onChange={this.handleChange}
        onSelect={this.handleSelect}
      >
        {({ getInputProps, suggestions, getSuggestionItemProps, loading }) => (
          <div>
            <input
              {...getInputProps({
                placeholder: 'Search Places ...',
                className: 'location-search-input',
              })}
            />
            <div className="autocomplete-dropdown-container">
              {loading && <div>Loading...</div>}
              {suggestions.map(suggestion => {
                const className = suggestion.active
                  ? 'suggestion-item--active'
                  : 'suggestion-item';
                const style = suggestion.active
                  ? { backgroundColor: '#fafafa', cursor: 'pointer' }
                  : { backgroundColor: '#ffffff', cursor: 'pointer' };
                return (
                  <div
                    {...getSuggestionItemProps(suggestion, { className, style })}
                    key={suggestion.placeId} // Key is important for React lists
                  >
                    <span>{suggestion.description}</span>
                  </div>
                );
              })}
            </div>
          </div>
        )}
      </PlacesAutocomplete>
    );
  }
}

export default LocationSearchInput;

view raw JSON →