【问题标题】:Google Map API in React: How to assign InfoWindow for each MarkerReact 中的 Google Map API:如何为每个标记分配 InfoWindow
【发布时间】:2018-07-26 16:34:25
【问题描述】:

我从create-react-app 开始我的项目,并安装了google-maps-react
现在我想为 每个 标记分配 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>
    )
  }
}

【问题讨论】:

    标签: reactjs google-maps google-maps-react


    【解决方案1】:

    要将信息窗口与标记相关联,请将其呈现在 &lt;Marker&gt; 中,以下示例演示如何为每个标记实例化信息窗口:

    <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>
    

    Demo

    【讨论】:

    • 我已经决定彻底​​改变我创建标记的方式(转回纯 JavaScript),因为我觉得我有更多这样的控制权。但这很有意义!其中之一是“为什么我没想到……”。 }:) 谢谢您的回答。 }:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2017-11-02
    相关资源
    最近更新 更多