出于某种原因,当我单击一个标记时,所有信息窗口都会显示出来。当我单击一个标记时,我希望会出现一个 InfoWindow。有人可以解释为什么所有标记中的所有信息窗口都会出现吗?所以当我关闭它时,InfoWindow 也会关闭。
当我单击目标标记时,预期的行为是打开 InfoWindow。
class VenuesMap extends Component {
constructor(props) {
super(props);
this.state = {
isOpen: false,
};
}
handleToggleOpen = () => {
this.setState({
isOpen: true,
});
};
handleToggleClose = () => {
this.setState({
isOpen: false,
});
};
render() {
const venues = this.props.venues;
let markers;
let userMarkers = (
<Marker
position={{
lat: Number(latCurrentLocation),
lng: Number(lngCurrentLocation),
}}
/>
);
if (venues !== null) {
markers = venues.map((location, i) => {
const lat = location.venue.location.lat;
const lng = location.venue.location.lng;
const index = i + 1;
return (
<Marker
key={i}
position={{ lat: lat, lng: lng }}
label={index.toString()}
onClick={() => this.handleToggleOpen()}
>
{this.state.isOpen && (
<InfoWindow onCloseClick={() => this.handleToggleClose()}>
<span>Something</span>
</InfoWindow>
)}
</Marker>
);
});
}
const MapWithAMarker = withGoogleMap(props => (
<GoogleMap
defaultZoom={this.state.zoom}
defaultCenter={{
lat: Number(latCurrentLocation) || 40.7128,
lng: Number(lngCurrentLocation) || -74.006,
}}
>
{markers}
{userMarkers}
</GoogleMap>
));
const googleMap = (
<MapWithAMarker
containerElement={
<div style={{ height: this.props.containerElement }} />
}
mapElement={<div style={{ height: this.props.mapElement }} />}
/>
);
return <div>{googleMap}</div>;
}
}