【问题标题】:React Google Map InfoWindow showing all the info when I click on a single Mark当我单击一个标记时,React Google Map InfoWindow 显示所有信息
【发布时间】:2018-06-28 03:58:42
【问题描述】:

由于某种原因,当我单击单个标记时,所有信息窗口都会显示。当我单击一个标记时,我希望会出现一个 InfoWindow。 有人可以解释为什么所有标记中的所有信息窗口都会出现吗?所以当我关闭它时,InfoWindow 也会关闭。

当我点击目标标记时,预期的行为是信息窗口打开。

class VenuesMap extends Component {
  constructor(props) {
    super(props);

    this.state = {
      isOpen: false,
    };
  }

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

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

  render() {
    const venues = this.props.venues;

    let markers;
    let userMarkers = (
      <Marker
        position={{
          lat: Number(latCurrentLocation),
          lng: Number(lngCurrentLocation),
        }}
      />
    );
    if (venues !== null) {
      markers = venues.map((location, i) => {
        const lat = location.venue.location.lat;
        const lng = location.venue.location.lng;
        const index = i + 1;
        return (
          <Marker
            key={i}
            position={{ lat: lat, lng: lng }}
            label={index.toString()}
            onClick={() => this.handleToggleOpen()}
          >
            {this.state.isOpen && (
              <InfoWindow onCloseClick={() => this.handleToggleClose()}>
                <span>Something</span>
              </InfoWindow>
            )}
          </Marker>
        );
      });
    }

    const MapWithAMarker = withGoogleMap(props => (
      <GoogleMap
        defaultZoom={this.state.zoom}
        defaultCenter={{
          lat: Number(latCurrentLocation) || 40.7128,
          lng: Number(lngCurrentLocation) || -74.006,
        }}
      >
        {markers}
        {userMarkers}
      </GoogleMap>
    ));

    const googleMap = (
      <MapWithAMarker
        containerElement={
          <div style={{ height: this.props.containerElement }} />
        }
        mapElement={<div style={{ height: this.props.mapElement }} />}
      />
    );

    return <div>{googleMap}</div>;
  }
}

【问题讨论】:

  • 快速现场演示怎么样?

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


【解决方案1】:

您需要为每个信息窗口设置一个单独的 isOpen 状态,否则如果isOpen 为真,它们都会打开。

选项1:您可以创建一个组件&lt;MarkerWithInfoWindow&gt;,在其中您可以维护它自己的isOpen 状态。这仍然允许通过一个一个单击来一次打开多个。

选项 2: 如果你想保持结构不变(这样你可以在另一个打开时关闭一个),你可以在状态中存储一个openInfoWindowMarkerId,就像这样

this.state = {
        openInfoWindowMarkerId: ''
    }

然后你可以做类似的事情

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

你可以这样称呼它

<Marker
    key={i}
    position={{ lat: lat, lng: lng}}
    label={index.toString()}
    onClick={() => this.handleToggleOpen(i)} // marker ID is the key here. 
>

【讨论】:

  • 嘿帕拉什,感谢您回复我。我正在尝试您的建议选项一。我仍然很困惑它如何知道单击标记时要显示哪个元素。此外,Mark 控制 InfoWindow State 是 true 还是 false。如果我要保持 InfoWindow 组件中的状态与 Marker 中的单击处理程序没有冲突。
  • 修复了这个问题。谢谢你给我洞察力!
【解决方案2】:

我按照上面的说明解决了这个问题。

我将每个标记提取到一个组件本身中。

我将 Mark 和 InfoWindow 合并为一个组件。

import React, {Component} from 'react';
import { Marker, InfoWindow } from "react-google-maps";


class InfoWindowMap extends Component {

    constructor(props){
        super(props);

        this.state = {
            isOpen: false
        }

    }

    handleToggleOpen = () => {

        this.setState({
            isOpen: true
        });
    }

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


  render() {

    return (
            <Marker
                key={this.props.index}
                position={{ lat: this.props.lat, lng: this.props.lng}}
                label={this.props.index.toString()}
                onClick={() => this.handleToggleOpen()}
            >

            {
                this.state.isOpen &&
             <InfoWindow onCloseClick={this.props.handleCloseCall}>
                 <span>Something</span>
             </InfoWindow>
            }


            </Marker>

        )
  }
}

export default InfoWindowMap;

【讨论】:

    【解决方案3】:
    try this: 
    
    {props.nhatro.map((nhatro, index) =>
            <Marker
              key={index}
              options={{icon: 'https://i.imgur.com/9G5JOp8.png'}}
              position={nhatro}
              onClick={()=>{ props.showInfo(index)} }
            >
              { (props.showInfoIndex == index ) && 
              <InfoWindow  onCloseClick={props.onToggleOpen}>
                <div>
                  <div>nhà trọ cho thuê</div>
                  <div >1.500.000đ</div>
                </div>
              </InfoWindow>}
            </Marker>
        )}
    
    and then :
    
    showInfo(a){
     setState({showInfoIndex: a })
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-09
      • 2020-07-22
      • 2020-11-13
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      相关资源
      最近更新 更多