React and google-maps-react. Not displaying InfoWindow

≡放荡痞女 提交于 2020-03-24 12:07:14

问题


I am using React and google-maps-react library for a project. I am trying to display InfoWindows for each of the markers. But it is not working. I have looked at the documentation as well as GitHub issues for this and have no been able to find a solution.

Here is the code

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

The maps displays all the markers, but not the InfoWindow. I am probably doing this wrong. How can I get the InfoWindow to display for the markers?

Here is a picture

Thank you.


回答1:


It looks like support for InfoWindow as a prop child of Marker was a pull request that was merged into version 2.0.3 which isn't live yet. The latest NPM version as of this date (1 September 2019) is 2.0.2.

What you can do is open an info window when a marker is clicked by placing the infowindow at the same level of the markers and then connect it to each marker depending on the one that is clicked.

Have a look at the following codesandbox which is based off of the demo. Sample code below.

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

Hope this helps you.




回答2:


Here is a complete solution InfoWindow shows. i have also attached the link you can see for more understanding.. 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);

enter image description here



来源:https://stackoverflow.com/questions/57666455/react-and-google-maps-react-not-displaying-infowindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!