我正在为一个项目使用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。我可能做错了。如何获取标记的InfoWindow显示?
这是一个picture
谢谢。
发布于 2019-09-02 01:37:02
看起来对InfoWindow的支持是一个合并到2.0.3版本的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>希望这对你有帮助。
发布于 2020-03-19 15:14:40
这是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);

https://stackoverflow.com/questions/57666455
复制相似问题