To help you to understand how to use the library, we have created a set of guides that will help you to get started with some of the most common use cases.


Per Session (session-based) approach

If your application requires Place Details such as the place name, business status, or opening hours, your implementation of Place Autocomplete should use a session token for a total cost of $0.017 per session plus applicable Places Data SKUs depending on which place data fields you request.



Take what you need

As you can see in the video above you can get partial data of the place details specifying the place fields that you want to receive. This is strongly recommended since there are a lot of data that you may don’t need and will generate a SKU charge.


Under the hood

Once the module is created, a session token will be automatically created for you and will be used to make the requests to the Google Places API with the autocomplete service. The session token will be automatically refreshed once a user fetches the place details.


Implementation

Since we already handle the session token and the whole logic to support both Android and iOS platforms for you, the implementation is pretty simple. You just need to complete the following steps:

NOTE
If you have not completed the Getting Started guide, please do so before proceeding.


1. Import the library

import * as ExpoGooglePlaces from "expo-google-places";


2. Fetch predictions

The fetchPredictionsWithSession method is the one designed to fetch the predictions using the session-based approach making a single request to the Google Places API with the Autocomplete Service.

const fetchPredictions = async (search: string) => {
  try {
    const predictions = await ExpoGooglePlaces.fetchPredictionsWithSession(search, {
      countries: ["us"],
    });
    // Do something with the predictions like set them
    // into a state and render them in a list
  } catch (error) {
    console.log("Error fetching predictions", error);
  }
};

It takes two arguments in the example above, the first one search is the text that the user has typed in the search input and the second one (which is optional) { countries: ["us"] } is an object where you can specify the restrictions that you want to apply to the predictions.


3. Fetch place details

The fetchPlaceWithSession method is the one designed to fetch the place details using the session-based approach making a single request to the Google Places API with the Places Service.

const fetchPlace = async (placeID: string) => {
  try {
    const placeDetails = await ExpoGooglePlaces.fetchPlaceWithSession(placeID, [
      "name",
      "formattedAddress",
      "coordinate",
    ]);
    // Do something with the details like storing them into a state
    // and displaying them in the UI and/or in a map
  } catch (error) {
    console.log("Error fetching place details", error);
  }
};

It takes two arguments in the example above, the first one placeID is the unique identifier of the place that you want to fetch the details and the second one (which is optional) ["name", "formattedAddress", "coordinate"] is an array of strings where you can specify the place fields that you want to receive.


Conclusion

As you can see, the implementation is pretty simple and straightforward. You just need to use the methods that we provide you according to your needs and we will handle the rest for you.


Per Request approach

🚧 Under construction