【问题标题】:React Google Maps InfoWindow toggle display one at a timeReact Google Maps InfoWindow 切换一次显示一个
【发布时间】:2018-10-28 04:41:14
【问题描述】:

我目前正在使用 react-google-maps,并且可以通过单击标记(从地图或列表中)正确显示标记和 InfoWindow。但是,一旦我单击要打开的标记,即使我选择了一个新的,以前的 InfoWindow 也会保持打开状态。

如果我单击列表中的“x”或标记,我可以关闭信息窗口。但我不确定如何自动关闭其他 InfoWindow 并且只打开当前标记的 InfoWindow。

我查看了几个类似的问题以获得一个想法:When displaying multiple markers on a map, how to open just one info window, when clicking on a marker?React-Google-Map multiple Info window open

但我仍然无法通过和检查唯一标记,因此只显示与标记 ID 匹配的信息窗口。

class MarkerInfoList extends Component {

  constructor(props){
    super(props);

    this.state = {
      isOpen: false
    };
  }

  handleToggleOpen = (markerId) => {
    this.setState({ 
      isOpen: true
    });
  }

  handleToggleClose = (markerId) => {
    this.setState({
      isOpen: false
    });
  }


render() {

  const isOpen = this.state.isOpen;

  return (

    <div>
      {this.state.isOpen ? (
        <ul>
          <li onClick={() => this.handleToggleClose()}>{this.props.venue}</li>

          <Marker
            key={this.props.index}
            id={this.props.index}
            position={{ lat: this.props.lat, lng: this.props.lng}}
            defaultAnimation={google.maps.Animation.DROP}
            onClick={() => this.handleToggleOpen(this.props.index)}>

            <InfoWindow 
                id = {this.props.index}
                onCloseClick={() => this.setState({isOpen: false})}>
                <div key={this.props.index}>
                  <h4>{this.props.venue}</h4>
                </div>
            </InfoWindow>  
          </Marker>
        </ul>

      ) : (

        <ul>
          <li onClick={() => this.handleToggleOpen()}>{this.props.venue}</li> 
        </ul>

      )
    }

  </div>

    )
  }
}

export default MarkerInfoList;

【问题讨论】:

    标签: javascript reactjs google-maps-api-3 infowindow react-google-maps


    【解决方案1】:

    我想切换单个窗口,但还需要知道这些位置在父组件上是否“活动”。因此,在父组件中,我使用活动键声明了。我将其作为activeKey 组件以及所有位置数据作为locations 的初始列表传递给地图。

     <GoogleMap defaultZoom={16} defaultCenter={this.state.currentLocation}>
            {this.props.locations.map((location, i) => (
              <Marker
                key={location.key}
                position={{
                  lat: location.latitude,
                  lng: location.longitude
                }}
                onClick={() => {
                  this.props.toggleLocationsActive(location.key);
                  // this.setCurrentLocation(location.latitude, location.longitude);
                }}
              >
                          {location.key === this.props.activeKey && (
              <InfoWindow onCloseClick={props.onToggleOpen}>
                <div>
                  {location.orgName}
                  {location.orgName !== location.programName &&
                    "/" + location.programName}
                </div>
              </InfoWindow>
            )}
              </Marker>
            ))}
          </GoogleMap>
    

    在父组件中,我有 state = {activeKey: ""} 和以下方法:

      toggleLocationsActive = locationKey => {
        this.setState({
          activeKey: locationKey
        });
      };
    

    两者都是在 props 中传下来的:

       <Map
                activeKey={this.state.activeKey}
                toggleLocationsActive={this.toggleLocationsActive}
              />
    

    请注意,如果您不需要此状态存在于父组件中,您可以在地图组件中执行此操作。

    【讨论】:

    • 我了解将“活动”位置与位置列表进行比较,但是如何将父组件的状态设置为向下传递?如果我从标记列表开始,当用户单击标记时,我将特定标记位置设置为活动并将其传递给子组件?
    • 编辑了我上面的答案来回答你的问题。
    • 感谢您的解释,我能够让信息窗口一次打开一个。但是每次我单击标记时,地图都会重新渲染,并且正确的信息窗口会打开。想知道您的地图是否也会重新渲染?
    • 它正在调用新的 API 并在我的应用中重新加载地图。
    猜你喜欢
    • 2020-02-17
    • 2014-04-25
    • 2021-02-19
    • 2011-05-16
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2014-02-13
    相关资源
    最近更新 更多