One reasonable solution is switching off the main geoJSON layer, whereas the another sublayers has been determined.
The simplified code can look like this:
/* ----------- main GeoJSON Layer job2 made inactive!!! ---------------
job2 = L.geoJson(data2, { //layer2 Virgin Media start
style: style2,
pointToLayer: function(feature, latlng) {
feature.properties.myKey = '<b>'+ feature.properties.Owner + '</b>; ' + feature.properties.Address
label = String(feature.properties.Owner)
if (feature.properties.Post_Survey_Home_Count >=100)
return L.circleMarker(latlng, {
radius:12,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count >=50)
return L.circleMarker(latlng, {
radius:9,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count >=1)
return L.circleMarker(latlng, {
radius:6,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count === "")
return L.circleMarker(latlng, {
radius:4,
//opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.6,
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
},
onEachFeature: function (feature, layer) { // Virgin Media tracker data start ---------
layer._leaflet_id = feature.properties.Owner;
var popupContent = "<p><h2>" +
feature.properties.Owner + "</h2><font color='red'><h3>Status:</font> " +
feature.properties.Status + "</h3><b> NBU ID:</b> " +
feature.properties.NBU + "</br><b> Ticket ID:</b> " +
feature.properties.Ticket_ID + "</br><b> Address (street):</b> " +
feature.properties.Address + "</br><b> Address (postcode):</b> " +
feature.properties.Postcode + "</br><b> Asbestos report:</b> " +
feature.properties.Asbestos_rep + "</br><font color='blue'><b>Planner</b>:</font> " +
feature.properties.Planner + "</br><font color='blue'><b>Surveyor</b>:</font> " +
feature.properties.Surveyor + "</br></h2><b>Units provisional:</b> " +
feature.properties.Client_Home_Count + "</br></h2><b> Post survey units:</b> " +
feature.properties.Post_Survey_Home_Count + "</br> <b>STATS (applied):</b> " +
feature.properties.STATS_Applied_for_date + "</br> <b>STATS (completed):</b> " +
feature.properties.STATS_compleeted + "</br> <b>VM issue date:</b> " +
feature.properties.Date_issued_from_VM + "</br> <b>QC date:</b> " +
feature.properties.ACTUAL_Internal_QC_Date + "</br> <b>VM ECD date:</b> " +
feature.properties.VM_ECD_date + "</br><b>General notes:</b> " +
feature.properties.General_notes + "</br>" +
'</br><center><a href="'+ feature.properties.Directory +'" target="_blank">Local directory</a></center></p>' +
'<center><font color="red"><a href="'+ feature.properties.Sharepoint +'" target="_blank">Sharepoint</a></font></center></p>';
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
layer.on({
mouseover: function (e) {
this.setStyle({
'fillColor': '#663300',
'weight':11
});
},
mouseout: function (e) {
job2.resetStyle(this);
}
}); //layer.on finish
} //Virgin Media onEachfeature tracker finish
})//.addTo(map); //layer 2 Virgin Media finish
*/
infill = L.geoJson(data2, { //1st geoJSON sublayer, deriving from the "job" layer
style: style2,
pointToLayer: function(feature, latlng) {
feature.properties.myKey = '<b>'+ feature.properties.Owner + '</b>; ' + feature.properties.Address
label = String(feature.properties.Owner)
if (feature.properties.Post_Survey_Home_Count >=100)
return L.circleMarker(latlng, {
radius:12,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count >=50)
return L.circleMarker(latlng, {
radius:9,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count >=1)
return L.circleMarker(latlng, {
radius:6,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count === "")
return L.circleMarker(latlng, {
radius:4,
//opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.6,
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
},
onEachFeature: function (feature, layer) {
layer._leaflet_id = feature.properties.Owner;
var popupContent = "<p><h2>" +
feature.properties.Owner + "</h2><font color='red'><h3>Status:</font> " +
feature.properties.Status + "</h3><b> NBU ID:</b> " +
feature.properties.NBU + "</br><b> Ticket ID:</b> " +
feature.properties.Ticket_ID + "</br><b> Address (street):</b> " +
feature.properties.Address + "</br><b> Address (postcode):</b> " +
feature.properties.Postcode + "</br><b> Asbestos report:</b> " +
feature.properties.Asbestos_rep + "</br><font color='blue'><b>Planner</b>:</font> " +
feature.properties.Planner + "</br><font color='blue'><b>Surveyor</b>:</font> " +
feature.properties.Surveyor + "</br></h2><b>Units provisional:</b> " +
feature.properties.Client_Home_Count + "</br></h2><b> Post survey units:</b> " +
feature.properties.Post_Survey_Home_Count + "</br> <b>STATS (applied):</b> " +
feature.properties.STATS_Applied_for_date + "</br> <b>STATS (completed):</b> " +
feature.properties.STATS_compleeted + "</br> <b>VM issue date:</b> " +
feature.properties.Date_issued_from_VM + "</br> <b>QC date:</b> " +
feature.properties.ACTUAL_Internal_QC_Date + "</br> <b>VM ECD date:</b> " +
feature.properties.VM_ECD_date + "</br><b>General notes:</b> " +
feature.properties.General_notes + "</br>" +
'</br><center><a href="'+ feature.properties.Directory +'" target="_blank">Local directory</a></center></p>' +
'<center><font color="red"><a href="'+ feature.properties.Sharepoint +'" target="_blank">Sharepoint</a></font></center></p>';
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
layer.on({
mouseover: function (e) {
this.setStyle({
'fillColor': '#663300',
'weight':11
});
},
mouseout: function (e) {
infill.resetStyle(this);
}
}); //layer.on finish
},
filter: function(feature, layer) {
return (feature.properties.Type == "Infill" );
}
})//.addTo(map); //Note turned on to start map with Data, Checkbox has checked property.
mdu = L.geoJson(data2, { //2nd geoJSON sublayer, deriving from the "job" layer
style: style2,
pointToLayer: function(feature, latlng) {
feature.properties.myKey = '<b>'+ feature.properties.Owner + '</b>; ' + feature.properties.Address
label = String(feature.properties.Owner)
if (feature.properties.Post_Survey_Home_Count >=100)
return L.circleMarker(latlng, {
radius:12,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count >=50)
return L.circleMarker(latlng, {
radius:9,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count >=1)
return L.circleMarker(latlng, {
radius:6,
opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.8
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
else if (feature.properties.Post_Survey_Home_Count === "")
return L.circleMarker(latlng, {
radius:4,
//opacity: .5,
//color: "#000",
color:getColor2(feature.properties.Type),
fillColor: getColor2(feature.properties.Type),
fillOpacity: 0.6,
}).bindTooltip(label, {permanent: false, direction: "top", className: "my-labels"}).openTooltip();
},
onEachFeature: function (feature, layer) {
layer._leaflet_id = feature.properties.Owner;
var popupContent = "<p><h2>" +
feature.properties.Owner + "</h2><font color='red'><h3>Status:</font> " +
feature.properties.Status + "</h3><b> NBU ID:</b> " +
feature.properties.NBU + "</br><b> Ticket ID:</b> " +
feature.properties.Ticket_ID + "</br><b> Address (street):</b> " +
feature.properties.Address + "</br><b> Address (postcode):</b> " +
feature.properties.Postcode + "</br><b> Asbestos report:</b> " +
feature.properties.Asbestos_rep + "</br><font color='blue'><b>Planner</b>:</font> " +
feature.properties.Planner + "</br><font color='blue'><b>Surveyor</b>:</font> " +
feature.properties.Surveyor + "</br></h2><b>Units provisional:</b> " +
feature.properties.Client_Home_Count + "</br></h2><b> Post survey units:</b> " +
feature.properties.Post_Survey_Home_Count + "</br> <b>STATS (applied):</b> " +
feature.properties.STATS_Applied_for_date + "</br> <b>STATS (completed):</b> " +
feature.properties.STATS_compleeted + "</br> <b>VM issue date:</b> " +
feature.properties.Date_issued_from_VM + "</br> <b>QC date:</b> " +
feature.properties.ACTUAL_Internal_QC_Date + "</br> <b>VM ECD date:</b> " +
feature.properties.VM_ECD_date + "</br><b>General notes:</b> " +
feature.properties.General_notes + "</br>" +
'</br><center><a href="'+ feature.properties.Directory +'" target="_blank">Local directory</a></center></p>' +
'<center><font color="red"><a href="'+ feature.properties.Sharepoint +'" target="_blank">Sharepoint</a></font></center></p>';
if (feature.properties && feature.properties.popupContent) {
popupContent += feature.properties.popupContent;
}
layer.bindPopup(popupContent);
layer.on({
mouseover: function (e) {
this.setStyle({
'fillColor': '#663300',
'weight':11
});
},
mouseout: function (e) {
mdu.resetStyle(this);
}
}); //layer.on finish
},
filter: function(feature, layer) {
return (feature.properties.Type == "MDU" );
}
})//.addTo(map);
and afterwards:
document.querySelector("input[name=vm]").addEventListener('change', function() {
if(this.checked) {
//if (!map.hasLayer(job2)) map.addLayer(job2); - not needed as the job layer is inactive
if (!map.hasLayer(infill)) map.addLayer(infill);
if (!map.hasLayer(mdu)) map.addLayer(mdu);
if (!map.hasLayer(featuresLayer2)) map.addLayer(featuresLayer2);
if (!map.hasLayer(featuresLayer2a)) map.addLayer(featuresLayer2a);
document.querySelector("input[name=infill]").disabled = false;
document.querySelector("input[name=mdu]").disabled = false;
document.querySelector("input[name=infill]").checked = true;
document.querySelector("input[name=mdu]").checked = true;
}
else {
//if (map.hasLayer(job2)) map.removeLayer(job2); - not needed as the job layer is inactive
if (map.hasLayer(infill)) map.removeLayer(infill);
if (map.hasLayer(mdu)) map.removeLayer(mdu);
if (map.hasLayer(featuresLayer2)) map.removeLayer(featuresLayer2);
if (map.hasLayer(featuresLayer2a)) map.removeLayer(featuresLayer2a);
document.querySelector("input[name=infill]").disabled = true;
document.querySelector("input[name=mdu]").disabled = true;
document.querySelector("input[name=infill]").checked = false;
document.querySelector("input[name=mdu]").checked = false;
}
});
document.querySelector("input[name=infill]").addEventListener('change', function() {
if(this.checked) {
if (!map.hasLayer(infill)) map.addLayer(infill);
if (!map.hasLayer(featuresLayer2)) map.addLayer(featuresLayer2);
}
else {
if (map.hasLayer(infill)) map.removeLayer(infill);
if (map.hasLayer(featuresLayer2)) map.removeLayer(featuresLayer2);
}
});
document.querySelector("input[name=mdu]").addEventListener('change', function() {
if(this.checked) {
if (!map.hasLayer(mdu)) map.addLayer(mdu);
if (!map.hasLayer(featuresLayer2a)) map.addLayer(featuresLayer2a);
}
else {
if (map.hasLayer(mdu)) map.removeLayer(mdu);
if (map.hasLayer(featuresLayer2a)) map.removeLayer(featuresLayer2a);
}
});
This is the one reasonable solution for now, which works. I believe, that we still are able to make the previous combination running.
!map.hasLayer(job)
you can always add the layer. If the map has already the layer it "overwrites" it, it is the same Object and so it is not add additionaly. – Falke Design