πŸ“… You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP β†’
Socket
Sign inDemoInstall
Socket

google-places-autocomplete-react

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

google-places-autocomplete-react

A lightweight React component for Google Places autocomplete.

1.0.14
latest
Source
npm
Version published
Weekly downloads
45
104.55%
Maintainers
1
Weekly downloads
Β 
Created
Source

Google Places Autocomplete for React

NPM Version License

A lightweight and easy-to-use Google Places Autocomplete component for React applications. It allows users to search for locations and fetch place details using the Google Places API.

πŸš€ Features

  • βœ… Google Places API Integration – Fetch place suggestions in real-time.
  • βœ… Lightweight & Fast – Minimal dependencies and optimized performance.
  • βœ… Customizable – Fully controllable via props.
  • βœ… Easy to Integrate – Works out of the box with any React app.
  • βœ… TypeScript Support – Includes TypeScript types for better development experience.

πŸ“¦ Installation

Install the package using npm or yarn:

npm install google-places-autocomplete-react

OR

yarn add google-places-autocomplete-react

πŸ“– How to Use

1️⃣ Basic Usage

import React from "react";
import GooglePlacesAutocomplete from "google-places-autocomplete-react";

const App = () => {
    const handlePlaceSelect = (place) => {
        console.log("Selected Place:", place);
    };

    return (
        <div style={{ maxWidth: "400px", margin: "50px auto" }}>
            <GooglePlacesAutocomplete 
                apiKey="YOUR_GOOGLE_API_KEY"
                onPlaceSelect={handlePlaceSelect}
            />
        </div>
    );
};

export default App;

2️⃣ Fetching Place Details

The onPlaceSelect callback receives an object containing:

  • description - The name of the selected place.
  • lat - Latitude of the location.
  • lng - Longitude of the location.

Example:

const handlePlaceSelect = (place) => {
    console.log("Place Name:", place.description);
    console.log("Latitude:", place.lat);
    console.log("Longitude:", place.lng);
};

3️⃣ Customizing Placeholder Text

<GooglePlacesAutocomplete 
    apiKey="YOUR_GOOGLE_API_KEY"
    onPlaceSelect={handlePlaceSelect}
    placeholder="Enter a city or address"
/>

4️⃣ Styling the Input Field

This component allows full customization via props.

<GooglePlacesAutocomplete
    apiKey="YOUR_GOOGLE_API_KEY"
    onPlaceSelect={handlePlaceSelect}
    inputClassName="custom-input"
    suggestionsClassName="custom-suggestions"
    suggestionItemClassName="custom-suggestion-item"
    inputStyle={{ padding: "10px", fontSize: "16px" }}
    suggestionsStyle={{ backgroundColor: "white", borderRadius: "5px" }}
    suggestionItemStyle={{ padding: "8px", cursor: "pointer" }}
/>

βš™οΈ Props

Prop NameTypeRequiredDescription
apiKeystringβœ… YesYour Google Maps API key.
onPlaceSelectfunctionβœ… YesCallback function triggered when a place is selected.
placeholderstring❌ NoPlaceholder text for the input field (default: "Search for a location").
inputClassNamestring❌ NoCustom class name for the input field.
suggestionsClassNamestring❌ NoCustom class name for the suggestions dropdown.
suggestionItemClassNamestring❌ NoCustom class name for each suggestion item.
inputStyleobject❌ NoInline styles for the input field.
suggestionsStyleobject❌ NoInline styles for the suggestions container.
suggestionItemStyleobject❌ NoInline styles for suggestion items.

🌎 Google API Setup

To use this package, you need a Google API key with the Places API enabled.

Steps to Get an API Key:

  • Go to the Google Cloud Console.
  • Create a new project or select an existing one.
  • Enable the Places API under "APIs & Services".
  • Generate an API key under "Credentials".
  • Restrict the key for security (optional).
  • Use the key in the apiKey prop.

πŸ›  Troubleshooting

1️⃣ Autocomplete not working?

βœ… Ensure:

  • The Places API is enabled in your Google Cloud Console.
  • The API key is correctly added.
  • No CORS or billing issues exist in your Google account.

2️⃣ API limit exceeded?

βœ… Check your Google Cloud billing settings. Google provides free tier limits for Places API usage.

βœ… Make sure:

  • The script is loaded only once.
  • The key is valid and has permissions.

πŸ“œ License

This project is licensed under the MIT License. See the LICENSE file for details.

πŸ™Œ Contributing

Contributions are welcome! If you’d like to improve this package:

  • Fork the repository.
  • Create a feature branch: git checkout -b feature-name.
  • Commit your changes: git commit -m "Added feature XYZ".
  • Push to the branch: git push origin feature-name.
  • Open a Pull Request.

πŸ’¬ Support

If you encounter any issues or have suggestions, feel free to:

  • Open an issue on GitHub
  • Reach out via Twitter

Happy coding! πŸš€

Keywords

google places

FAQs

Package last updated on 27 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts