Skip to content

Commit 6495e93

Browse files
committed
Adding Google Maps
1 parent 977ccf9 commit 6495e93

File tree

19 files changed

+14776
-5571
lines changed

19 files changed

+14776
-5571
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ yarn-error.log*
3333
# vercel
3434
.vercel
3535
.env
36+
37+
# Local Netlify folder
38+
.netlify

component/ABComponent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import styles from './ABComponent.module.css'
2+
import styles from '../styles/ABComponent.module.css'
33

44
const sectionStyle = (backgroundColor) => (
55
{

component/Map.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useEffect, useState , useRef} from "react";
2+
import styles from '../styles/map.module.css';
3+
4+
const Map = ({ mapCords }) => {
5+
const ref = useRef();
6+
const [map, setMap] = useState();
7+
8+
const ZOOM = 9;
9+
10+
useEffect(() => {
11+
if (ref.current && !map) {
12+
const newMap = new window.google.maps.Map(ref.current, {
13+
zoom: ZOOM,
14+
center: mapCords,
15+
});
16+
17+
const mapOverlay = new google.maps.ImageMapType({
18+
getTileUrl: function (coord, zoom) {
19+
return `https://tiles.aqicn.org/tiles/usepa-aqi/${zoom}/${coord.x}/${coord.y}.png?token=${process.env.NEXT_PUBLIC_AQICN_API_KEY}`;
20+
},
21+
name: 'Air Quality',
22+
});
23+
newMap.overlayMapTypes.insertAt(0, mapOverlay);
24+
25+
setMap(newMap);
26+
}
27+
}, [ref, map, mapCords]);
28+
29+
return <div ref={ref} className={styles.map}></div>;
30+
}
31+
32+
export default Map;

component/MapContainer.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Map from "../component/Map";
2+
import { Wrapper } from '@googlemaps/react-wrapper'
3+
4+
import styles from '../styles/map.module.css';
5+
6+
const MapContainer = ({ mapCords, address }) => {
7+
8+
const apiKey = process.env.NEXT_PUBLIC_GOOGLE_API_KEY;
9+
console.log('MapContainer', apiKey, mapCords)
10+
11+
return (
12+
<section className={styles.container}>
13+
<Wrapper apiKey={apiKey}>
14+
<Map mapCords={mapCords} />
15+
</Wrapper>
16+
</section>
17+
);
18+
}
19+
20+
export default MapContainer;

functions/admin.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

functions/data.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

middleware.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { MiddlewareRequest } from '@netlify/next';
2+
3+
export async function middleware(nextRequest) {
4+
5+
if (nextRequest.nextUrl.pathname !== '/signup') {
6+
return;
7+
}
8+
9+
const middlewareRequest = new MiddlewareRequest(nextRequest);
10+
const response = await middlewareRequest.next();
11+
12+
const GOOGLE_API_KEY = Deno.env.get('NEXT_PUBLIC_GOOGLE_API_KEY');
13+
const address = nextRequest.cookies.get('state');
14+
15+
const api = new URL('https://maps.googleapis.com/maps/api/geocode/json');
16+
17+
api.searchParams.set('address', address);
18+
api.searchParams.set('key', GOOGLE_API_KEY);
19+
20+
const res = await fetch(api.toString());
21+
const data = await res.json();
22+
const [result] = data.results;
23+
24+
if (!result || !result.geometry || !result.geometry.location) {
25+
return;
26+
}
27+
28+
response.setPageProp('address', address);
29+
response.setPageProp('mapCords', result.geometry.location);
30+
31+
return response;
32+
}

netlify.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[build]
2-
command = "npm run build"
3-
functions = "dist-functions"
2+
command = "npm run build"
3+
4+
[build.environment]
5+
NEXT_USE_NETLIFY_EDGE = "true"
46

57
[[plugins]]
6-
package = "@netlify/plugin-nextjs"
8+
package = "@netlify/plugin-nextjs"

next.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ module.exports = {
1313
},
1414
images: {
1515
domains: ['localhost', 'optimizely-demo.netlify.app'],
16-
},
16+
}
1717
}

0 commit comments

Comments
 (0)