Last active
February 2, 2021 09:21
-
-
Save MicheleBertoli/cdd3960f608574e49e24 to your computer and use it in GitHub Desktop.
Multiple Info Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi is there a way to set this code in ts format?