How to remove all featureGroups from a map in leaflet? - leaflet

I have two maps: map_1 and map_2. I added several featureGroups to each one of them. For example:
L.featureGroup({}).addTo(map_1);
Besides that, I add the following to each one of the maps. Each one with different information.
var layer_control = {
base_layers : {
"cartodbpositron" : tile_layer,
},
overlays : {
...
},
};
L.control.layers(
layer_control.base_layers,
layer_control.overlays,
{"autoZIndex": true, "collapsed": true, "position": "topright"}
).addTo(map_1);
How can I remove all the featureGroups and the controls from one of the maps?

Rather than creating anonymous features, create them in variables that you can reference later when you want to remove them.
var myFeatureGroup = L.featureGroup({}).addTo(map_1);
var myLayersControl = L.control.layers(
layer_control.base_layers,
layer_control.overlays,
{"autoZIndex": true, "collapsed": true, "position": "topright"}
).addTo(map_1);
Then later
myFeatureGroup.remove();
myLayersControl.remove();

Related

Add Layer Groups and Markers in a loop ( from json)

I just started working with Leaflet. I want to create a map for a game, so this is a map created with L.CRS.Simple.
I have been able to set the map image, and add marker manually.
Now, I want to create markers and layers groups dynamicly from a json file that I generate in PHP from a sqlite database.
My json is this one for now : https://grounded.dubebenjamin.com/api/markers.json
My json is a list of type(layergroup) inside which I have put another object containing all the markers for the type(layergroup) :
Structure :
{
"landmarks": {
"id": 1,
"slug": "landmark",
"type": "Landmarks",
"markers": [
{"id": 1, "title": "Title of marker 1 of layergroup1"},
{"id": 2, "title": "Title of marker 2 of layergroup1"}
]
},
"science": {
"id": 2,
"slug": "science",
"type": "Science Points",
"markers": [
{"id": 1, "title": "Title of marker 1 of layergroup2"}
]
}
}
= Where landmarks and science are layergroup.
From this json, my plan was to have a first loop, where for each type I want to create a layer group, then create the markers for this layer group and assign the markers to the layer groups.
Manually, I understand the usage of L.marker and L.layerGroup, but where I am stuck, is how to do that in a loop, and defined the layoutgroup name from the json data.
From now, I have just been able to create the marker, but not the layer group. You can see my progress here: https://grounded.dubebenjamin.com/
If you need more precision, just ask me.
Use this code:
var layergroups = {};
fetch('https://grounded.dubebenjamin.com/api/markers.json').then(response => response.json())
.then((data)=>{
layergroups = {};
for(type in data){
var lg = L.layerGroup().addTo(map)
layergroups[type] = lg;
if(data[type].markers){
var markers = data[type].markers;
markers.forEach((marker)=>{
L.marker([marker.x,marker.y]).bindPopup(marker.desc).addTo(lg);
})
}
}
})
First fetch the data from the server, then loop through the layergroups and add them to the map and then to the Object/Array.
Then you create the markers and add them to the layergroup.
You can get the layergroups over the Object layergroups but keep in mind that the request is async.

adding geojson layer results in a grey background which covers base map

Steps to reproduce
Steps to reproduce the behavior:
Go to https://virgilxw.github.io/Singapore_Election_1955/
Click on the top right button on the map
click on "data"
click on "constituencies"
Expected behavior
Polygons should overlay base map
Current behavior
base map covered by grey-colored layer
Leaflet version:1.51.1
Browser (with version):
OS/Platform (with version):
Additional context
``
// Default base Map
map.addLayer(LayerOSM);
// Add geojson
var Layer1955Wards = new L.GeoJSON.AJAX("data/wards1955.geojson");
// Styled Layer Control
var baseMaps = [
{
groupName: "Base Maps",
expanded: true,
layers: {
"OneMap": LayerOneMapSG_Default,
"OSM": LayerOSM
}
}];
var overlays = [
{
groupName: "data",
expaned: "true",
layers: {
"Constituencies": Layer1955Wards
}
},
{
groupName: "Map Overlays",
expanded: "true",
layers: {
"1953 Topological": Layer1953Topo,
"1953 Aerial Photographs": Layer1953Aerial
}
}];
var styledLayerControlOptions = {
container_width: "300px",
container_maxHeight: "350px",
group_maxHeight: "80px",
exclusive: false
};
var styledLayerControl = L.Control.styledLayerControl(baseMaps, overlays, styledLayerControlOptions);
map.addControl(styledLayerControl, overlays);

Repeated tiles when switching from OpenLayers to Leaflet

I am trying to change an OpenLayers 2 call to Leaflet, but when I do the map is displayed fine at zoom level 0, but every time I zoom in, the map doubles from the previous number. Any suggestions as to why? Here is a picture to what its doing.
OpenLayers 2 map options
var options = {
projection: new OpenLayers.Projection("EPSG:3857"),
displayProjection: new OpenLayers.Projection("EPSG:4326"),
units: "m",
maxResolution: 156543.0339,
maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34,
20037508.34, 20037508.34),
controls: [
new OpenLayers.Control.Navigation(
{dragPanOptions: {enableKinetic: true}}
)
]
};
OpenLayers 2 code
var bathyEsri = new OpenLayers.Layer.XYZ(
' ESRI Ocean'
,'http://services.arcgisonline.com/ArcGIS/rest/services/Ocean_Basemap/MapServer/tile/${z}/${y}/${x}.jpg'
,{
sphericalMercator : true
,isBaseLayer : true
,wrapDateLine : true
,opacity : 1
,visibility : true
}
);
Leaflet Options
var options = {
worldCopyJump: true,
maxBounds: L.LatLngBounds([20037508.34,20037508.34],[-20037508.34,-20037508.34]),
crs: L.CRS.EPSG4326,
center: [39.73, -104.99],
zoom: 0
};
Leaflet Code
var bathyEsri = L.tileLayer('http://services.arcgisonline.com/ArcGIS/rest/services/Ocean_Basemap/MapServer/tile/${z}/${y}/${x}.jpg');
Your problem is basically a typo.
Your analysis is also misleading: it's not that the map is being "duplicated", but rather every tile being requested is the 0/0/0 tile. If you use the network inspection tools of your browser, you'll see that the tile URL is something like https://services.arcgisonline.com/ArcGIS/rest/services/Ocean_Basemap/MapServer/tile/$3/$4/$5.jpg , but the tile image corresponds to the /0/0/0.jpg tile.
If you look at those URLs a bit more closely, you'll notice some "extra" $ signs. Why are those there? Well, consider that you wrote your tilelayer URL scheme as
var bathyEsri = L.tileLayer('http://...../tile/${z}/${y}/${x}.jpg');
But keep in mind that the Leaflet documentation clearly states:
var bathyEsri = L.tileLayer('http://...../tile/{z}/{y}/{x}.jpg');
Change this, and everything will magically start working.

Leaflet: Create layers from geojson "properties"?

I've new to mapping, and I've started creating my map following the documentations and tutorials on http://leafletjs.com. So far so good, I've manage to create a map, add a lot of geosjon data from geojson.io that dislay "popupContent" in a popup. And it works.
But I can't understand how create a few layers to sort all those markers. What I want is too use the "properties" to do it. I have properties named "status" with a values "done", "new" and "active".
Like so:
"type": "Feature",
"properties": {
"popupContent": "content",
"marker-color": "#FFFFFF",
"title": "title of project",
"status": "active
Is that possible? To create a 3 layers based on "status" properties?
If you need the html file to look at, say so and I can add it.
Thanks
You can use the filter option of L.geoJson to create separate layers based on the feature properties. Just specify a function that will return true for the features you want included in each layer:
var activeLayer = L.geoJson(yourGeoJson, {
filter: function(feature, layer) {
return (feature.properties.status === "active");
}
}).addTo(map);
var newLayer = L.geoJson(yourGeoJson, {
filter: function(feature, layer) {
return (feature.properties.status === "new");
}
}).addTo(map);
var doneLayer = L.geoJson(yourGeoJson, {
filter: function(feature, layer) {
return (feature.properties.status === "done");
}
}).addTo(map);

Kendo Chart Missing CategoryAxis Text for multiple series

I am creating a kendo chart that can have multiple datasets.
I am creating a chartOptions object that is only being manipulated by referencing the properties and is not manipulated through Kendo functionality. This is done by the following code:
var chartOptions = {
theme: "",
seriesDefaults: {
type: "line"
},
title: {
text: ""
},
legend: {
position: "bottom"
},
series: "",
categoryAxis: {
field: "category"
}
};
function createChart() {
$("#chart").kendoChart(
$.extend(true, {}, chartOptions)
);
}
I also have the user defining which datasets they want. The choose their datasets and create their chart. The chart is then rendered but missing its categoryAxis data.
I am setting the series data (the data comes from the server, but is available for example) in the following way:
dataSetContents.Series = {"Series":[{"name":"2009 Data","data":[{"category":"2008","value":18159},{"category":"2007","value":315},{"category":"2009","value":8}]},{"name":"2008-2010","data":[{"category":"2010","value":750},{"category":"2009","value":2980},{"category":"2008","value":4135},{"category":"2007","value":55}]}]}
chartOptions.series = dataSetContents.Series;
I figured out the reason why I was losing my categories. It has to do with the multiple series, the way to fix this by setting the categoryAxis. This can be done by passing the array of categories like this:
chartOptions.categoryAxis = { categories: [2007,2008,2009,2010] };