I’m developing a React site and I want to incorporate the Google Maps API to display bars associated with specific football clubs on a map. Essentially, I would like users to input a country and a football club, and the site should show markers or pins on the map indicating the locations of bars associated with that club in the selected country.
I have already set up the basic structure of my React site and have successfully integrated the Google Maps API to display a default map. However, I’m unsure about how to proceed with the dynamic display of bars based on user input.
Here’s an overview of my current setup:
I have the MapComponent.jsx file:
import { GoogleMap, Marker } from "@react-google-maps/api";
// Array of bar data with country, football club, and coordinates
const bars = [
{ country: 'Australia', club: 'Arsenal', lat: -33.885055433597, lng: 151.20966156719123 },
{ country: 'Australia', club: 'Manchester United', lat: -34.94636965495918, lng: 138.62657079153266 },
{ country: 'Canada', club: 'Liverpool', lat: 49.28637807745695, lng: -123.11695651719553 },
{ country: 'Germany', club: 'Bayern Munich', lat: 48.175002206190136, lng: 11.592748746031473 },
{ country: 'Japan', club: 'Manchester United', lat: 53.4631, lng: -2.2913 },
{ country: 'Japan', club: 'Liverpool', lat: 35.64857860594483, lng: 139.71303475978445 },
{ country: 'South Korea', club: 'Manchester United', lat: 53.4631, lng: -2.2913 },
{ country: 'South Korea', club: 'Tottenham Hotspurs', lat: 37.552079094148446, lng: 126.9226752350085 },
{ country: 'South Korea', club: 'Liverpool', lat: 37.56004882603966, lng: 126.92511388465444 },
{ country: 'United States', club: 'Arsenal', lat: 40.74496672417892, lng: -73.99249257407631 },
{ country: 'United States', club: 'Arsenal', lat: 32.74202567965132, lng: -117.1289996550291 },
{ country: 'United States', club: 'Liverpool', lat: 34.007969428759544, lng: -118.41245218835955 },
{ country: 'United States', club: 'Manchester United', lat: 39.750624531403766, lng: -104.9853437709032 },
// Add more bar data as needed
];
const mapOptions = {
mapId: "cb21e6acbb31cd1",
scrollwheel: true, // Enable scroll wheel zooming
minZoom: 2,
maxZoom: 20
};
function MapComponent({ center, zoom }) {
return (
<GoogleMap
mapContainerStyle={{ width: "75%", height: "600px", margin: "0 auto" }}
mapTypeId=""
center={center}
zoom={zoom}
options={mapOptions}
>
{/* Render markers here */}
</GoogleMap>
);
}
export default MapComponent;
and I have a Map.jsx file:
import React, { useState } from "react";
import NavBar from "./NavBar";
import Image from "../Haaland.png";
import MapComponent from "./MapComponent";
import { LoadScript } from "@react-google-maps/api";
import Footer from "./Footer";
import CountryAutoComplete from "./CountryAutoComplete";
import ClubAutocomplete from "./ClubAutoComplete";
function Map() {
// Set initial center and zoom level
const center = { lat: 0, lng: 0 };
const zoom = 3;
return (
<>
<NavBar />
<section id="InputSection">
<div className="InputDiv">
<h1 className="MapPageHeaderText">Type in your Location and Club</h1>
<div className="CountryDiv">
<i
class="fa-solid fa-earth-europe"
style={{
color: "white",
fontSize: "xx-large",
marginBottom: "15px",
marginRight: "10px",
}}
></i>
<CountryAutoComplete
suggestions={[
"Australia",
"Belgium",
"Canada",
"England",
"France",
"Germany",
"Ireland",
"Italy",
"Japan",
"Spain",
"South Korea",
"Sweden",
"United States of America",
]}
/>
</div>
<div className="ClubDiv">
<i
class="fa-regular fa-futbol"
style={{
color: "white",
fontSize: "xx-large",
marginBottom: "15px",
marginRight: "10px",
}}
></i>
<ClubAutocomplete
suggestions={[
"Arsenal",
"AC Milan",
"Barcelona FC",
"Bayern Munich",
"Chelsea",
"Juventus",
"Manchester City",
"Manchester United",
"Liverpool",
"Real Madrid",
"Tottenham",
"West Ham United",
]}
/>
</div>
<button
type="submit"
className="SearchButton"
style={{ width: "300px", height: "50px" }}
>
Search
</button>
</div>
<div className="ImageDiv">
<img
className="PlayerImage2"
src={Image}
alt="Erling Haaland holding Premier League Trophy"
/>
</div>
</section>
<div className="MapSection">
<h1 className="MapDivText">Results</h1>
<LoadScript googleMapsApiKey="AIzaSyC5TQRQXVpsBHskHxxifLJfl9w53tp4hqo">
<div
style={{
marginLeft: "auto",
marginRight: "auto",
marginTop: "50px",
}}
>
<MapComponent center={center} zoom={zoom} />
</div>
</LoadScript>
<a href="/map">
<button className="ResetButton">Reset</button>
</a>
</div>
<Footer />
</>
);
}
export default Map;
Any pointers in the right direction are appreciated.