1

我开始了我的项目create-react-app,并安装了google-maps-react.
现在我想为每个Marker 分配 InfoWindow,这样我就可以一次显示很多。(也许答案很明显,但对我来说不是......)
我的代码:

export class MapContainer extends Component {
  render() {
    return (
      <Map
        className={'map'}
        google={this.props.google}
        zoom={13}
        initialCenter={{lat: 54.753986, lng: 24.670219}}
        style={nullStyle}
      >
        {this.props.places.map(place => (
          <Marker
            key={`marker-${place.name}`}
            title={place.title}
            name={place.name}
            position={place.position}
          />
        ))}
        {this.props.places.map(place => (
          <InfoWindow
            key={`infowindow-${place.name}`}
            marker={_JUST_BEFORE_CREATED_MARKER_}
            visible={true}>
            <div>{place.title}</div>
          </InfoWindow>
        ))}
      </Map>
    )
  }
}
4

1 回答 1

1

要将信息窗口与标记相关联,请将其呈现在 a<Marker>中,下面的示例演示了如何为每个标记实例化信息窗口:

<GoogleMap
    defaultZoom={5}
    defaultCenter={new google.maps.LatLng(-25.0317893, 115.219989)}>

    {props.places.map(place => (
        <Marker
            key={`marker-${place.name}`}
            title={place.title}
            name={place.name}
            position={place.position}

        >
            <InfoWindow
                key={`infowindow-${place.name}`}
                visible={true}>
                <div>{place.title}</div>
            </InfoWindow>
        </Marker>
    ))}

</GoogleMap>

演示

于 2018-07-28T09:14:05.557 回答