Ipyleaflet intro example
Documentation for getting started with ipyleaflet:
https://ipyleaflet.readthedocs.io
Video tutorial for this:
Create default interactive map¶
In [1]:
# import the package
import ipyleaflet
In [2]:
# define m as a default map
m = ipyleaflet.Map()
# display map
m
Customize default map settings¶
In [3]:
# import some classes from the package, so "ipyleaflet." no longer needs to be typed just before them
from ipyleaflet import Map, FullScreenControl, LayersControl, DrawControl, MeasureControl, ScaleControl
In [4]:
# define a map with new center and zoom settings
m = Map(center=[30, -85], zoom=3, scroll_wheel_zoom=True)
# set display height at 500 pixels
m.layout.height="500px"
# display map
m
Add widget controls to interactive map interface¶
In [5]:
# add full screen control, default position is top left
m.add_control(FullScreenControl())
In [6]:
# add layers control
m.add_control(LayersControl(position="topright"))
In [7]:
# add draw control
m.add_control(DrawControl(position="topleft"))
In [8]:
# add measure control
m.add_control(MeasureControl())
In [9]:
# add scale control
m.add_control(ScaleControl(position="bottomleft"))
Add basemaps¶
In [10]:
# import some classes from the package, so "ipyleaflet." no longer needs to be typed just before them
from ipyleaflet import basemaps, TileLayer
In [11]:
# add OpenTopoMap basemap layer
m.add_layer(basemaps.OpenTopoMap)
In [12]:
# add Esri.WorldImagery basemap layer
m.add_layer(basemaps.Esri.WorldImagery)
In [13]:
# display map
m
In [14]:
# define a tile layer for Google Maps
google_map = TileLayer(
url="https://mt1.google.com/vt/lyrs=m&x={x}&y={y}&z={z}",
attribution="Google",
name="Google Maps",
)
In [15]:
# add layer to map
m.add_layer(google_map)
In [16]:
# define a tile layer for Google Satellite Imagery
google_satellite = TileLayer(
url="https://mt1.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
attribution="Google",
name="Google Satellite"
)
In [17]:
# add layer to map
m.add_layer(google_satellite)
In [18]:
# display map
m
In [19]:
# disable the map attribution label
m.attribution_control = False
Add markers¶
In [20]:
# import marker class from package
from ipyleaflet import Marker
In [21]:
# define three markers
marker1 = Marker(name='marker1', location=(40, -100))
marker2 = Marker(name='marker2', location=(30, -90))
marker3 = Marker(name='marker3', location=(20, -80))
# add them as layers
m.add_layer(marker1)
m.add_layer(marker2)
m.add_layer(marker3)
# display map
m
Add marker cluster¶
In [22]:
# import classes from package
from ipyleaflet import Map, Marker, MarkerCluster
In [23]:
# define three markers
marker1 = Marker(name='marker1', location=(50, -100))
marker2 = Marker(name='marker2', location=(30, -110))
marker3 = Marker(name='marker3', location=(40, -90))
# define marker cluster
marker_cluster = MarkerCluster(
markers=(marker1, marker2, marker3), name="marker cluster"
)
# add marker cluster as map layer
m.add_layer(marker_cluster)
# display map
m
Last update: 2021-05-07