내지도에있는 각 마커에 대한 정보 창을 만들어야합니다. 지도에서 마커를 볼 수는 있지만 마커를 클릭하여 InfoWindow를 가져 오면이 오류가 TypeError: Cannot read property 'isOpen' of undefined
이며 의 this.setState({isOpen: !this.state.isOpen})
을 참조합니다. 대신react-google-maps 여러 마커 및 정보창
감사
componentWillMount() {
this.setState({ markers: [], isOpen: false })
}
componentDidMount() {
let list = { restaurants : []}
database.ref('..').on('value', .. => {
...
})
this.setState({ markers: list.restaurants});
})
}
onToggleOpen(){
this.setState({isOpen: !this.state.isOpen})
}
render() {
const MapWithAMarker = compose(
withProps({
googleMapURL: ...,
loadingElement: ...,
containerElement: ...,
mapElement: ...
}),
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
defaultZoom={17}
defaultCenter={{ lat: ..., lng: ... }}
>
{props.markers.map(marker => (
<Marker
key={marker.name}
position={{ lat: marker.latitude, lng: marker.longitude }}
onClick={this.onToggleOpen}
>
{this.state.isOpen && <InfoWindow onCloseClick={this.onToggleOpen}> marker.key </InfoWindow>}
</Marker>
))}
</GoogleMap>
);
return (
<section id="mapa">
<div class= "container">
<div class="row">
<div class="col-md-12 text-left">
<h2 class="section-heading">MAPA</h2>
<MapWithAMarker markers={this.state.markers}/>
</div>
</div>
<div class="row text-center">
</div>
</div>
</section>
)
}
편집 : 업데이트 된 코드 :
export default class Mapa extends Component {
constructor(props) {
super(props)
this.state = {
markers: [],
isOpen: false
}
}
componentDidMount() {
let list = { restaurants : []}
database.ref('..').on('value', restaurants => {
restaurants.forEach(restaurant => {
list.restaurants.push({'name': restaurant.key,
'longitude': restaurant.val().lng,
'latitude': restaurant.val().lat}
)
})
this.setState({ markers: list.restaurants });
})
}
onToggleOpen =() => {
this.setState({isOpen: !this.state.isOpen})
}
render() {
const MapWithAMarker = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?key=...&v=3.exp&libraries=geometry,drawing,places",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />
}),
withScriptjs,
withGoogleMap
)(props =>
<GoogleMap
defaultZoom={17}
defaultCenter={{ lat: .., lng: ... }}
>
{props.markers.map(marker => (
<Marker
key={marker.name}
position={{ lat: marker.latitude, lng: marker.longitude }}
onClick={this.onToggleOpen}
>
{this.state.isOpen && <InfoWindow onCloseClick={this.onToggleOpen}> marker.key </InfoWindow>}
</Marker>
))}
</GoogleMap>
);
return (
<section id="mapa">
<div class= "container">
<h3 class="info-title"><span class="info-title-span">Tasty and Convenient.</span></h3>
<div class="row">
<div class="col-md-12 text-left">
<MapWithAMarker markers={this.state.markers}/>
</div>
</div>
<div class="row text-center">
</div>
</div>
</section>
)
}
그냥 렌더링 방법이를 선언하고이 범위에 그래서 거기에서 호출은'VAR ISOPEN = this.state.isOpen,' – fungusanthrax