Skip to content

Commit ccb783d

Browse files
authored
Merge pull request #64 from ubilabs/feat/new-hook-distance-matrix
feat(hooks): add new hook for distance matrix service
2 parents 55aa9cc + 798a2a7 commit ccb783d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/hooks/distance-matrix.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {useMemo} from 'react';
2+
3+
import useGoogleMap from './map-instance';
4+
5+
/**
6+
* Hook to get Distance Matrix Service instance
7+
*/
8+
const useDistanceMatrix =
9+
(): google.maps.DistanceMatrixService | null => {
10+
const {map} = useGoogleMap();
11+
12+
// Creates a Distance Matrix Service instance
13+
const distanceMatrixService =
14+
useMemo<google.maps.DistanceMatrixService | null>(() => {
15+
// Wait for map to be initialized
16+
if (!map) {
17+
return null;
18+
}
19+
20+
if (!google.maps.DistanceMatrixService) {
21+
throw Error('Distance Matrix library missing.');
22+
}
23+
24+
return new google.maps.DistanceMatrixService();
25+
}, [map]);
26+
27+
return distanceMatrixService;
28+
};
29+
30+
export default useDistanceMatrix;

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export {default as usePlacesService} from './hooks/places';
44
export {default as useAutocomplete} from './hooks/autocomplete';
55
export {default as useDirections} from './hooks/directions';
66
export {default as useGeocoder} from './hooks/geocoder';
7+
export {default as useDistanceMatrix} from './hooks/distance-matrix';
8+

0 commit comments

Comments
 (0)