Skip to content

Commit b0ab3ac

Browse files
authored
Merge branch 'develop' into chore/workflows
2 parents 4f16b8f + 55aa9cc commit b0ab3ac

File tree

17 files changed

+244
-36
lines changed

17 files changed

+244
-36
lines changed

.pretiierrc renamed to .prettierrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"singleQuote": true,
77
"trailingComma": "none",
88
"bracketSpacing": false,
9-
"jsxBracketSameLine": true,
9+
"bracketSameLine": true,
1010
"arrowParens": "avoid",
1111
"parser": "typescript"
12-
}
12+
}

examples/geocoding/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Basic Google Maps Setup Example
2+
3+
This is an example setup to show the usage of the **useGeocoder 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+
At the root of the repository, run:
10+
11+
```shell
12+
npm run start:geocoding-example
13+
```
14+
15+
**NOTE**:
16+
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:
17+
18+
`GOOGLE_MAPS_API_KEY="<YOUR API KEY HERE>"`
19+
20+
An example can be found in `.env.example`.
21+
22+
## Output
23+
24+
The project will start at [localhost:1234](http://localhost:1234) and show a Google Map where the user can click on the map and the coordinates will be reversed geocoded into a human readable address.
25+
26+
![image](https://user-images.githubusercontent.com/39244966/196205574-b0c318a8-1e55-4c52-a18f-43fc13d2d903.png)

examples/geocoding/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 GeocoderExample from './components/geocoder-example/geocoder-example';
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: 8,
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+
<GeocoderExample />
39+
</div>
40+
</GoogleMapProvider>
41+
</React.StrictMode>
42+
);
43+
};
44+
45+
export default App;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {useEffect, useState} from 'react';
2+
import {useGeocoder, useGoogleMap} from '@ubilabs/google-maps-react-hooks';
3+
4+
const GeocoderExample = () => {
5+
const {map} = useGoogleMap();
6+
7+
// Get the geocoder from the useGeocoder hook
8+
const geocoder = useGeocoder();
9+
10+
const initialPosition = {lat: 51.08998021141488, lng: 10.627828045134935};
11+
12+
const [marker, setMarker] = useState<google.maps.Marker | null>(null);
13+
const [infoWindow, setInfoWindow] = useState<google.maps.InfoWindow | null>(
14+
null
15+
);
16+
17+
useEffect(() => {
18+
if (!map) {
19+
return () => {};
20+
}
21+
22+
// Add an initial marker
23+
if (!marker) {
24+
const initialMarker = new google.maps.Marker({
25+
map,
26+
position: initialPosition
27+
});
28+
29+
setMarker(initialMarker);
30+
31+
return () => {};
32+
}
33+
34+
// Add an initial infowindow
35+
if (!infoWindow) {
36+
const initialInfoWindow = new google.maps.InfoWindow({
37+
content:
38+
'Click somewhere on the map to reverse geocode the position to an address.',
39+
position: initialPosition
40+
});
41+
42+
setInfoWindow(initialInfoWindow);
43+
initialInfoWindow.open(map, marker);
44+
45+
return () => {};
46+
}
47+
48+
// Click on the map and open an infowindow with the reversed geocoded address.
49+
map.addListener('click', (mapsMouseEvent: google.maps.MapMouseEvent) => {
50+
// Use the geocoder to reverse geocode the position from the map
51+
// and add the address as content of the infowindow
52+
geocoder?.geocode(
53+
{location: mapsMouseEvent.latLng},
54+
(
55+
results: google.maps.GeocoderResult[],
56+
status: google.maps.GeocoderStatus
57+
) => {
58+
if (status === 'OK') {
59+
const position = results[0].geometry.location;
60+
const formattedAddress = results[0].formatted_address;
61+
62+
marker.setPosition(position);
63+
64+
infoWindow.setPosition(position);
65+
infoWindow.setContent(formattedAddress);
66+
67+
map.setCenter(results[0].geometry.location);
68+
} else {
69+
// eslint-disable-next-line no-console
70+
console.log(
71+
`Geocode was not successful for the following reason: ${status}`
72+
);
73+
}
74+
}
75+
);
76+
});
77+
78+
// Clean up listeners and remove marker and infowindow instances
79+
return () => {
80+
google.maps.event.clearListeners(map, 'click');
81+
};
82+
}, [map, infoWindow, marker]);
83+
84+
return null;
85+
};
86+
87+
export default GeocoderExample;
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/geocoding/index.html

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

examples/geocoding/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/geocoding/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/google-map-with-markers/components/map-markers/map-markers.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const MapMarkers: VoidFunctionComponent = (): null => {
5858
if (map) {
5959
const initialBounds = new google.maps.LatLngBounds();
6060

61-
const museumMarkers: Array<google.maps.Marker> = museums.map((museum) => {
61+
const museumMarkers: Array<google.maps.Marker> = museums.map(museum => {
6262
const {position, name} = museum;
6363

6464
const markerOptions: google.maps.MarkerOptions = {

0 commit comments

Comments
 (0)