Rewrite marker show and hide logic

This commit is contained in:
Arne Schlüter 2015-02-08 15:44:00 +01:00
commit 792f8f9447

View file

@ -17,7 +17,8 @@ class Visualization {
this.data = data this.data = data
this.colors = colors this.colors = colors
this._markers = [] this._markers = new Map()
this.setupMarkers()
} }
/** /**
@ -105,6 +106,24 @@ class Visualization {
return this return this
} }
/**
* Sets up all markers for the first time so afterwards they only need to be
* faded in and out
* @return {Visualization}
* @chainable
*/
setupMarkers () {
this.data.forEach(incident => {
var options = { color: this._pickColor(incident) }
var marker = L.circleMarker([incident.lat, incident.lng], options)
.addTo(this.map)
this._markers.set(incident, marker)
})
return this;
}
/** /**
* Clear the map and render new markers * Clear the map and render new markers
* @param {Array[Object]} incidents The incidents to be shown * @param {Array[Object]} incidents The incidents to be shown
@ -115,25 +134,17 @@ class Visualization {
if (incidents == null) if (incidents == null)
incidents = this.data incidents = this.data
this._markers.forEach(marker => { this.map.removeLayer(marker) }) for (var [incident, marker] of this._markers)
this._markers = incidents.map(incident => { if (incidents.indexOf(incident) == -1)
return this._createMarker(incident).bindPopup(incident.description.replace(/\n/g, '<br>')) this.map.removeLayer(marker)
}) else
marker
.addTo(this.map)
.bindPopup(incident.description.replace(/\n/g, '<br>'))
return this return this
} }
/**
* Creates the correct marker for a single incident and adds it to the map
* @param {Object} data A single incident
* @return {L.CircleMarker}
*/
_createMarker (data) {
var options = { color: this._pickColor(data) }
var marker = L.circleMarker([data.lat, data.lng], options).addTo(this.map)
return marker
}
/** /**
* Picks a color for a given incident based on its categories * Picks a color for a given incident based on its categories
* @param {Object} incident A single incident * @param {Object} incident A single incident