Skip to content

Commit

Permalink
Merge pull request #11 from 392-f24/map
Browse files Browse the repository at this point in the history
Map
  • Loading branch information
VihaanShah26 authored Nov 8, 2024
2 parents bc33139 + 56c0284 commit 4826943
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 33 deletions.
93 changes: 68 additions & 25 deletions src/MapComponent.jsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,85 @@
import React, { useEffect, useRef, useState } from "react";
import { useNavigate } from 'react-router-dom';

const MapComponent = ( {cafes} ) => {
const MapComponent = ({ cafes }) => {
const mapRef = useRef(null);
const defaultLocation = { lat: 42.0451, lng: -87.6877 }; // Default to a location, e.g., Chicago

const defaultLocation = { lat: 42.0451, lng: -87.6877 };
const [map, setMap] = useState(null);
const navigate = useNavigate();

useEffect(() => {
if (!mapRef.current) return;

// Initialize map
const map = new window.google.maps.Map(mapRef.current, {
center: defaultLocation,
zoom: 14,
mapId: 'ae95546a5be3631',
disableDefaultUI: true,
});
const initMap = async () => {
const { Map } = await window.google.maps.importLibrary("maps");

Check failure on line 14 in src/MapComponent.jsx

View workflow job for this annotation

GitHub Actions / Build and unit test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'maps') ❯ initMap src/MapComponent.jsx:14:43 ❯ src/MapComponent.jsx:27:5 ❯ commitHookEffectListMount node_modules/react-dom/cjs/react-dom.development.js:23189:26 ❯ commitPassiveMountOnFiber node_modules/react-dom/cjs/react-dom.development.js:24970:11 ❯ commitPassiveMountEffects_complete node_modules/react-dom/cjs/react-dom.development.js:24930:9 ❯ commitPassiveMountEffects_begin node_modules/react-dom/cjs/react-dom.development.js:24917:7 ❯ commitPassiveMountEffects node_modules/react-dom/cjs/react-dom.development.js:24905:3 ❯ flushPassiveEffectsImpl node_modules/react-dom/cjs/react-dom.development.js:27078:3 ❯ flushPassiveEffects node_modules/react-dom/cjs/react-dom.development.js:27023:14 ❯ node_modules/react-dom/cjs/react-dom.development.js:26808:9 This error originated in "src/App.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "should show all the headings". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 14 in src/MapComponent.jsx

View workflow job for this annotation

GitHub Actions / Build and unit test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'maps') ❯ initMap src/MapComponent.jsx:14:43 ❯ src/MapComponent.jsx:27:5 ❯ commitHookEffectListMount node_modules/react-dom/cjs/react-dom.development.js:23189:26 ❯ commitPassiveMountOnFiber node_modules/react-dom/cjs/react-dom.development.js:24970:11 ❯ commitPassiveMountEffects_complete node_modules/react-dom/cjs/react-dom.development.js:24930:9 ❯ commitPassiveMountEffects_begin node_modules/react-dom/cjs/react-dom.development.js:24917:7 ❯ commitPassiveMountEffects node_modules/react-dom/cjs/react-dom.development.js:24905:3 ❯ flushPassiveEffectsImpl node_modules/react-dom/cjs/react-dom.development.js:27078:3 ❯ flushPassiveEffects node_modules/react-dom/cjs/react-dom.development.js:27023:14 ❯ node_modules/react-dom/cjs/react-dom.development.js:26808:9 This error originated in "src/Cafes_Nearby.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "src/Cafes_Nearby.test.jsx". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.

Check failure on line 14 in src/MapComponent.jsx

View workflow job for this annotation

GitHub Actions / Build and unit test

Unhandled error

TypeError: Cannot read properties of undefined (reading 'maps') ❯ initMap src/MapComponent.jsx:14:43 ❯ src/MapComponent.jsx:27:5 ❯ commitHookEffectListMount node_modules/react-dom/cjs/react-dom.development.js:23189:26 ❯ commitPassiveMountOnFiber node_modules/react-dom/cjs/react-dom.development.js:24970:11 ❯ commitPassiveMountEffects_complete node_modules/react-dom/cjs/react-dom.development.js:24930:9 ❯ commitPassiveMountEffects_begin node_modules/react-dom/cjs/react-dom.development.js:24917:7 ❯ commitPassiveMountEffects node_modules/react-dom/cjs/react-dom.development.js:24905:3 ❯ flushPassiveEffectsImpl node_modules/react-dom/cjs/react-dom.development.js:27078:3 ❯ flushPassiveEffects node_modules/react-dom/cjs/react-dom.development.js:27023:14 ❯ node_modules/react-dom/cjs/react-dom.development.js:26808:9 This error originated in "src/App.test.jsx" test file. It doesn't mean the error was thrown inside the file itself, but while it was running. The latest test that might've caused the error is "show sign in/sign out button". It might mean one of the following: - The error was thrown, while Vitest was running this test. - If the error occurred after the test had been completed, this was the last documented test before it was thrown.
const { AdvancedMarkerElement } = await window.google.maps.importLibrary("marker");

// Add markers for each cafe
cafes.forEach((cafe) => {
const marker = new window.google.maps.Marker({
position: { lat: cafe.lat, lng: cafe.lng },
map: map,
title: cafe.name,
const newMap = new Map(mapRef.current, {
center: defaultLocation,
zoom: 14,
mapId: 'ae95546a5be3631',
disableDefaultUI: true,
});

// Info window with cafe name
const infoWindow = new window.google.maps.InfoWindow({
content: `<div><strong>${cafe.name}</strong></div>`,
});
setMap(newMap);
};

initMap();
}, []);

useEffect(() => {
if (!map || !cafes.length) return;

// Show info window on marker click
marker.addListener("click", () => {
infoWindow.open(map, marker);
const bounds = new window.google.maps.LatLngBounds();
const geocoder = new window.google.maps.Geocoder();

cafes.forEach((cafe) => {
geocoder.geocode({ address: cafe.vicinity }, (results, status) => {
if (status === 'OK' && results[0]) {
const position = results[0].geometry.location;

const marker = new window.google.maps.marker.AdvancedMarkerElement({
map,
position,
title: cafe.name,
});

const infoWindowContent = `
<div>
<strong>${cafe.name}</strong>
<br/>
<button id="goto-${cafe.placeId}" style="margin-top: 10px;">Go to Cafe Page</button>
</div>
`;

const infoWindow = new window.google.maps.InfoWindow({
content: infoWindowContent,
});

marker.addListener("click", () => {
infoWindow.open(map, marker);

// We need to wait for the InfoWindow to be added to the DOM
setTimeout(() => {
const button = document.getElementById(`goto-${cafe.placeId}`);
if (button) {
button.addEventListener('click', () => {
navigate(`/cafe/${cafe.placeId}`);
});
}
}, 0);
});

bounds.extend(position);
map.fitBounds(bounds);
} else {
console.error(`Geocoding failed for ${cafe.name}: ${status}`);
}
});
});
}, []);
}, [cafes, map, navigate]);

return <div ref={mapRef} style={{ width: "100%", height: "100%" }} />;
};

export default MapComponent;
export default MapComponent;
9 changes: 1 addition & 8 deletions src/pages/LandingPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,11 @@ const LandingPage = () => {
console.log(cafes);
}, [cafes]);

const cafesMap = [
{ name: "Cafe Blue", lat: 42.046, lng: -87.688 },
{ name: "Green Bean Cafe", lat: 42.048, lng: -87.684 },
{ name: "Java Lounge", lat: 42.044, lng: -87.690 },
];

return (
<div className="App">
<Banner cafes={cafes} />
{/* Integrate MapComponent with hardcoded data */}
<div style={{ height: '600px', width: '100%'}}>
<MapComponent cafes={cafesMap} />
<MapComponent cafes={cafes} />
</div>
</div>
);
Expand Down

0 comments on commit 4826943

Please sign in to comment.