|
| 1 | +# Using Place Details with Expo Web |
| 2 | + |
| 3 | +## The CORS Issue with Google Places Details API |
| 4 | + |
| 5 | +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: |
| 6 | + |
| 7 | +``` |
| 8 | +Access to fetch at 'https://places.googleapis.com/v1/places/...' has been blocked by CORS policy: |
| 9 | +No 'Access-Control-Allow-Origin' header is present on the requested resource. |
| 10 | +``` |
| 11 | + |
| 12 | +## Solution: Use a Proxy Server |
| 13 | + |
| 14 | +To work around this limitation, you need to set up a proxy server that: |
| 15 | + |
| 16 | +1. Receives requests from your Expo Web application |
| 17 | +2. Forwards them to the Google Places Details API |
| 18 | +3. Returns the response back to your application |
| 19 | + |
| 20 | +The proxy server adds the necessary CORS headers to make the browser happy. |
| 21 | + |
| 22 | +## Setting Up the Proxy Server |
| 23 | + |
| 24 | +We've included an example proxy server in the repository. Here's how to set it up: |
| 25 | + |
| 26 | +### 1. Install Dependencies |
| 27 | + |
| 28 | +Navigate to your project directory and install the required packages: |
| 29 | + |
| 30 | +```bash |
| 31 | +npm install express axios |
| 32 | +# or |
| 33 | +yarn add express axios |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Copy the Proxy Server Code |
| 37 | + |
| 38 | +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. |
| 39 | + |
| 40 | +### 3. Run the Proxy Server |
| 41 | + |
| 42 | +Start the proxy server: |
| 43 | + |
| 44 | +```bash |
| 45 | +node places-details-proxy.js |
| 46 | +``` |
| 47 | + |
| 48 | +By default, the server will run at `http://localhost:3001`. |
| 49 | + |
| 50 | +### 4. Configure Your React Native Component |
| 51 | + |
| 52 | +Update your GooglePlacesTextInput component to use the proxy: |
| 53 | + |
| 54 | +```javascript |
| 55 | +<GooglePlacesTextInput |
| 56 | + apiKey="YOUR_GOOGLE_PLACES_API_KEY" |
| 57 | + fetchDetails={true} |
| 58 | + detailsProxyUrl="http://localhost:3001/places-details" |
| 59 | + detailsFields={['formattedAddress', 'location']} |
| 60 | + onPlaceSelect={handlePlaceSelect} |
| 61 | +/> |
| 62 | +``` |
| 63 | + |
| 64 | +## Deploying to Production |
| 65 | + |
| 66 | +For production use: |
| 67 | + |
| 68 | +1. Deploy the proxy server to a hosting service (like Heroku, Vercel, AWS, etc.) |
| 69 | +2. Update your `detailsProxyUrl` to point to your deployed proxy |
| 70 | + |
| 71 | +## Security Considerations |
| 72 | + |
| 73 | +When implementing this in production: |
| 74 | + |
| 75 | +1. **Don't expose your Google API key**: Consider validating requests and using your API key on the server side only |
| 76 | +2. **Limit access**: Restrict who can use your proxy with authentication or origin checking |
| 77 | +3. **Monitor usage**: Keep track of API usage to avoid unexpected costs |
| 78 | + |
| 79 | +By following these steps, you can use Google Places Details API with Expo Web applications effectively. |
0 commit comments