There is not a lot of examples out there working on ipyleaflet layers. Here in my mock up I have a map with a rectangle on it. I want to have a user input where the user can specify a new center point for a "square" rectangle on a ipyleaflet map.
When I move my latitude, my observer sees the event and redraws the map, but then immediately goes into an error state. What I expected was that the first time I make a change in the latitude center position, my observe function would properly move my square rectangle because my "rectangle" object is a global object.
So after first time I have:
This action throws the error:
So my questions are as follows:
- Can anyone tell me how to determine what layer.model_id I have in my map?
- Can anyone suggest a way to multiple times substitute one layer for another?
- Why does it move then immediately give an error? I expected first time it would work, but second time through it would fail?
Thank you-- really trying to understand the world of ipyleaflet. Please note, I tried to make this as simple as possible, but still lots of code. Below is the full program including my observer function:
import ipywidgets as widgets
from ipyleaflet import Map, basemaps, Circle, Rectangle, LayerGroup
from IPython.display import display
from ipywidgets import Button, HBox, VBox, Layout
spacer_box = HBox(layout={"padding": "4px 4px 4px 4px"})
center_pt_lat = widgets.BoundedFloatText(value=21, min=0, max=90, step=5.0,
disabled=False, description="Latitude", layout=Layout(width="140px"))
center_pt_lon = widgets.BoundedFloatText(value=-157, min=-180, max=180, disabled=False,
description="Longitude",layout=Layout(width="150px"))
input_box = HBox([center_pt_lat,spacer_box,center_pt_lon])
def on_value_change(b):
lat = center_pt_lat.value
lon = center_pt_lon.value
llat = lat - 5.0
rlat = lat + 5.0
llon = lon - 5.0
rlon = lon + 5.0
center = (lat, lon)
m.center=center
box = ((llat,llon),(rlat,rlon))
rectangle_new = Rectangle(bounds=box, color='green', fill=False, weight=2)
layer_group.substitute(rectangle, rectangle_new)
center_pt_lat.observe(on_value_change)
center = (26.0, -156.)
m = Map(center=center, zoom=4, basemap=basemaps.Esri.DeLorme)
m.layout.height= '300px'
m.layout.width = '300px'
box = ((21, -151), (31, -161))
rectangle = Rectangle(bounds=box, color='green', fill=False, weight=2)
circle = Circle(location=center, radius=500, color="yellow", fill_color="yellow")
# Create layer group
layer_group = LayerGroup(layers=(rectangle, circle))
m.add(layer_group)
dashboard = VBox([input_box, m])
display(dashboard)