From 1aa77bea2b94c11d9971f305f82781ae0035a370 Mon Sep 17 00:00:00 2001 From: lphan48 Date: Mon, 18 Mar 2024 18:15:54 -0400 Subject: [PATCH 1/8] First commit --- apps/frontend/src/components/map/Map.tsx | 144 +++++++++--------- .../src/components/mapIcon/PopupBox.tsx | 20 ++- 2 files changed, 89 insertions(+), 75 deletions(-) diff --git a/apps/frontend/src/components/map/Map.tsx b/apps/frontend/src/components/map/Map.tsx index 11575fd..97b41aa 100644 --- a/apps/frontend/src/components/map/Map.tsx +++ b/apps/frontend/src/components/map/Map.tsx @@ -10,28 +10,29 @@ import generateTriangleSVG from '../../images/markers/triangle'; import generateStarSVG from '../../images/markers/star'; import generatePentagonSVG from '../../images/markers/pentagon'; - - const MapDiv = styled.div` height: 100%; `; - -function filterMarkers(selectedFeatures: string[], selectedStatuses: string[], markers: google.maps.Marker[], map: google.maps.Map) { +function filterMarkers( + selectedFeatures: string[], + selectedStatuses: string[], + markers: google.maps.Marker[], + map: google.maps.Map, +) { let tempMarkers: google.maps.Marker[] = []; if (selectedFeatures.length === 0) { markers.forEach((marker: google.maps.Marker) => { marker.setMap(map); - }) + }); tempMarkers = markers; - } - else { + } else { markers.forEach((marker: google.maps.Marker) => marker.setMap(null)); markers.forEach((marker: google.maps.Marker) => { - const featureType = marker.get("featureType"); + const featureType = marker.get('featureType'); if (selectedFeatures.includes(featureType)) { marker.setMap(map); - tempMarkers.push(marker) + tempMarkers.push(marker); } }); } @@ -40,13 +41,12 @@ function filterMarkers(selectedFeatures: string[], selectedStatuses: string[], m if (selectedStatuses.length === 0) { tempMarkers.forEach((marker: google.maps.Marker) => { marker.setMap(map); - }) - } - else { + }); + } else { tempMarkers.forEach((marker: google.maps.Marker) => marker.setMap(null)); tempMarkers.forEach((marker: google.maps.Marker) => { - const status = marker.get("status"); - console.log(selectedStatuses) + const status = marker.get('status'); + console.log(selectedStatuses); if (selectedStatuses.includes(status)) { marker.setMap(map); } @@ -54,16 +54,14 @@ function filterMarkers(selectedFeatures: string[], selectedStatuses: string[], m } } - interface MapProps { readonly zoom: number; selectedFeatures: string[]; selectedStatuses: string[]; } - function randomizeStatus(): string { - const statuses = ["Available", "Adopted"]; + const statuses = ['Available', 'Adopted']; return statuses[Math.floor(Math.random() * statuses.length)]; } @@ -72,15 +70,11 @@ const Map: React.FC = ({ selectedFeatures, selectedStatuses, }) => { - - const mapRef = useRef(null); const [markers, setMarkers] = useState([]); - let map: google.maps.Map; - useEffect(() => { if (mapRef.current) { loader.load().then(() => { @@ -103,78 +97,90 @@ const Map: React.FC = ({ }, }); - // sets the style for the boundary - const featureLayer = map.getFeatureLayer(google.maps.FeatureType.LOCALITY); + const featureLayer = map.getFeatureLayer( + google.maps.FeatureType.LOCALITY, + ); const featureStyleOptions: google.maps.FeatureStyleOptions = { strokeColor: '#50B0E6', strokeOpacity: 1.0, strokeWeight: 3.0, fillColor: '#50B0E6', - fillOpacity: 0.3 + fillOpacity: 0.3, }; featureLayer.style = (options) => { const feature = options.feature as google.maps.PlaceFeature; - if (feature.placeId === BOSTON_PLACE_ID) { // Place ID for Boston + if (feature.placeId === BOSTON_PLACE_ID) { + // Place ID for Boston return featureStyleOptions; } }; let currentInfoWindow: google.maps.InfoWindow | null = null; - const markersArray: google.maps.Marker[] = []; - - SITES.forEach(markerInfo => { - - const types = ['Rain Garden', 'Bioswale', 'Bioretention', 'Porous Paving', 'Tree Trench/Pit', 'Green Roof/Planter'] - - if (markerInfo["Lat"] != null && markerInfo["Long"] != null && types.includes(markerInfo['Symbol Type'])) { - - const status = randomizeStatus() - - let typeColor = ""; + SITES.forEach((markerInfo) => { + const types = [ + 'Rain Garden', + 'Bioswale', + 'Bioretention', + 'Porous Paving', + 'Tree Trench/Pit', + 'Green Roof/Planter', + ]; + + if ( + markerInfo['Lat'] != null && + markerInfo['Long'] != null && + types.includes(markerInfo['Symbol Type']) + ) { + const status = randomizeStatus(); + + let typeColor = ''; if (status === 'Available') { - typeColor = "#2D6A4F" - } - else if (status === 'Adopted') { - typeColor = "#FB4D42" + typeColor = '#2D6A4F'; + } else if (status === 'Adopted') { + typeColor = '#FB4D42'; } - let tempIcon = ""; + let tempIcon = ''; let iconFunc = null; if (markerInfo['Symbol Type'] === 'Rain Garden') { tempIcon = generateSquareSVG(typeColor); iconFunc = generateSquareSVG; - } - else if (markerInfo['Symbol Type'] === 'Bioswale') { + } else if (markerInfo['Symbol Type'] === 'Bioswale') { tempIcon = generateTriangleSVG(typeColor); iconFunc = generateTriangleSVG; - } - else if (markerInfo['Symbol Type'] === 'Bioretention') { + } else if (markerInfo['Symbol Type'] === 'Bioretention') { tempIcon = generateCircleSVG(typeColor); iconFunc = generateCircleSVG; - } - else if (markerInfo['Symbol Type'] === 'Porous Paving') { + } else if (markerInfo['Symbol Type'] === 'Porous Paving') { tempIcon = generateDiamondSVG(typeColor); iconFunc = generateDiamondSVG; - } - else if (markerInfo['Symbol Type'] === 'Tree Trench/Pit') { + } else if (markerInfo['Symbol Type'] === 'Tree Trench/Pit') { tempIcon = generateStarSVG(typeColor); iconFunc = generateStarSVG; - } - else if (markerInfo['Symbol Type'] === 'Green Roof/Planter') { + } else if (markerInfo['Symbol Type'] === 'Green Roof/Planter') { tempIcon = generatePentagonSVG(typeColor); iconFunc = generatePentagonSVG; } - const typeIcon = `data:image/svg+xml;utf8,${encodeURIComponent(tempIcon)}`; + const typeIcon = `data:image/svg+xml;utf8,${encodeURIComponent( + tempIcon, + )}`; const infoWindow = new google.maps.InfoWindow({ - content: createPopupBoxContent(markerInfo['Asset Name'], markerInfo['Address'], 'Available', markerInfo['Symbol Type'], typeColor, iconFunc as (color: string) => string), + content: createPopupBoxContent( + markerInfo['Asset Name'], + markerInfo['Address'], + 'Available', + markerInfo['Symbol Type'], + typeColor, + iconFunc as (color: string) => string, + ), }); const customIcon = { @@ -186,13 +192,16 @@ const Map: React.FC = ({ }; const marker: google.maps.Marker = new google.maps.Marker({ - position: { lat: Number(markerInfo["Lat"]), lng: markerInfo["Long"] }, + position: { + lat: Number(markerInfo['Lat']), + lng: markerInfo['Long'], + }, map: map, - icon: customIcon + icon: customIcon, }); - marker.set("featureType", markerInfo['Symbol Type']); - marker.set("status", status); + marker.set('featureType', markerInfo['Symbol Type']); + marker.set('status', status); marker.addListener('click', () => { if (currentInfoWindow) { @@ -203,11 +212,10 @@ const Map: React.FC = ({ }); markersArray.push(marker); } - - }) + }); setMarkers(markersArray); - console.log(selectedFeatures) + console.log(selectedFeatures); filterMarkers(selectedFeatures, selectedStatuses, markersArray, map); const input = document.getElementById('pac-input') as HTMLInputElement; @@ -215,9 +223,7 @@ const Map: React.FC = ({ const autocomplete = new google.maps.places.Autocomplete(input); autocomplete.bindTo('bounds', map); - autocomplete.addListener('place_changed', () => { - // marker.setVisible(false); const place = autocomplete.getPlace(); @@ -238,19 +244,17 @@ const Map: React.FC = ({ }); }); } - - - }, [zoom, selectedFeatures, selectedStatuses]); - - return (
- +
); }; - -export default Map; \ No newline at end of file +export default Map; diff --git a/apps/frontend/src/components/mapIcon/PopupBox.tsx b/apps/frontend/src/components/mapIcon/PopupBox.tsx index 32ea1fc..8149a2e 100644 --- a/apps/frontend/src/components/mapIcon/PopupBox.tsx +++ b/apps/frontend/src/components/mapIcon/PopupBox.tsx @@ -1,14 +1,24 @@ -export function createPopupBoxContent(name: string, location: string, status: string, type: string, color: string, svgFunction: (color: string) => string) { +import { useState } from 'react'; +export function createPopupBoxContent( + name: string, + location: string, + status: string, + type: string, + color: string, + svgFunction: (color: string) => string, +) { return `