Skip to content

Commit 6d1a290

Browse files
committed
feat: add origin parameter to calculate distance in Google Autocomplete
1 parent 78156e9 commit 6d1a290

4 files changed

Lines changed: 37 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The `useGoogleAutocomplete` hook takes 2 arguments
3636
| lat | optional - The latitude. If provided, lng is required |
3737
| lng | optional - The longitude. If provided, lat is required |
3838
| strictBounds | optional - Returns only places that are strictly within the region defined by location and radius. |
39+
| origin | optional - The origin point as { lat, lng } from which to calculate straight-line distance to the destinations (returned as distance_meters) |
3940
| proxyUrl | optional - This is required if you want to use the hook in a Web based platform. Since we dont use the Google SDK, the http call will fail because of issues related to CORS unless a proxyUrl is provided |
4041
| headers | optional - Custom headers to include in the Google Places API requests. Useful for passing platform-specific API restrictions such as `X-Android-Package`, `X-Android-Cert`, or `X-Ios-Bundle-Identifier`. |
4142

@@ -109,6 +110,7 @@ export interface GoogleLocationResult {
109110
value: string;
110111
}>;
111112
types: string[];
113+
distance_meters?: number; // Present when origin parameter is provided
112114
}
113115
```
114116

example/src/App.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export default function App() {
1919
language: 'en',
2020
minLength: 3,
2121
proxyUrl: isWeb ? 'https://cors-anywhere.herokuapp.com/' : undefined,
22+
// Example: Using origin to get distance from Times Square, NYC
23+
// Uncomment to see distance_meters in results
24+
// origin: { lat: 40.7580, lng: -73.9855 },
2225
}
2326
);
2427

@@ -39,6 +42,11 @@ export default function App() {
3942
{locationResults.map((location) => (
4043
<View key={location.id}>
4144
<Text>{location.structured_formatting.main_text}</Text>
45+
{location.distance_meters && (
46+
<Text style={{ fontSize: 12, color: '#666' }}>
47+
{Math.round(location.distance_meters)} meters away
48+
</Text>
49+
)}
4250
</View>
4351
))}
4452
</ScrollView>

src/GoogleAutocomplete.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ interface Options {
7272
*/
7373
strictBounds?: boolean;
7474

75+
/**
76+
* The origin point from which to calculate straight-line distance to the destination (returned as distance_meters).
77+
* If omitted, straight-line distance will not be returned.
78+
*/
79+
origin?: {
80+
lat: number;
81+
lng: number;
82+
};
83+
7584
/**
7685
* Proxy url if you want to use the web, this is needed cause of CORS issue
7786
*/
@@ -120,6 +129,7 @@ export const useGoogleAutocomplete = (apiKey: string, opts: Options = {}) => {
120129
lng: opts.lng,
121130
radius: opts.radius,
122131
components: opts.components,
132+
origin: opts.origin,
123133
},
124134
opts.proxyUrl,
125135
opts.headers

src/services/google.service.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export interface Query {
1616
lat?: number;
1717
lng?: number;
1818
strictBounds?: boolean;
19+
origin?: {
20+
lat: number;
21+
lng: number;
22+
};
1923
}
2024

2125
export interface GoogleLocationDetailResult {
@@ -71,6 +75,7 @@ export interface GoogleLocationResult {
7175
value: string;
7276
}>;
7377
types: string[];
78+
distance_meters?: number;
7479
}
7580

7681
interface NormalizeQuery {
@@ -85,11 +90,12 @@ interface NormalizeQuery {
8590
components?: string;
8691
radius?: string;
8792
location?: string;
93+
origin?: string;
8894
strictBounds?: boolean;
8995
}
9096

9197
const normalizeQuery = (query: Query): NormalizeQuery => {
92-
const { lat, lng, ...rest } = query;
98+
const { lat, lng, origin, ...rest } = query;
9399

94100
// The latitude/longitude around which to retrieve place information. This must be specified as latitude,longitude.
95101
let location;
@@ -103,9 +109,19 @@ const normalizeQuery = (query: Query): NormalizeQuery => {
103109
location = `${lat},${lng}`;
104110
}
105111

112+
// The origin point from which to calculate straight-line distance. This must be specified as latitude,longitude.
113+
let originStr;
114+
if (origin) {
115+
if (!origin.lat || !origin.lng) {
116+
throw new Error('Query: Origin must have both lat & lng');
117+
}
118+
originStr = `${origin.lat},${origin.lng}`;
119+
}
120+
106121
return {
107122
...rest,
108123
location,
124+
origin: originStr,
109125
};
110126
};
111127

0 commit comments

Comments
 (0)