Search code examples
reactjsreact-google-maps

react-google-maps: multiple info windows opening up


This is probably going to be a stupidly simple react question but please bear with me. I am trying to run a react-google-maps with multiple markers with info windows. Here is the code:

const MapWithADirectionsRenderer = compose(
    withProps({
        googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyDf-yIqxErTkbWzKhLox7nAANnrfDIY190&libraries=geometry,drawing,places",
        loadingElement: <div style={{ height: `100%` }} />,
        containerElement: <div style={{ height: `800px` }} />,
        mapElement: <div style={{ height: `100%` }} />,
    }),
    withStateHandlers(() => ({
        isOpen: false,
    }), {
        onToggleOpen: ({ isOpen }) => () => ({
            isOpen: !isOpen,
        })
    }),
    withScriptjs,
    withGoogleMap,
    })
)(props =>
    <GoogleMap
        defaultZoom={13}
        defaultCenter={new google.maps.LatLng(42.357064, -71.062577)}
    >

        <Marker
            position={{ lat: 42.3381437, lng: -71.0475773 }}
            onClick={props.onToggleOpen}
        >
            {props.isOpen && <InfoWindow onCloseClick={props.onToggleOpen}>
                <h3>South Boston</h3>
            </InfoWindow>}
        </Marker>
        <Marker
            position={{ lat: 42.3875968, lng: -71.0994968 }}
            onClick={props.onToggleOpen}
        >
            {props.isOpen && <InfoWindow onCloseClick={props.onToggleOpen}>
                <h3>Somervil</h3>
            </InfoWindow>}
        </Marker>

        {props.directions && <DirectionsRenderer directions={props.directions} />}
    </GoogleMap>
);

When I click one of the markers, the info windows for all the markers toggle open. How do I just toggle open the info window of the marker I click?


Solution

  • I would suggest to introduce a component:

    class MarkerWithInfoWindow extends React.Component {
    
        constructor() {
            super();
            this.state = {
                isOpen: false
            }
            this.onToggleOpen = this.onToggleOpen.bind(this);
        }
    
        onToggleOpen() {
            this.setState({
                isOpen: !this.state.isOpen
            });
        }
    
        render() {
            return (<Marker
                position={this.props.position}
                onClick={this.onToggleOpen}>
                {this.state.isOpen && <InfoWindow onCloseClick={this.onToggleOpen}>
                    <h3>{this.props.content}</h3>
                </InfoWindow>}
            </Marker>)
        }
    }
    

    to control info window per marker via MarkerWithInfoWindow state:

     <GoogleMap
        defaultZoom={13}
        defaultCenter={new google.maps.LatLng(42.357064, -71.062577)}>
    
        <MarkerWithInfoWindow position={{ lat: 42.3381437, lng: -71.0475773 }} content="South Boston" />
        <MarkerWithInfoWindow position={{ lat: 42.3875968, lng: -71.0994968 }} content="Somervil" />
    
    </GoogleMap>
    

    Demo