Skip to content

Instantly share code, notes, and snippets.

@MicheleBertoli
Last active February 2, 2021 09:21
Show Gist options
  • Save MicheleBertoli/cdd3960f608574e49e24 to your computer and use it in GitHub Desktop.
Save MicheleBertoli/cdd3960f608574e49e24 to your computer and use it in GitHub Desktop.
Multiple Info Windows
import React from 'react';
import ReactDOM from 'react-dom';
import {Gmaps, Marker, InfoWindow} from 'react-gmaps';
const points = [{
lat: 51.5258541,
lng: -0.09040660000006028
}, {
lat: 51.5258541,
lng: -0.08040660000006028
}, {
lat: 51.5258541,
lng: -0.07040660000006028
}];
const App = React.createClass({
getInitialState() {
return {
infoWindows: [false, false, false]
};
},
toggleInfoWindow(index) {
const {infoWindows} = this.state;
infoWindows[index] = !infoWindows[index];
this.setState({
infoWindows
});
},
renderInfoWindows() {
const {infoWindows} = this.state;
return points.map((coords, index) => {
if (!infoWindows[index]) return null;
return (
<InfoWindow
key={index}
lat={coords.lat}
lng={coords.lng}
onCloseClick={() => this.toggleInfoWindow(index)}
/>
);
});
},
renderMarkers() {
return points.map((coords, index) =>
<Marker
key={index}
lat={coords.lat}
lng={coords.lng}
onClick={() => this.toggleInfoWindow(index)}
/>
);
},
render() {
return (
<Gmaps
ref="gmaps"
width={400}
height={300}
lat={points[1].lat}
lng={points[1].lng}
zoom={12}
>
{this.renderMarkers()}
{this.renderInfoWindows()}
</Gmaps>
);
}
});
ReactDOM.render(<App />, document.getElementById('gmaps'));
@prashanthravi19
Copy link

hi is there a way to set this code in ts format?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment