You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@appandflow/react-native-google-autocomplete

Package Overview
Dependencies
Maintainers
0
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@appandflow/react-native-google-autocomplete

A library to help you use google places autocomplete

2.1.0
latest
Source
npm
Version published
Weekly downloads
842
15.03%
Maintainers
0
Weekly downloads
 
Created
Source

google_autocomplete

About

App & Flow is a Montreal-based React Native engineering and consulting studio. We partner with the world’s top companies and are recommended by Expo. Need a hand? Let’s build together. team@appandflow.com

Installation

yarn add @appandflow/react-native-google-autocomplete
npm i @appandflow/react-native-google-autocomplete

Usage

The useGoogleAutocomplete hook takes 2 arguments

ArgDescription
apiKeyYour google places api key
configoptional config object

Config object

PropertyDescription
debounceoptional - default 300
debounceOptionsoptional - Configuration options for debounce behavior.
languageoptional - default 'en'
queryTypesoptional - default address - https://developers.google.com/places/web-service/autocomplete#place_types
minLengthoptional - default 2 - this is the min length of the term search before start
componentsoptional - A grouping of places to which you would like to restrict your results
radiusoptional - The distance (in meters) within which to return place results
latoptional - The latitude. If provided, lng is required
lngoptional - The longitude. If provided, lat is required
strictBoundsoptional - Returns only places that are strictly within the region defined by location and radius.
proxyUrloptional - This is required if you want to use the hook in a Web based platform. Since we dont use the Google SDK, the http call will fail because of issues related to CORS unless a proxyUrl is provided

Exposed properties

PropertyDescription
clearSearchClears your current search
isSearchingBoolean that lets you know the search is underway
locationResultsThe array of results of the search
searchDetailsFunction that lets you get more details, good for when you select a result
searchErrorErrors that could occur during search
term/setTermThe term is the search term used, it's set using setTerm

Code example

This is a minimalistic functionnal snippet that returns 3 results for your search. Clicking on a result logs the details of that result.

  const { locationResults, setTerm, clearSearch, searchDetails, term } =
    useGoogleAutocomplete(API_KEY, {
      language: 'en',
      debounce: 300,
    });

  return (
    <View>
      <TextInput
        value={term}
        onChangeText={setTerm}
      />
      {locationResults.slice(0, 3).map((el, i) => (
        <TouchableOpacity
          key={String(i)}
          onPress={async () => {
            const details = await searchDetails(el.place_id);
            console.log(JSON.stringify(details, null, 2));
          }}
        >
          <Text>{el.structured_formatting.main_text}</Text>
        </TouchableOpacity>
      ))}
    </View>
  );

Results

locationResults returns the following. The maximum result set by Google is 5 locations by query.

export interface GoogleLocationResult {
  description: string;
  id: string;
  matched_substrings: Array<{
    length: number;
    offset: number;
  }>;
  place_id: string;
  reference: string;
  structured_formatting: {
    main_text: string;
    secondary_text: string;
    main_text_matched_substrings: Array<{
      length: number;
    }>;
  };
  terms: Array<{
    offset: number;
    value: string;
  }>;
  types: string[];
}

When calling the searchDetails this is what you get

export interface GoogleLocationDetailResult {
  adr_address: string;
  formatted_address: string;
  icon: string;
  id: string;
  name: string;
  place_id: string;
  scope: string;
  reference: string;
  url: string;
  utc_offset: number;
  vicinity: string;
  types: string[];
  geometry: {
    location: {
      lat: number;
      lng: number;
    };
    viewport: {
      [type: string]: {
        lat: number;
        lng: number;
      };
    };
  };
  address_components: Array<{
    long_name: string;
    short_name: string;
    types: string[];
  }>;
}

Typings

You can import both result typing if you need for flow or typescript.

import {
  GoogleLocationDetailResult,
  GoogleLocationResult,
} from 'react-native-google-autocomplete';

Restrict by country

If you want to restrict the search by country you can add this as a props components="country:ca". This here would example restrict it to Canada only.

Keywords

react-native

FAQs

Package last updated on 28 Feb 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