Giter Site home page Giter Site logo

kenvrlim / expo-google-places-autocomplete Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alanjhughes/expo-google-places-autocomplete

0.0 0.0 0.0 1.52 MB

License: MIT License

Shell 0.72% JavaScript 2.79% Ruby 5.15% C++ 10.77% Objective-C 0.57% Java 25.99% Kotlin 8.80% TypeScript 24.10% Objective-C++ 9.61% Swift 8.15% CMake 0.44% Starlark 2.92%

expo-google-places-autocomplete's Introduction

expo-google-places-autocomplete

Google Places Autocomplete for React Native. This library uses the native Google Places SDK for iOS and Android.

Preview

drawing

Installation

npx expo install expo-google-places-autocomplete

For bare React Native projects, you must ensure that you have installed and configured the expo package before continuing.

Configuration for iOS ๐Ÿ

This is only required for usage in bare React Native apps.

Run npx pod-install after installing the npm package.

Configuration for Android ๐Ÿค–

No further steps are needed on Android

Get your api key

Create a an API key here and enable the "Places API". You must have billing enabled on the account.

Usage

import { GooglePlacesAutocomplete } from "expo-google-places-autocomplete";

// ...
const onSearchError = React.useCallback((error: PlacesError) => {
  console.log(error);
}, []);

const onPlaceSelected = React.useCallback((place: PlaceDetails) => {
  console.log(place);
}, []);

<View>
  <GooglePlacesAutocomplete
    apiKey={API_KEY}
    requestConfig={{ countries: ["US"] }}
    onPlaceSelected={onPlaceSelected}
    onSearchError={onSearchError}
  />
</View>;

Build your own

The library exposes three fucntions that you can use to build your own autocomplete component.

Start by initializing the SDK with your API key.

import PlacesAutocomplete from "expo-google-places-autocomplete";

// ...

useEffect(() => {
  PlacesAutocomplete.initPlaces(apiKey);
}, [apiKey]);

To get a list of predictions based on a users input you can attach an onChangeText handler to your TextInput and pass in your RequestConfig object. You will be returned an array of Place objects with at most 6 results.

  // ...
  const [inputValue, setInputValue] = React.useState("");
  const [results, setResults] = React.useState<Place[]>([]);

  // ...
  const onChangeText = React.useCallback(
    async (text: string) => {
      try {
        let result = await PlacesAutocomplete.findPlaces(text, requestConfig);
        setResults(result.places);
        setInputValue(text);
      } catch (e) {
        console.log(e);
      }
    },
    [requestConfig],
  );

When an item is selected from your list of results you can get the PlaceDetails with the following

const onPlaceSelected = React.useCallback(
  async (placeId: string) => {
    try {
      const details = await PlacesAutocomplete.placeDetails(placeId);
      console.log(details);
    } catch (e) {
      console.log(e);
    }
  },
  [onPlaceSelected],
);

Styling the provided component

key type
containerStyle ViewStyle
searchInputStyle ViewStyle
inputContainerStyle ViewStyle
resultsContainerStyle ViewStyle
resultItemStyle ViewStyle
listFooterStyle ViewStyle

Contributing

Contributions are welcome!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.