Skip to content

Instantly share code, notes, and snippets.

@gearmobile
Created April 23, 2019 21:38
Show Gist options
  • Save gearmobile/8b5ac17d116dbda86f98b438bf4415b9 to your computer and use it in GitHub Desktop.
Save gearmobile/8b5ac17d116dbda86f98b438bf4415b9 to your computer and use it in GitHub Desktop.
class Map extends React.Component {
constructor(props) {
super(props);
this.map = undefined;
this.markers = [];
}
removeMarkers = () => {
this.markers.forEach(marker => marker.setMap(null));
this.markers = [];
};
addMarkers = points => {
this.markers = points.map(
({ lat, lon }) =>
new google.maps.Marker({
position: { lat: Number(lat), lng: Number(lon) }
})
);
};
setMarkersToMap = () => {
this.markers.forEach(marker => marker.setMap(this.map));
};
fitZoomToMarkers = () => {
const bounds = new google.maps.LatLngBounds();
this.markers.forEach(marker => bounds.extend(marker.getPosition()));
this.map.fitBounds(bounds);
};
setMarkers = points => {
this.removeMarkers();
this.addMarkers(points);
this.setMarkersToMap();
this.fitZoomToMarkers();
};
initMap = () => {
this.map = new google.maps.Map(document.getElementById('gmap'), {
zoom: 4,
center: { lat: -25.344, lng: 131.036 }
});
this.addMarkers(this.markers);
};
componentWillReceiveProps(nextProps) {
const { points } = this.props;
if (points !== nextProps.points) {
this.setMarkers(nextProps.points);
}
}
componentDidMount() {
this.initMap();
}
render() {
return <div id="gmap" className="google-map" />;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment