I am building a boat visualizer using AISHub. I was able to locate the vessels I wanted using latitude/longitude and show them on a map. However there are too many vessel (markers) and I don't know who is who.
The problem: How do I show a pop-up window dynamically after clicking on a marker like below?
Below the most important part of the code I am using:
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>
);
}
}
What I have done so far:
1) I found this post which was useful to understand the procedure. But unfortunately I was not able to solve it.
2) Also I found this one which is very useful, but there are two problems that are keeping me from using it: a) the info box is not dynamic, and b) I am using google-map-react
but the post isn't:
3) Lastly I tried to write my own component InfoWindowBox.js
and below is what I have done so far but have no idea if I am going in the right direction or not and if that should be implemented in the initial code:
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} />;
}
}
Please is anyone has gone though this, guide to the right direction for solving it as I am running out of ideas.