πŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more β†’
Socket
DemoInstallSign in
Socket

rn-use-google-places-autocomplete

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rn-use-google-places-autocomplete

An easy-to-use hook for integrating Google Places Autocomplete in your app with Maps API. πŸš€

1.1.1
latest
Source
npm
Version published
Weekly downloads
78
151.61%
Maintainers
1
Weekly downloads
Β 
Created
Source

πŸ—ΊοΈ useGooglePlacesAutocomplete

A fully-typed and modern React Native hook for consuming Google Places API v1, without axios or unnecessary dependencies.

✨ Why this package?

The goal was to build a lightweight, developer-friendly and React Native-compliant hook that allows you to easily integrate Google Places Autocomplete in your apps, with geolocation support β€” all typed, simple, and production-ready.

Most existing packages either:

  • Use outdated APIs (Place Autocomplete v1 is recommended)
  • Rely on axios, which bloats your bundle
  • Aren’t tailored for React Native
  • Lack proper TypeScript support

This solves all that in one clean, open-source package πŸš€

πŸ“¦ Installation

npm install rn-use-google-places-autocomplete
# or
yarn add rn-use-google-places-autocomplete

πŸ› οΈ Setup Before Use

Before you start using this package, ensure that you’ve properly set up your Google Cloud Platform (GCP) project and API keys. Here are the steps to follow:

1. Create a Google Cloud Project

  • Access the Google Cloud Console: Go to Google Cloud Console.
  • Create a New Project: Click on the project dropdown and select "New Project". Give it a name and create it.

2. Enable Billing

  • Enable Billing for Your Project: You need to link a billing account to your project. If you don’t have one, create it following the instructions in the console.

Note: Google Cloud offers a free tier, and you won’t be charged unless your usage exceeds the free tier limits.

3. Enable Required APIs

In order to use Google Places Autocomplete and Geocoding, you must enable the following APIs:

  • Places API
  • Geocoding API

a. Enable APIs:

  • Go to the API Library in the Google Cloud Console.
  • Enable Places API:
    • Search for "Places API", select it, and click "Enable".
  • Enable Geocoding API:
    • Search for "Geocoding API", select it, and click "Enable".

4. Create an API Key

  • Navigate to API Credentials: In the Google Cloud Console, go to "APIs & Services" > "Credentials".
  • Create a New API Key:
    • Click on "Create Credentials" and choose "API Key".
    • Copy the generated key. This key will be used in your app.
  • Restrict Your API Key (Recommended):
    • To prevent unauthorized usage, restrict the key to only work with your allowed referrers (such as your domain or mobile app).
    • You can also restrict the key to only the Places and Geocoding APIs.

For a complete guide on setting up your API keys, see Google’s official documentation.

πŸ› οΈ Usage

import useGooglePlacesAutocomplete from "use-google-places-autocomplete";
import { useGooglePlacesAutocomplete } from 'rn-use-google-places-autocomplete';
import { Text, View, TextInput, FlatList, TouchableOpacity, RefreshControl, } from 'react-native';

export default function App() {
  const { query, setQuery, places, loading, error } =
  useGooglePlacesAutocomplete({
    gcpApiKey: "YOUR_GOOGLE_API_KEY",
    language: "en", // or "fr", etc.
    countries: ["FR", "US"], // Optional country filter
  });

  return(
    <View>
      // Bind `query` to your input and render `places`
      <TextInput
        placeholder="Search a place..."
        value={query}
        onChangeText={setQuery}
      />
      // Render the places
      <FlatList
        refreshControl={
          <RefreshControl 
            onRefresh={() => {}} 
            refreshing={loading} 
          />
        }
        refreshing={loading}
        data={places}
        keyExtractor={(item) => item.id}
        renderItem={({ item }) => (
          <TouchableOpacity onPress={() => {}}>
            <Text>{item.name}</Text>
            <Text>{item.fullAddress}</Text>
            <Text>
              Lat : {item.latitude} | Long : {item.longitude}
            </Text>
          </TouchableOpacity>
        )}
      />
    </View>
  )
}

🧠 Features

  • βœ… Fully typed (TypeScript ready)
  • βœ… Google Places API v1 compatible
  • βœ… Geocoding (lat/lng) included
  • βœ… Customizable endpoints (for advanced use)
  • βœ… Designed for React Native
  • βœ… No external dependencies

πŸ“„ API

useGooglePlacesAutocomplete(props)

PropTypeRequiredDescription
gcpApiKeystringβœ…Your Google Cloud API Key with Places and Geocoding enabled
languagestring❌Language code (default is 'fr')
countriesstring[]❌Country restriction (e.g., ['FR', 'US'])
autocompUrlstring❌Override autocomplete endpoint
geocodingUrlstring❌Override geocoding endpoint
timeoutValuenumber❌Override timeout (default is 1500)

Returns

FieldTypeDescription
querystringThe current user input
setQueryfunctionUpdate the query string
placesPlace[]List of place suggestions with metadata
loadingbooleanWhether a request is in progress
errorstring | nullAny API or fetch error

Place Object Structure

interface Place {
  id: string;
  name: string;
  fullAddress: string;
  types: string[];
  latitude: number | null;
  longitude: number | null;
  address_components: {
    mainText: { text: string };
    secondaryText: { text: string };
  };
  raw?: PlacePrediction; // Full Google prediction object
}

πŸš€ Demo

πŸ“Ί Demo Video

You can view the demo video by clicking here.

🀝 Contribute

PRs are welcome! If you’d like to add features (like place detail fetch), feel free to open an issue or contribute directly.

πŸ’¬ Need a custom React Native component?

I build production-grade mobile experiences, MVPs and complex app architectures.
πŸ“§ Contact me at contact@devops-office.com

πŸ“„ License

MIT – Free for personal and commercial use.

Keywords

react-native

FAQs

Package last updated on 09 Apr 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