@appandflow/react-native-google-autocomplete

中文文档
2.2.0发布于 2026/02/16MITTypeScriptESM + CJS近 30 天下载 1.3万

A library to help you use google places autocomplete

原始使用文档
文档目录7 节
  1. About
  2. Installation
  3. Usage
  4. Results
  5. Typings
  6. Custom Headers
  7. Restrict by country

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. [email protected]

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.
originoptional - The origin point as { lat, lng } from which to calculate straight-line distance to the destinations (returned as distance_meters)
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
headersoptional - Custom headers to include in the Google Places API requests. Useful for passing platform-specific API restrictions such as X-Android-Package, X-Android-Cert, or X-Ios-Bundle-Identifier.

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[];
  distance_meters?: number; // Present when origin parameter is provided
}

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';

Custom Headers

You can pass custom headers to the underlying Google Places API requests. This is useful for application restrictions such as Android app or iOS bundle restrictions.

// Android
const { locationResults, setTerm } = useGoogleAutocomplete(API_KEY, {
  headers: {
    'X-Android-Package': 'com.example.app',
    'X-Android-Cert': 'your-signing-certificate-fingerprint',
  },
});

// iOS
const { locationResults, setTerm } = useGoogleAutocomplete(API_KEY, {
  headers: {
    'X-Ios-Bundle-Identifier': 'com.example.app',
  },
});

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.

版本与发布记录

@appandflow/react-native-google-autocomplete 共发布 11 个版本,首次发布于 2024/07/11;过去 12 个月发布了 3 个版本。

版本发布日期距今
2.2.0latest2026/02/168 个月前
2.1.22026/01/319 个月前
2.1.12026/01/309 个月前
2.1.02025/03/011.7 年前
2.0.22024/11/251.9 年前
2.0.12024/07/232 年前

其他发布通道:beta → 2.0.0-beta.3。

关于 @appandflow/react-native-google-autocomplete 的常见问题

@appandflow/react-native-google-autocomplete 是什么?

A library to help you use google places autocomplete

如何安装 @appandflow/react-native-google-autocomplete?

在项目目录运行 npm install @appandflow/react-native-google-autocomplete,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@appandflow/react-native-google-autocomplete 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@appandflow/react-native-google-autocomplete 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@appandflow/react-native-google-autocomplete 可以商用吗?

@appandflow/react-native-google-autocomplete 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@appandflow/react-native-google-autocomplete 的下载量有多少?

根据 npm 官方下载统计,@appandflow/react-native-google-autocomplete 在 2026/10/07 – 2026/11/05 这 30 天内被下载 12,671 次,日均约 422 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@appandflow/react-native-google-autocomplete 还在维护吗?

最新版本 2.2.0 发布于 2026/02/16(8 个月前);至今共发布 11 个版本,其中过去 12 个月发布了 3 个;首次发布于 2024/07/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。