google-maps-react icon indicating copy to clipboard operation
google-maps-react copied to clipboard

Info Window Not Displaying

Open PedroNunezCode opened this issue 6 years ago • 2 comments

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!

PedroNunezCode avatar Feb 19 '20 06:02 PedroNunezCode

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.

ozzyonfire avatar Feb 28 '20 02:02 ozzyonfire

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.

ClevercodeTech avatar Apr 26 '20 06:04 ClevercodeTech