I am trying check if Layer already exist on map...
- IF(no) THEN Addlayer on Map.
- IF(yes) THEN removeLayer and AddLayer on map "because I dont want same Layer to be added again and again on map".
I think, I am close but I dont know why its not working... Help Please...
// load GeoJSON from an external file
$.getJSON(url,function(data){
// add GeoJSON layer to the map once the file is loaded
DistrictLayer = L.geoJson(data, {
style: {color: "#000", weight: 2, fillColor: "#03a6fa", fillOpacity: .6},
onEachFeature: function (feature, layer) {
layer.bindPopup("<strong>" + feature.properties['DISTRICT'])
}
})
if(map.hasLayer(DistrictLayer)){
map.removeLayer(DistrictLayer);
map.addLayer(DistrictLayer);
map.fitBounds(DistrictLayer.getBounds());
}
else{
map.addLayer(DistrictLayer);
map.fitBounds(DistrictLayer.getBounds());
}
});