File size: 1,085 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
class MarkerManager {
constructor(map) {
this.map = map;
this.markers = {};
}
updateMarkers(spots) {
let spotObj = {};
spots.forEach(spot => spotObj[spot.id] = spot);
spots
.filter(spot => !this.markers[spot.id])
.forEach(newSpot => this.createMarkerFromSpot(newSpot));
Object.keys(this.markers)
.filter(spotId => !spotObj[spotId])
.forEach(spotId => this.removeMarker(this.markers[spotId]));
}
createMarkerFromSpot(spot) {
const position = new google.maps.LatLng(spot.lat, spot.lng);
const marker = new google.maps.Marker({
position,
map: this.map,
spotId: spot.id
});
marker.addListener('click', () => this.handleClick(spot));
this.markers[marker.spotId] = marker;
}
removeMarker(marker) {
if (this.markers[marker.id]) {
this.markers[marker.id].setMap(null);
delete this.markers[marker.id];
}
}
}
export default MarkerManager; |