【发布时间】:2020-10-06 11:10:21
【问题描述】:
我正在尝试向我的 Google 地图添加多个标记,但我不确定如何开始,因为我是 React 新手。我想创建一个标记数组并将它们全部渲染到地图上。我该怎么做?
到目前为止的代码:
import React, { Component } from 'react';
import { Map, GoogleApiWrapper, InfoWindow, Marker } from 'google-maps-react';
const mapStyles = {
width: '100%',
height: '100%'
};
export class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {}
};
onMarkerClick = (props, marker, e) =>
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
onClose = props => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
});
}
};
render() {
return (
<Map
google={this.props.google}
zoom={14}
style={mapStyles}
initialCenter={{ lat: 49.166590, lng: -123.133569 }}
>
<Marker
onClick={this.onMarkerClick}
name={''}
/>
<InfoWindow
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}
onClose={this.onClose}
>
<div>
<h4>{this.state.selectedPlace.name}</h4>
</div>
</InfoWindow>
</Map>
);
}
}
我看到了
<Marker
onClick={this.onMarkerClick}
name={''}
/>
正在渲染标记,但我如何将它变成一个循环来显示标记数组?
【问题讨论】:
标签: javascript reactjs google-maps