Skip to content

Commit 99eeec3

Browse files
wang-boyurht
authored andcommitted
add scale to Leaflet map
1 parent 3873ca3 commit 99eeec3

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

examples/urban_growth/urban_growth/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def render(self, model):
4646
zoom=12.1,
4747
map_height=394,
4848
map_width=531,
49+
scale_options={"imperial": False},
4950
)
5051
urbanized_text = UrbanizedText()
5152
urbanized_chart = mesa.visualization.ChartModule(

mesa_geo/visualization/modules/MapVisualization.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def __init__(
5454
zoom=None,
5555
map_width=500,
5656
map_height=500,
57+
scale_options=None,
5758
):
5859
"""
5960
Create a new MapModule.
@@ -68,14 +69,21 @@ def __init__(
6869
of the space. Default is None.
6970
:param map_width: The width of the map in pixels. Default is 500.
7071
:param map_height: The height of the map in pixels. Default is 500.
72+
:param scale_options: A dictionary of options for the map scale. Default is None (no map scale).
73+
The available options can be found at: https://leafletjs.com/reference.html#control-scale-option
7174
"""
7275
self.portrayal_method = portrayal_method
7376
self._crs = "epsg:4326"
7477

75-
if view is None and zoom is None:
76-
new_element = f"new MapModule(null, null, {map_width}, {map_height})"
77-
else:
78-
new_element = f"new MapModule({view}, {zoom}, {map_width}, {map_height})"
78+
view_js = "null" if view is None else view
79+
zoom_js = "null" if zoom is None else zoom
80+
scale_options_js = (
81+
"null"
82+
if scale_options is None
83+
else str(scale_options).replace("True", "true").replace("False", "false")
84+
)
85+
86+
new_element = f"new MapModule({view_js}, {zoom_js}, {map_width}, {map_height}, {scale_options_js})"
7987
self.js_code = f"elements.push({new_element});"
8088

8189
def render(self, model):

mesa_geo/visualization/templates/js/MapModule.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const MapModule = function (view, zoom, map_width, map_height) {
1+
const MapModule = function (view, zoom, map_width, map_height, scale_options) {
22
// Create the map tag
33
const map_tag = document.createElement("div");
44
map_tag.style.width = map_width + "px";
@@ -16,6 +16,9 @@ const MapModule = function (view, zoom, map_width, map_height) {
1616
if (customView) {
1717
Lmap.setView(view, zoom)
1818
}
19+
if (scale_options !== null) {
20+
L.control.scale(scale_options).addTo(Lmap)
21+
}
1922
let agentLayer = L.geoJSON().addTo(Lmap)
2023

2124
// create the OSM tile layer with correct attribution

0 commit comments

Comments
 (0)