File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed
Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change @@ -4,3 +4,5 @@ export {default as usePlacesService} from './hooks/places';
44export { default as useAutocomplete } from './hooks/autocomplete' ;
55export { default as useDirections } from './hooks/directions' ;
66export { default as useGeocoder } from './hooks/geocoder' ;
7+ export { default as useDistanceMatrix } from './hooks/distance-matrix' ;
8+
You can’t perform that action at this time.
0 commit comments