Info Window Not Displaying
This is my first time using any google maps package and I found this one was the easiest to use by far. Excuse me if this question has already been answered but I cannot find any issue associated with it.
I want an InfoWindow to pop up when I click on the marker and It doesn't open even if the value of visible is set to true. Any direction of how to fix this would be extremely helpful! Thanks.
The Spaghettios:
import React, { useState } from 'react' import { Map, InfoWindow, Marker, GoogleApiWrapper } from 'google-maps-react';
export function MapContainer(props) { const [showInfoWindow, setShowInfoWindow] = useState(false); const [initialCenter] = useState({ lat: 37.548271, lng: -121.988571 });
function openInfoWindow() {
setShowInfoWindow(!showInfoWindow);
}
return (
<div>
<Map google={props.google} zoom={13} initialCenter={initialCenter}>
<Marker
onClick={openInfoWindow}
name={'Current location'}
position={initialCenter}
/>
<InfoWindow visible={true}>
<div>
<h1>Name</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
export default GoogleApiWrapper({ apiKey: //correct api key is used here in compiled code })(MapContainer);
Thanks!
Pass your marker through the onClick event on Marker and set it to a state variable.
const handleMarkerClick = (props, marker, e) => {
setInfoWindowVisible(true);
setActiveMarker(marker);
}
Then in the InfoWindow set the marker prop to activeMarker.
<InfoWindow marker={activeMarker} visible={infoWindowVisible} onClose={handleWindowClose} >
<div>
<h1>Name</h1>
</div>
</InfoWindow>
That should work. The onClose event will be useful if you want to show and hide the InfoWindow.
Pass your marker through the onClick event on Marker and set it to a state variable.
is there way to reference the marker from outside. I have list of item that corresponds to marker. I would like to highlight marker when i hover over the list items.