Skip to content

Commit

Permalink
Add position to add method
Browse files Browse the repository at this point in the history
  • Loading branch information
gjmooney committed Dec 14, 2023
1 parent 1b37189 commit 3fc8223
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ipyleaflet/leaflet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2676,13 +2676,15 @@ def __isub__(self, item):
def __add__(self, item):
return self.add(item)

def add(self, item):
def add(self, item, position=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.
position: int
The position 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()
Expand All @@ -2692,7 +2694,13 @@ def add(self, item):
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 position is not None:
if not isinstance(position, int) or position < 0 or position > len(self.layers):
raise ValueError("Invalid position value")
self.layers = tuple(list(self.layers)[:position] + [item] + list(self.layers)[position:])
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

0 comments on commit 3fc8223

Please sign in to comment.