I'm trying to put two different json files over a single map. One is drawn by zip code boundaries, the other is county. For some reason, I can't get my leaflet to work. IF someone can help point me to where the issue lies, that'll be great!
<script>
//ingest and congiure zip file
zipjson = L.geoJson("zip-file.json", {
style: style,
onEachFeature: function(feature, layer){
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
})
layer.bindPopup('<b>Zip Code: </b>' + feature.properties.ZIPCODE + '<br>' +
"<b> Data Attribute #1: </b> " + feature.properties.attr1 + '<br>' +
"<b> Data Attribute #2: </b> " + feature.properties.attr2 + '<br>' +
)
}
}).addTo(map);
//ingest and configure county level layer
countyjson = L.geoJson("county-file.json", {
style: style,
onEachFeature: function(feature, layer){
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
})
layer.bindPopup('<b>County: </b>' + feature.properties.COUNTY + '<br>' +
"<b> Data Attribute #1: </b> " + feature.properties.attr1 + '<br>' +
"<b> Data Attribute #2: </b> " + feature.properties.attr2 + '<br>' +
)
}
}).addTo(map);
// set map layer
var usmap = L.tileLayer('mapbox.streets', {
maxZoom: 18,
minZoom: 1,
attribution: '<font size = "2"><b> Target Universe: Voters who voted in the last two caucus plus the top 100k modeled voters</b></font><br>'
}).
L.mapbox.accessToken = 'fake-token-name';
//initalize map
var map = L.mapbox.map('map',{
center: [41.9, -91.5],
zoom: 7,
layers: [usmap]
});
//set parameters to color map
var max_num = 2000;
var color = d3.scale.linear()
.domain([0, max_num])
.range(['#F0F0D0', '#930F16']);
function style(feature) {
return {
fillColor: color(feature.properties.targetnum), // fill depending on value
weight: 1,
opacity: 0.5,
color: 'black',
fillOpacity: 0.9
};
}
// configuration to fix map
var baseMaps = {
"US" : usmap,
};
var overlayMap = {
"Counties" : countyjson,
"Zip Codes" : zipjson
};
L.control.layers(baseMaps, overlayMap).addTo(map);
</script>
Best Answer
There are two possibilities I found (I have had the same problem).
<script>
tag to load the files, but if you do so, it works. Declare each JSON file using the<script>
tag:Make sure each JSON file starts with the the
var
declaration, as it will be included through the script tag:Call them in:
When calling them, you do not need to
addTo(map)
since this will be taken care of, as you did on your code, byL.control.layers(baseMaps, overlayMaps).addTo(map);
.$.geojson
I found another option which is just calling theL.geoJson
withnull
. It works just as well (though I don't know if its the most elegant solution):...then:
hope this helps!