Skip to content

Commit b43947e

Browse files
committed
feat: add locationBias and locationRestriction for geographic filtering
1 parent cca39cd commit b43947e

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ const CustomizedExample = () => {
251251
| proxyHeaders | object | No | - | Headers to pass to the proxy (ex. { Authorization: 'Bearer AUTHTOKEN' } ) |
252252
| languageCode | string | No | - | Language code (e.g., 'en', 'fr') |
253253
| includedRegionCodes | string[] | No | - | Array of region codes to filter results |
254+
| locationBias | Record<string, any> | No | - | Bias search results by location (circle or rectangle). [Details](#location-filtering) |
255+
| locationRestriction | Record<string, any> | No | - | Restrict search results to location (circle or rectangle). [Details](#location-filtering) |
254256
| types | string[] | No | [] | Array of place types to filter |
255257
| biasPrefixText | (text: string) => string | No | - | Optional function to modify the input text before sending to the Places API |
256258
| minCharsToFetch | number | No | 1 | Minimum characters before triggering search |
@@ -276,6 +278,58 @@ const CustomizedExample = () => {
276278
| onError | (error: any) => void | No | - | Callback when API errors occur |
277279
| enableDebug | boolean | No | false | Enable detailed console logging for troubleshooting |
278280

281+
## Location Filtering
282+
283+
You can filter or bias search results based on geographic location using `locationBias` or `locationRestriction`.
284+
285+
### Location Bias (Soft Filter)
286+
287+
**Biases** results towards a location but can still return results outside the area if they're highly relevant:
288+
289+
```javascript
290+
<GooglePlacesTextInput
291+
apiKey="YOUR_GOOGLE_PLACES_API_KEY"
292+
onPlaceSelect={handlePlaceSelect}
293+
locationBias={{
294+
circle: {
295+
center: {
296+
latitude: 37.7937,
297+
longitude: -122.3965
298+
},
299+
radius: 500.0 // meters
300+
}
301+
}}
302+
/>
303+
```
304+
305+
### Location Restriction (Hard Filter)
306+
307+
**Restricts** results to only those within the specified area:
308+
309+
```javascript
310+
<GooglePlacesTextInput
311+
apiKey="YOUR_GOOGLE_PLACES_API_KEY"
312+
onPlaceSelect={handlePlaceSelect}
313+
locationRestriction={{
314+
rectangle: {
315+
low: { latitude: 37.7749, longitude: -122.4194 },
316+
high: { latitude: 37.8049, longitude: -122.3894 }
317+
}
318+
}}
319+
/>
320+
```
321+
322+
**Supported shapes:**
323+
- **Circle**: Define center (lat/lng) and radius in meters
324+
- **Rectangle**: Define southwest and northeast corners (low/high)
325+
326+
**Use cases:**
327+
- Show places near user's current GPS location
328+
- Limit results to delivery radius
329+
- Search within a specific neighborhood or city bounds
330+
331+
For more details, see [Google Places API Location Biasing](https://developers.google.com/maps/documentation/places/web-service/autocomplete#location_biasing).
332+
279333
## Place Details Fetching
280334

281335
You can automatically fetch detailed place information when a user selects a place suggestion by enabling the `fetchDetails` prop:

src/GooglePlacesTextInput.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ interface GooglePlacesTextInputProps
128128
proxyHeaders?: Record<string, string>;
129129
languageCode?: string;
130130
includedRegionCodes?: string[];
131+
locationBias?: Record<string, any>;
132+
locationRestriction?: Record<string, any>;
131133
types?: string[];
132134
biasPrefixText?: (text: string) => string;
133135
minCharsToFetch?: number;
@@ -176,6 +178,8 @@ const GooglePlacesTextInput = forwardRef<
176178
proxyHeaders = null,
177179
languageCode,
178180
includedRegionCodes,
181+
locationBias,
182+
locationRestriction,
179183
types = [],
180184
biasPrefixText,
181185
minCharsToFetch = 1,
@@ -307,6 +311,8 @@ const GooglePlacesTextInput = forwardRef<
307311
sessionToken,
308312
languageCode,
309313
includedRegionCodes,
314+
locationBias,
315+
locationRestriction,
310316
types,
311317
minCharsToFetch,
312318
});
@@ -331,6 +337,8 @@ const GooglePlacesTextInput = forwardRef<
331337
sessionToken,
332338
languageCode,
333339
includedRegionCodes,
340+
locationBias,
341+
locationRestriction,
334342
types,
335343
biasPrefixText,
336344
});

src/services/googlePlacesApi.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface FetchPredictionsParams {
1010
sessionToken?: string | null;
1111
languageCode?: string;
1212
includedRegionCodes?: string[];
13+
locationBias?: Record<string, any>;
14+
locationRestriction?: Record<string, any>;
1315
types?: string[];
1416
biasPrefixText?: (text: string) => string;
1517
}
@@ -45,6 +47,8 @@ export const fetchPredictions = async ({
4547
sessionToken,
4648
languageCode,
4749
includedRegionCodes,
50+
locationBias,
51+
locationRestriction,
4852
types = [],
4953
biasPrefixText,
5054
}: FetchPredictionsParams): Promise<PredictionResult> => {
@@ -76,6 +80,8 @@ export const fetchPredictions = async ({
7680
...(sessionToken && { sessionToken }),
7781
...(includedRegionCodes &&
7882
includedRegionCodes.length > 0 && { includedRegionCodes }),
83+
...(locationBias && { locationBias }),
84+
...(locationRestriction && { locationRestriction }),
7985
...(types.length > 0 && { includedPrimaryTypes: types }),
8086
};
8187

0 commit comments

Comments
 (0)