【问题标题】:React and google-maps-react. Not displaying InfoWindow反应和谷歌地图反应。不显示信息窗口
【发布时间】:2019-12-31 03:35:28
【问题描述】:

我正在为一个项目使用 React 和 google-maps-react 库。我正在尝试为每个标记显示 InfoWindows。但它不起作用。我已经为此查看了文档以及 GitHub 问题,但无法找到解决方案。

这里是代码

import React from "react";
import { connect } from "react-redux";

import { Map, GoogleApiWrapper, Marker, InfoWindow } from "google-maps-react";

import { API_GOOGLE_MAPS } from "../config";

const mapStyles = {
    width: "100%",
    height: "100%"
};

export class GoogleMap extends React.Component {
    render() {
        let markers = this.props.vendors
            ? this.props.vendors.map((vendor, index) => {
                  return (
                      <Marker
                          key={index}
                          id={index}
                          position={{
                              lat: vendor.Latitude,
                              lng: vendor.Longitude
                          }}
                          onClick={() => console.log("You clicked me!")}
                      >
                          <InfoWindow
                              position={{
                                  lat: vendor.Latitude,
                                  lng: vendor.Longitude
                              }}
                              visible={true}
                          >
                              <div id={vendor.Latitude}>hello</div>
                          </InfoWindow>
                      </Marker>
                  );
              })
            : null;

        return (
            <Map google={this.props.google} zoom={13} style={mapStyles} initialCenter={this.props.mapCenter}>
                {markers ? markers : null}
            </Map>
        );
    }
}

const mapStateToProps = state => ({
    vendors: state.app.vendors,
    mapCenter: state.app.mapCenter
});

const WrappedContainer = GoogleApiWrapper({
    apiKey: API_GOOGLE_MAPS
})(GoogleMap);

export default connect(mapStateToProps)(WrappedContainer);

地图显示所有标记,但不显示信息窗口。我可能做错了。如何让 InfoWindow 显示标记?

这是picture

谢谢。

【问题讨论】:

    标签: javascript reactjs google-maps google-maps-api-3 google-maps-markers


    【解决方案1】:

    看起来支持 InfoWindow 作为 Marker 的子属性是 pull request,它已合并到尚未发布的版本 2.0.3 中。截至目前(2019 年 9 月 1 日)的最新 NPM 版本是 2.0.2。

    您可以做的是在单击标记时打开一个信息窗口,方法是将信息窗口放置在标记的同一级别,然后根据单击的标记将其连接到每个标记。

    查看以下基于演示的codesandbox。示例代码如下。

          <Map
            google={this.props.google}
            onClick={this.onMapClicked}
            zoom={13}
          >
            <Marker
              name="Marker 1"
              onClick={this.onMarkerClick}
              position={{ lat: 37.778519, lng: -122.40564 }}
            />
    
            <InfoWindow
              marker={this.state.activeMarker}
              visible={this.state.showingInfoWindow}
            >
              <div>
                <h4>Hello</h4>
              </div>
            </InfoWindow>
          </Map>
    

    希望对你有所帮助。

    【讨论】:

    • 谢谢。但是我注意到,当您单击&lt;Marker /&gt; 时正在更新状态,您将导致整个&lt;Map/&gt; 的重新渲染。让它看起来有问题。有什么解决办法吗?
    • 嘿,你能解释一下你所说的越野车外观是什么意思吗?这是库的代码,也是它在演示中的实现方式,所以无论如何我都假设没有针对预期行为的“解决方案”,或者您指的是其他东西?
    • 是的,澄清一下。如果您仔细查看演示,当您单击标记时,所有标记都会非常快速地重新渲染。这很可能是因为组件的状态正在更改为显示/取消显示 InfoWindow。
    • 感谢您的澄清,我明白您现在的意思了,我很乐意帮助您找到解决方案,尽管这将是一个完全不同的问题
    【解决方案2】:

    这是InfoWindow 显示的完整解决方案。我还附上了你可以看到的链接以便更多地理解..https://codesandbox.io/s/quizzical-hermann-5ehs7

    import React, { Component } from "react";
    import { Map, InfoWindow, Marker, GoogleApiWrapper } from "google-maps-react";
    
    export class MapContainer extends Component {
      state = {
        activeMarker: {},
        selectedPlace: {},
        showingInfoWindow: false
      };
    
      onMarkerClick = (props, marker) =>
        this.setState({
          activeMarker: marker,
          selectedPlace: props,
          showingInfoWindow: true
        });
    
      onInfoWindowClose = () =>
        this.setState({
          activeMarker: null,
          showingInfoWindow: false
        });
    
      onMapClicked = () => {
        if (this.state.showingInfoWindow)
          this.setState({
            activeMarker: null,
            showingInfoWindow: false
          });
      };
    
      render() {
        if (!this.props.loaded) return <div>Loading...</div>;
    
        return (
          <Map
            className="map"
            google={this.props.google}
            onClick={this.onMapClicked}
            style={{ height: "100%", position: "relative", width: "100%" }}
            zoom={13}
          >
            <Marker
              name="Marker 1"
              onClick={this.onMarkerClick}
              position={{ lat: 37.778519, lng: -122.40564 }}
            />
    
            <Marker
              name="Marker 2"
              onClick={this.onMarkerClick}
              position={{ lat: 37.759703, lng: -122.428093 }}
            />
    
            <Marker name="Marker 3" onClick={this.onMarkerClick} />
    
            <InfoWindow
              marker={this.state.activeMarker}
              onClose={this.onInfoWindowClose}
              visible={this.state.showingInfoWindow}
            >
              <div>
                <h4>{this.state.selectedPlace.name}</h4>
              </div>
            </InfoWindow>
          </Map>
        );
      }
    }
    export default GoogleApiWrapper({
      apiKey: "",
      version: "3.38"
    })(MapContainer);
    
    

    【讨论】:

      猜你喜欢
      • 2015-04-30
      • 2016-12-21
      • 2012-02-18
      • 2013-01-07
      • 2022-06-10
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多