Skip to content

Commit ebc420d

Browse files
authored
Merge pull request #67 from ubilabs/feat/example-directions
feat(examples): example for useDirections hook
2 parents 2d76869 + e40217f commit ebc420d

File tree

10 files changed

+183
-2
lines changed

10 files changed

+183
-2
lines changed

examples/directions/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Basic Google Maps Setup Example
2+
3+
This is an example setup to show the usage of the **useDirections hook with findAndRenderRoute** with the Google Maps React Hooks library.
4+
5+
## Instructions
6+
7+
To run this project, clone the Google Maps React Hooks repository locally.
8+
9+
Go to the root of the repository and run
10+
11+
12+
```shell
13+
npm install
14+
```
15+
16+
17+
once to install all dependencies.
18+
19+
Then start this example locally with
20+
21+
22+
```shell
23+
npm run start:directions-example
24+
```
25+
26+
**NOTE**:
27+
To see the examples it is needed to add an `.env` file with a [Google Maps API key](https://developers.google.com/maps/documentation/embed/get-api-key#:~:text=Go%20to%20the%20Google%20Maps%20Platform%20%3E%20Credentials%20page.&text=On%20the%20Credentials%20page%2C%20click,Click%20Close.) in the following format:
28+
29+
`GOOGLE_MAPS_API_KEY="<YOUR API KEY HERE>"`
30+
31+
An example can be found in `.env.example`.
32+
33+
## Output
34+
35+
The project will start at [localhost:1234](http://localhost:1234) and show a Google Map with a route from Berlin to Munich, retrieved from directions.
36+
37+
![image](https://user-images.githubusercontent.com/39244966/196410364-de14d9a0-5ecf-430d-846e-bc04e405b889.png)

examples/directions/app.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React, {FunctionComponent, useState, useCallback} from 'react';
2+
import {GoogleMapProvider} from '@ubilabs/google-maps-react-hooks';
3+
4+
import MapCanvas from './components/map-canvas/map-canvas';
5+
import DirectionsExample from './components/directions/directions';
6+
7+
import {GOOGLE_MAPS_API_KEY} from '../constants';
8+
9+
import './main.module.css';
10+
11+
const mapOptions = {
12+
center: {lat: 51.08998021141488, lng: 10.627828045134935},
13+
zoom: 6,
14+
disableDefaultUI: true,
15+
zoomControl: false,
16+
clickableIcons: false
17+
};
18+
19+
const App: FunctionComponent<Record<string, unknown>> = () => {
20+
const [mapContainer, setMapContainer] = useState<HTMLDivElement | null>(null);
21+
22+
const mapRef = useCallback(
23+
(node: React.SetStateAction<HTMLDivElement | null>) => {
24+
node && setMapContainer(node);
25+
},
26+
[]
27+
);
28+
29+
return (
30+
<React.StrictMode>
31+
<GoogleMapProvider
32+
googleMapsAPIKey={GOOGLE_MAPS_API_KEY}
33+
mapContainer={mapContainer}
34+
options={mapOptions}
35+
>
36+
<div id="container">
37+
<MapCanvas ref={mapRef} />
38+
<DirectionsExample />
39+
</div>
40+
</GoogleMapProvider>
41+
</React.StrictMode>
42+
);
43+
};
44+
45+
export default App;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {useEffect} from 'react';
2+
import {useDirections, useGoogleMap} from '@ubilabs/google-maps-react-hooks';
3+
4+
const DirectionsExample = () => {
5+
const {map} = useGoogleMap();
6+
7+
const directionsOptions = {
8+
renderOnMap: true,
9+
renderOptions: {
10+
suppressMarkers: true,
11+
polylineOptions: {strokeColor: '#FB2576', strokeWeight: 4}
12+
}
13+
};
14+
15+
// Use findAndRenderRoute to get directions and render a route to the map
16+
const {findAndRenderRoute} = useDirections(directionsOptions);
17+
18+
useEffect(() => {
19+
if (!map || !findAndRenderRoute) {
20+
return;
21+
}
22+
23+
// Form Request to pass to findAndRenderRoute
24+
// https://developers.google.com/maps/documentation/javascript/directions#DirectionsRequests
25+
const request = {
26+
travelMode: google.maps.TravelMode.DRIVING,
27+
origin: 'Berlin',
28+
destination: 'München',
29+
drivingOptions: {
30+
departureTime: new Date(),
31+
trafficModel: google.maps.TrafficModel.BEST_GUESS
32+
}
33+
};
34+
35+
findAndRenderRoute(request)
36+
.then((result: google.maps.DirectionsResult) => {
37+
// eslint-disable-next-line no-console
38+
console.log(result);
39+
})
40+
.catch((errorStatus: google.maps.DirectionsStatus) => {
41+
console.error(errorStatus);
42+
});
43+
}, [map]);
44+
45+
return null;
46+
};
47+
48+
export default DirectionsExample;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.map {
2+
width: 100%;
3+
height: 100%;
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React, {forwardRef} from 'react';
2+
3+
import styles from './map-canvas.module.css';
4+
5+
const MapCanvas = forwardRef<HTMLDivElement, Record<string, unknown>>(
6+
(_, ref) => <div ref={ref} className={styles.map} />
7+
);
8+
9+
export default MapCanvas;

examples/directions/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="de-DE">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta
6+
name="viewport"
7+
content="width=device-width, initial-scale=1.0, user-scalable=no"
8+
/>
9+
<title>
10+
Example of the usage of the useDirections hook with the Google Maps React Hooks.
11+
</title>
12+
<meta
13+
name="description"
14+
content="Example of the usage of the useDirections hook with the Google Maps React Hooks."
15+
/>
16+
</head>
17+
<body>
18+
<div id="app"></div>
19+
<script type="module" src="./index.tsx"></script>
20+
</body>
21+
</html>

examples/directions/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
4+
import App from './app';
5+
6+
ReactDOM.render(<App />, document.getElementById('app'));

examples/directions/main.module.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
html,
2+
body,
3+
:global(#app),
4+
:global(#container) {
5+
height: 100vh;
6+
overflow: hidden;
7+
margin: 0;
8+
padding: 0;
9+
}

examples/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"start:map-markers": "PARCEL_AUTOINSTALL=false parcel serve ./google-map-with-markers/index.html --dist-dir public --port 1234",
1111
"start:geocoding": "PARCEL_AUTOINSTALL=false parcel serve ./geocoding/index.html --dist-dir public --port 1234",
1212
"start:places": "PARCEL_AUTOINSTALL=false parcel serve ./places/index.html --dist-dir public --port 1234",
13-
"start:autocomplete": "PARCEL_AUTOINSTALL=false parcel serve ./places-autocomplete/index.html --dist-dir public --port 1234"
13+
"start:autocomplete": "PARCEL_AUTOINSTALL=false parcel serve ./places-autocomplete/index.html --dist-dir public --port 1234",
14+
"start:directions": "PARCEL_AUTOINSTALL=false parcel serve ./directions/index.html --dist-dir public --port 1234"
1415
},
1516
"license": "MIT",
1617
"dependencies": {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"start:map-with-markers": "cd examples && npm run clean-examples && npm i && npm run start:map-markers",
2727
"start:geocoding-example": "cd examples && npm run clean-examples && npm i && npm run start:geocoding",
2828
"start:places-example": "cd examples && npm run clean-examples && npm run start:places",
29-
"start:autocomplete-example": "cd examples && npm run clean-examples && npm run start:autocomplete"
29+
"start:autocomplete-example": "cd examples && npm run clean-examples && npm run start:autocomplete",
30+
"start:directions-example": "cd examples && npm run clean-examples && npm run start:directions"
3031
},
3132
"keywords": [
3233
"React hooks",

0 commit comments

Comments
 (0)