我正在使用 AISHub 构建船只可视化器。我能够使用纬度/经度找到我想要的船只并将它们显示在地图上。但是容器(标记)太多,我不知道谁是谁。
问题:如何在单击如下标记后动态显示弹出窗口?
下面是我正在使用的代码最重要的部分:
class BoatMap extends Component {
constructor(props) {
super(props);
this.state = {
buttonEnabled: true,
buttonClickedAt: null,
progress: 0,
ships: [],
type: 'All',
shipTypes: [],
activeShipTypes: [],
logoMap: {}
};
this.updateRequest = this.updateRequest.bind(this);
this.countDownInterval = null;
}
// ... other operations
// ... other operations
render() {
return (
<div className="google-map">
<GoogleMapReact
bootstrapURLKeys={{ key: 'My_KEY' }}
center={{
lat: this.props.activeShip ? this.props.activeShip.latitude : 42.4,
lng: this.props.activeShip ? this.props.activeShip.longitude : -71.1
}}
zoom={8}
>
{/* Rendering all the markers here */}
{this.state.ships.map((ship) => (
<Ship
ship={ship}
key={ship.CALLSIGN}
lat={ship.LATITUDE}
lng={ship.LONGITUDE}
logoMap={this.state.logoMap}
/>
))}
<select className="combo-companies" onClick={this.props.handleDropdownChange}>
<option value="All">All</option>
{this.state.shipTypes.map((type) => (
<option
className={this.state.activeShipTypes.includes(type) ? 'active' : ''}
key={type}
value={type}
>
{type}
</option>
))}
</select>
</GoogleMapReact>
</div>
);
}
}
到目前为止我做了什么:
1)我发现这篇文章对理解程序很有用。但不幸的是,我无法解决它。
2)我还发现这个非常有用,但是有两个问题使我无法使用它:a)信息框不是动态的,b)我正在使用google-map-react
但帖子不是:
3)最后我尝试编写自己的组件InfoWindowBox.js
,下面是我到目前为止所做的,但不知道我是否朝着正确的方向前进,以及是否应该在初始代码中实现:
InfoWindowBox.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { InfoWindow } from 'google-maps-react';
export default class InfoWindowEx extends Component {
constructor(props) {
super(props);
this.infoWindowRef = React.createRef();
this.contentElement = document.createElement(`div`);
}
componentDidUpdate(prevProps) {
if (this.props.children !== prevProps.children) {
ReactDOM.render(React.Children.only(this.props.children), this.contentElement);
this.infoWindowRef.current.infowindow.setContent(this.contentElement);
}
}
render() {
return <InfoWindow ref={this.infoWindowRef} {...this.props} />;
}
}
请有没有人经历过这个,指导正确的方向来解决它,因为我已经没有想法了。