Skip to content
Advertisement

How to add “outside” event listener to Markers in Google Maps (react-google-maps/api)

I have a list of hikes stored in state and I rendered the location of those hikes as Markers on the Google Map Component like so:

JavaScript

I also display the list of hikes and its other details in its own BusinessCard Component like so:

JavaScript

When I hover over each of the BusinessCard components, I want the corresponding marker to animate “bounce.” I tried manipulate google.maps.event.addListener but I think I was doing it wrong. I’m not sure if it can detect events outside of the GoogleMap component? Any ideas on how should I approach this problem?

Advertisement

Answer

Okay so I don’t know exactly how your components are structured, but try adding state activeMarker inside the component where your Markers are located. Then pass down setActiveMarker as a prop to the Business component. And inside the Business component, pass down hike.coordinates.latitude, hike.coordinates.longitude and setActiveMarker as props to the BusinessCard components. Inside BusinessCard, add onHover={() => props.setActiveMarker({ lat: props.latitude, lng: props.longitude })}

Something like this…

Component where Markers are located

JavaScript

Business component

JavaScript

BusinessCard component

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement