When using the Google Places Details API in a web browser (including Expo Web), you’ll encounter Cross-Origin Resource Sharing (CORS) restrictions. Google’s Places API doesn’t include the necessary CORS headers to allow direct browser requests, which results in errors like:
Access to fetch at 'https://places.googleapis.com/v1/places/...' has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
To work around this limitation, you need to set up a proxy server that:
The proxy server adds the necessary CORS headers to make the browser happy.
We’ve included an example proxy server in the repository. Here’s how to set it up:
Navigate to your project directory and install the required packages:
npm install express axios
# or
yarn add express axios
Copy the example proxy server code from example/places-details-proxy.js
to your project. You can place it in your project root or in a server directory.
Start the proxy server:
node places-details-proxy.js
By default, the server will run at http://localhost:3001
.
Update your GooglePlacesTextInput component to use the proxy:
<GooglePlacesTextInput
apiKey="YOUR_GOOGLE_PLACES_API_KEY"
fetchDetails={true}
detailsProxyUrl="http://localhost:3001/places-details"
detailsFields={['formattedAddress', 'location']}
onPlaceSelect={handlePlaceSelect}
/>
For production use:
detailsProxyUrl
to point to your deployed proxyWhen implementing this in production:
By following these steps, you can use Google Places Details API with Expo Web applications effectively.