Socket
Book a 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. ๐Ÿš€

latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
1
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