Skip to content

Commit be9d081

Browse files
authored
Merge pull request #65 from ubilabs/feat/add-places-example
feat(examples): add places example
2 parents e4b5833 + 86fa2d6 commit be9d081

File tree

10 files changed

+204
-4
lines changed

10 files changed

+204
-4
lines changed

examples/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
"clean-examples": "rm -rf public .parcel-cache",
99
"start:map": "PARCEL_AUTOINSTALL=false parcel serve ./basic-google-map/index.html --dist-dir public --port 1234",
1010
"start:map-markers": "PARCEL_AUTOINSTALL=false parcel serve ./google-map-with-markers/index.html --dist-dir public --port 1234",
11-
"start:autocomplete": "PARCEL_AUTOINSTALL=false parcel serve ./places-autocomplete/index.html --dist-dir public --port 1234",
12-
"start:geocoding": "PARCEL_AUTOINSTALL=false parcel serve ./geocoding/index.html --dist-dir public --port 1234"
11+
"start:geocoding": "PARCEL_AUTOINSTALL=false parcel serve ./geocoding/index.html --dist-dir public --port 1234",
12+
"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"
1314
},
1415
"license": "MIT",
1516
"dependencies": {

examples/places/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Basic Google Maps Setup Example
2+
3+
This is an example setup to show a **Google Map with place** using the usePlacesService hook 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:places-example
24+
```
25+
26+
27+
**NOTE**:
28+
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:
29+
30+
`GOOGLE_MAPS_API_KEY="<YOUR API KEY HERE>"`
31+
32+
An example can be found in `.env.example`.
33+
34+
## Output
35+
36+
The project will start at [localhost:1234](http://localhost:1234) and show a Google Map that highlights all nearby Cafes to the central station in Hamburg, and their opening hours. Information is retrieved from Places Service.
37+
38+
![image](https://user-images.githubusercontent.com/39244966/196244324-8d761a8f-25d1-4e87-adb3-9d0d66f97b66.png)

examples/places/app.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
6+
import {GOOGLE_MAPS_API_KEY} from '../constants';
7+
8+
import './main.module.css';
9+
import PlacesExample from './components/places-example/places-example';
10+
11+
const mapOptions = {
12+
center: {lat: 53.5582447, lng: 9.647645},
13+
zoom: 20,
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+
// Add library places
36+
libraries={['places']}
37+
>
38+
<div id="container">
39+
<MapCanvas ref={mapRef} />
40+
<PlacesExample />
41+
</div>
42+
</GoogleMapProvider>
43+
</React.StrictMode>
44+
);
45+
};
46+
47+
export default App;
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;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {useEffect} from 'react';
2+
3+
import {
4+
useGoogleMap,
5+
usePlacesService
6+
} from '@ubilabs/google-maps-react-hooks';
7+
8+
const PlacesExample = () => {
9+
const {map} = useGoogleMap();
10+
11+
// Get the places service from the usePlacesService hook
12+
const service = usePlacesService();
13+
14+
useEffect(() => {
15+
if (!map || !service) {
16+
return;
17+
}
18+
19+
const bounds = new google.maps.LatLngBounds();
20+
21+
const request = {
22+
location: {lat: 53.550481787761306, lng: 9.992336490896136},
23+
radius: 500,
24+
type: 'cafe'
25+
};
26+
27+
function callback(
28+
results: google.maps.places.PlaceResult[],
29+
status: google.maps.places.PlacesServiceStatus
30+
) {
31+
if (status === google.maps.places.PlacesServiceStatus.OK) {
32+
for (let index = 0; index < results.length; index++) {
33+
const name = results[index].name;
34+
const position = results[index].geometry?.location;
35+
const openingHours = results[index].opening_hours;
36+
37+
const isOpenStatus = openingHours ? 'Is open' : 'Closed';
38+
39+
if (!position) {
40+
return;
41+
}
42+
43+
const marker = new google.maps.Marker({
44+
map,
45+
position
46+
});
47+
48+
map?.fitBounds(bounds.extend(position));
49+
50+
const infowindow = new google.maps.InfoWindow({
51+
position,
52+
content: `<b>${name}</b> is ${isOpenStatus}`
53+
});
54+
55+
infowindow.open(map, marker);
56+
}
57+
}
58+
}
59+
60+
service.nearbySearch(request, callback);
61+
}, [map]);
62+
63+
return null;
64+
};
65+
66+
export default PlacesExample;

examples/places/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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>Google Maps with places, using the usePlacesService hook with the Google Maps React Hooks.</title>
10+
<meta
11+
name="description"
12+
content="Google Maps with places, using the usePlacesService hook with the Google Maps React Hooks."
13+
/>
14+
</head>
15+
<body>
16+
<div id="app"></div>
17+
<script type="module" src="./index.tsx"></script>
18+
</body>
19+
</html>

examples/places/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/places/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+
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
"postversion": "git push -u origin chore/release-${npm_package_version} && git push --tags --no-verify",
2525
"start:sample-map": "cd examples && npm run clean-examples && npm i && npm run start:map",
2626
"start:map-with-markers": "cd examples && npm run clean-examples && npm i && npm run start:map-markers",
27-
"start:autocomplete-example": "cd examples && npm run clean-examples && npm run start:autocomplete",
28-
"start:geocoding-example": "cd examples && npm run clean-examples && npm i && npm run start:geocoding"
27+
"start:geocoding-example": "cd examples && npm run clean-examples && npm i && npm run start:geocoding",
28+
"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"
2930
},
3031
"keywords": [
3132
"React hooks",

0 commit comments

Comments
 (0)