update PoolMap.tsx

This commit is contained in:
Michael Fatemi 2021-04-11 14:30:02 -04:00
parent 4042d4038e
commit b2db90949f
2 changed files with 32 additions and 23 deletions

View File

@ -200,7 +200,7 @@ export default function Pool({
</Button> </Button>
)} )}
<hr /> <hr />
<PoolMap pool={pool._id} /> <PoolMap pool={pool} />
<hr /> <hr />
<Textarea <Textarea
cols={80} cols={80}

View File

@ -1,30 +1,39 @@
import GoogleMapReact from 'google-map-react'; import GoogleMapReact from 'google-map-react';
import { useCallback } from 'react';
import { makeAPIGetCall } from '../api/utils'; import { makeAPIGetCall } from '../api/utils';
const position = { lat: 38.817, lng: -77.1679 }; const position = { lat: 38.817, lng: -77.1679 };
export default function PoolMap(pool: any) { export type AddressMarker = {
const renderMarkers = (map: any, maps: any) => { user: string;
pool: string;
location: string;
lat: string;
lng: string;
};
export default function PoolMap({ pool }: { pool: Carpool.Pool }) {
const renderMarkers = useCallback(
(map: any, maps: any) => {
let markers: any[] = []; let markers: any[] = [];
makeAPIGetCall(`/addresses/pool/${pool.pool}`).then((res) => { makeAPIGetCall(`/addresses/pool/${pool._id}`).then((res) => {
if (res.data.data) { if (res.data.data) {
res.data.data.forEach((element: any) => { res.data.data.forEach((element: AddressMarker) => {
let marker = new maps.Marker({ let marker = new maps.Marker({
position: { position: {
lat: parseFloat(element.lat), lat: parseFloat(element.lat),
lng: parseFloat(element.lng), lng: parseFloat(element.lng),
}, },
map, map,
title: 'Hello World!', title: 'Anonymous Address',
}); });
markers.push(marker); markers.push(marker);
}); });
} }
}); });
},
console.log(markers); [pool._id]
return markers; );
};
return ( return (
<div style={{ height: '50vh', width: '100%' }}> <div style={{ height: '50vh', width: '100%' }}>
@ -36,7 +45,7 @@ export default function PoolMap(pool: any) {
defaultZoom={11} defaultZoom={11}
yesIWantToUseGoogleMapApiInternals yesIWantToUseGoogleMapApiInternals
onGoogleApiLoaded={({ map, maps }: any) => renderMarkers(map, maps)} onGoogleApiLoaded={({ map, maps }: any) => renderMarkers(map, maps)}
></GoogleMapReact> />
</div> </div>
); );
} }