Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions ipyleaflet/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1758,11 +1758,17 @@ class LayersControl(Control):
"""LayersControl class, with Control as parent class.

A control which allows hiding/showing different layers on the Map.
Attributes
----------------------
collapsed: bool, default True
Set whether control should be open or closed by default
"""

_view_name = Unicode('LeafletLayersControlView').tag(sync=True)
_model_name = Unicode('LeafletLayersControlModel').tag(sync=True)

collapsed = Bool(True).tag(sync=True, o=True)


class MeasureControl(Control):
"""MeasureControl class, with Control as parent class.
Expand Down Expand Up @@ -2676,23 +2682,31 @@ def __isub__(self, item):
def __add__(self, item):
return self.add(item)

def add(self, item):
def add(self, item, index=None):
"""Add an item on the map: either a layer or a control.

Parameters
----------
item: Layer or Control instance
The layer or control to add.
index: int
The index to insert a Layer. If not specified, the layer is added to the end (on top).
"""
if hasattr(item, 'as_leaflet_layer'):
item = item.as_leaflet_layer()

if isinstance(item, Layer):
if isinstance(item, dict):
item = basemap_to_tiles(item)

if item.model_id in self._layer_ids:
raise LayerException('layer already on map: %r' % item)
self.layers = tuple([layer for layer in self.layers] + [item])
if index is not None:
if not isinstance(index, int) or index < 0 or index > len(self.layers):
raise ValueError("Invalid index value")
self.layers = tuple(list(self.layers)[:index] + [item] + list(self.layers)[index:])
else:
self.layers = tuple([layer for layer in self.layers] + [item])

elif isinstance(item, Control):
if item.model_id in self._control_ids:
Expand Down
64 changes: 64 additions & 0 deletions ui-tests/notebooks/LayerIndex.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "01b5fa59850e400c907ab3a552ac4e85",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(center=[52.204793, 360.121558], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title'…"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from ipyleaflet import Map, Marker, LayersControl\n",
"\n",
"\n",
"m = Map(center=(52.204793, 360.121558), zoom=15)\n",
"m.add(LayersControl(position='topright', collapsed=False))\n",
"marker = Marker(location=(52.204793, 360.121558), draggable=False, name=\"One\")\n",
"marker2 = Marker(location=(52.204793, 360.131728), draggable=False, name=\"Two\")\n",
"marker3 = Marker(location=(52.204793, 360.111728), draggable=False, name=\"Three\")\n",
"\n",
"\n",
"m.add(marker)\n",
"m.add(marker2)\n",
"m.add(marker3, index=1)\n",
"\n",
"m"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.