How to remove a layer ?
In the map_openstreetmap_custom_geojson viz, replace JS part by:
// Leaflet use
// https://nouvelle-techno.fr/articles/pas-a-pas-inserer-une-carte-openstreetmap-sur-votre-site
// Custom geojson
// https://leafletjs.com/examples/geojson/
({
getShadowDomLocation: function(selector) {
let vizLocation;
// output-viz being the top selector used by the Transform plugin
// Get all the output-viz elements (can have mutliple selector if multiple transfrm vizs in a dashboard)
// selector parameter value must be unque within the DOM
const elements = $('.output-vis');
let shadow;
for (let elem of elements) {
shadow = elem.shadowRoot;
vizLocation = $(shadow).find(selector);
if (vizLocation.length > 0) {
// selector found, exiting
break;
} else {
vizLocation = '.notFound';
}
}
const obj = {
vizLocation: vizLocation,
shadowRoot: shadow
}
// obj object contains the shadowRoot element and and the location of the selector within the shadowRoot
return obj;
},
css: function() {
const css_list = [
"https://use.fontawesome.com/releases/v5.6.3/css/all.css",
"https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
];
for (let css_file of css_list) {
const links = document.head.querySelectorAll("link");
// Already been injected
for(let l in links) if(links[l].href == css_file) return;
const link = document.createElement('link');
link.rel = "stylesheet";
link.href = css_file;
document.head.appendChild(link);
}
},
before_render: function() {
// Set geo points
let locationsForLayer1 = [
{
"id": "1",
"name": "Paris",
"lat": "48.8566",
"lon": "2.3522",
"size": "4",
"class": "myDivIconPurple"
},
{
"id": "3",
"name": "Toulouse",
"lat": "43.6045",
"lon": " 1.4440",
"size": "2",
"class": "myDivIconPurple"
}, {
"id": "4",
"name": "Grenoble",
"lat": "45.1715",
"lon": "5.7224",
"size": "3",
"class": "myDivIconPurple"
}
]
let locationsForLayer2 = [
{
"id": "2",
"name": "Nice",
"lat": "43.7034",
"lon": " 7.2663",
"size": "2",
"class": "myDivIcon"
},
{
"id": "5",
"name": "Bordeaux",
"lat": "44.8400",
"lon": "-0.5800",
"size": "2",
"class": "myDivIcon"
}
]
response = {
"locationsForLayer1": locationsForLayer1,
"locationsForLayer2": locationsForLayer2
}
},
after_render: function() {
const meta = this.meta;
let locationsForLayer1 = response.locationsForLayer1;
let locationsForLayer2 = response.locationsForLayer2;
$.getScript("https://cdn.jsdelivr.net/npm/leaflet@1.7.1/dist/leaflet.js")
.done(function(script, textStatus) {
$.getScript("https://cdn.jsdelivr.net/gh/lguillaud/osd_transform_vis@main/geojson/departements.geojson.js")
.done(function(script, textStatus) {
// Get geojson into json object
let myGeoJson = JSON.parse(departements);
// Create HTML container for map
// Rebuild each time to avoid multiple maps layers
let myDiv = meta.getShadowDomLocation("#myDiv").vizLocation;
$(myDiv).empty();
$(myDiv).append('<div id="map"></div>')
let mapLocation = meta.getShadowDomLocation("#map").vizLocation[0];
// Create map object
let map=new L.map(mapLocation);
// Add GeoJSON layer to map
// Set options
let myStyle = {
"color": "#ff7800",
"weight": 3,
"opacity": 0.15
};
L.geoJSON(myGeoJson, { style: myStyle }).addTo(map);
// Maps location
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'data © <a href="//osm.org/copyright">OpenStreetMap</a>/ODbL - render <a href="//openstreetmap.fr">OSM France</a>',
minZoom: 1,
maxZoom: 20
}).addTo(map);
// Create markers group
let markerGroup1 = L.layerGroup();
let markerGroup2 = L.layerGroup();
// Markers creation layer 1
let markers = [];
for (let location in locationsForLayer1) {
// Icon to use
let myIcon = L.divIcon({
html: '<i class="fas fa-circle fa-' + locationsForLayer1[location].size + 'x"></i>',
iconSize: [20, 20],
className: locationsForLayer1[location].class
});
// If custom icon
let marker = L.marker([locationsForLayer1[location].lat, locationsForLayer1[location].lon], { icon: myIcon })
// Default icon
//let marker = L.marker([locations[location].lat, locations[location].lon])
.addTo(map)
.bindPopup(locationsForLayer1[location].name);
// Add marker to group
markerGroup1.addLayer(marker);
// Add marker to list of markers
markers.push(marker);
}
// Markers creation layer 2
markers = [];
for (let location in locationsForLayer2) {
// Icon to use
let myIcon = L.divIcon({
html: '<i class="fas fa-circle fa-' + locationsForLayer2[location].size + 'x"></i>',
iconSize: [20, 20],
className: locationsForLayer2[location].class
});
// If custom icon
let marker = L.marker([locationsForLayer2[location].lat, locationsForLayer2[location].lon], { icon: myIcon })
// Default icon
//let marker = L.marker([locations[location].lat, locations[location].lon])
.addTo(map)
.bindPopup(locationsForLayer2[location].name);
// Add marker to group
markerGroup2.addLayer(marker);
// Add marker to list of markers
markers.push(marker);
}
// Create marker group for zoom
let group = new L.featureGroup(markers);
// Make all markers visible, using pad to avoid marker being cut
map.fitBounds(group.getBounds().pad(0.3));
map.addLayer(markerGroup1);
map.addLayer(markerGroup2);
// Manage Button
const shadowMyButtonLocation = meta.getShadowDomLocation("#myBtn").vizLocation[0];
$(shadowMyButtonLocation).unbind()
$(shadowMyButtonLocation).click(function() {
map.removeLayer(markerGroup2);
})
});
});
}
})
In the template part, replace all by:
<style>
#map { height: 600px; }
.myDivIcon {
text-align: center; /* Horizontally center the text (icon) */
line-height: 20px; /* Vertically center the text (icon) */
color: blue;
}
.myDivIconPurple {
text-align: center; /* Horizontally center the text (icon) */
line-height: 20px; /* Vertically center the text (icon) */
color: purple;
}
</style>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin=""/>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<div id="myDiv">
</div>
<button class='btn btn-primary active' id="myBtn">Remove blue points</button>