Categories: softare development

How to Use Google Places AutoComplete in React Native

The Google Places Autocomplete feature in mobile software development is useful for searching for verified locations that are available on Google Maps.

In this tutorial, we are going to see how we can use the Google Places Autocomplete feature in our React Native application.

Let’s get started!

1. Obtain an API key from Google Cloud Console

First you need to add billing to your Google Cloud Console account and enable Geocoding.

2. Install the Google Places AutoComplete dependency

Read more about the Google AutoComplete dependency

yarn add react-native-google-places-autocomplete

3. Add code for the Google API

<GooglePlacesAutocomplete
    placeholder='Search location'
    returnKeyType={'default'}
    fetchDetails={true}
    currentLocation = {true}
    isRowScrollable={true}
    keepResultsAfterBlur={false}
    enablePoweredByContainer = { false }
    styles={{
      textInputContainer: {
        marginTop: 0,
      },
      textInput: {
        height: 45,
        color: 'black',
        fontSize: 16,
        backgroundColor: '#ddd'
      },
      predefinedPlacesDescription: {
        color: '#1faadb',
      },
      listView: {
        position: 'relative',
        zIndex: 3,
        marginTop: 0,
        padding: 0
      },
      row: {
      backgroundColor: '#FFFFFF',
      height: 100,
      flexDirection: 'row',
      },
      separator: {
      height: 0.5,
      backgroundColor: '#c8c7cc',
      },
      description: {},
      loader: {
      flexDirection: 'row',
      justifyContent: 'flex-end',
      height: 20,
      },
    }}

    onPress={(data, details) => {
      // 'details' is provided when fetchDetails = true
      // console.warn(details);
    }}
    query={{
      key: API_KEY,
      language: 'en',
      components: 'country:us'
    }}
    />

Result:

Did it work out for you? Let me know in the comments.

Don’t build mobile apps from scratch

Author

Recent Posts

Observer Pattern in JavaScript: Implementing Custom Event Systems

Introduction The Observer Pattern is a design pattern used to manage and notify multiple objects…

3 weeks ago

Memory Management in JavaScript

Memory management is like housekeeping for your program—it ensures that your application runs smoothly without…

1 month ago

TypeScript vs JavaScript: When to Use TypeScript

JavaScript has been a developer’s best friend for years, powering everything from simple websites to…

1 month ago

Ethics in Web Development: Designing for Inclusivity and Privacy

In the digital age, web development plays a crucial role in shaping how individuals interact…

1 month ago

Augmented Reality (AR) in Web Development Augmented Reality (AR) is reshaping the way users interact…

1 month ago

Node.js Streams: Handling Large Data Efficiently

Introduction Handling large amounts of data efficiently can be a challenge for developers, especially when…

1 month ago