Skip to content

Commit 5a4e8d1

Browse files
committed
idle_camera.py example
1 parent 46fb9d7 commit 5a4e8d1

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import flet as ft
2+
import flet_map as ftm
3+
4+
5+
def main(page: ft.Page):
6+
page.title = "Map camera controls"
7+
page.padding = 16
8+
9+
async def update_camera_status(trigger: str):
10+
camera = await my_map.get_camera()
11+
camera_status.value = (
12+
f"Camera [{trigger}]: "
13+
f"center=({camera.center.latitude:.5f}, {camera.center.longitude:.5f}), "
14+
f"zoom={camera.zoom:.2f}, rotation={camera.rotation:.1f}"
15+
)
16+
page.update()
17+
18+
async def zoom_in(e: ft.Event[ft.Button]):
19+
await my_map.zoom_in()
20+
await update_camera_status("zoom_in")
21+
22+
async def zoom_out(e: ft.Event[ft.Button]):
23+
await my_map.zoom_out()
24+
await update_camera_status("zoom_out")
25+
26+
async def rotate_plus_15(e: ft.Event[ft.Button]):
27+
await my_map.rotate_from(15)
28+
await update_camera_status("rotate_from(+15)")
29+
30+
async def reset_rotation(e: ft.Event[ft.Button]):
31+
await my_map.reset_rotation()
32+
await update_camera_status("reset_rotation")
33+
34+
async def center_berlin(e: ft.Event[ft.Button]):
35+
await my_map.center_on(point=ftm.MapLatitudeLongitude(52.52, 13.405), zoom=12)
36+
await update_camera_status("center_on(berlin)")
37+
38+
async def move_tokyo(e: ft.Event[ft.Button]):
39+
await my_map.move_to(
40+
destination=ftm.MapLatitudeLongitude(35.6762, 139.6503), zoom=11
41+
)
42+
await update_camera_status("move_to(tokyo)")
43+
44+
async def set_world_zoom(e: ft.Event[ft.Button]):
45+
await my_map.zoom_to(3)
46+
await update_camera_status("zoom_to(3)")
47+
48+
page.add(
49+
ft.Column(
50+
expand=True,
51+
controls=[
52+
ft.Text("Camera controls", size=20, weight=ft.FontWeight.BOLD),
53+
ft.Text(
54+
"Use buttons to control map camera programmatically.",
55+
size=12,
56+
),
57+
ft.Row(
58+
wrap=True,
59+
spacing=8,
60+
run_spacing=8,
61+
controls=[
62+
ft.Button("Zoom in", on_click=zoom_in),
63+
ft.Button("Zoom out", on_click=zoom_out),
64+
ft.Button("Rotate +15°", on_click=rotate_plus_15),
65+
ft.Button("Reset rotation", on_click=reset_rotation),
66+
ft.Button("Center Berlin", on_click=center_berlin),
67+
ft.Button("Move to Tokyo", on_click=move_tokyo),
68+
ft.Button("World zoom (3)", on_click=set_world_zoom),
69+
],
70+
),
71+
camera_status := ft.Text(selectable=True, font_family="monospace"),
72+
my_map := ftm.Map(
73+
expand=True,
74+
initial_center=ftm.MapLatitudeLongitude(52.52, 13.405),
75+
initial_zoom=5,
76+
layers=[
77+
ftm.TileLayer(
78+
url_template="https://tile.memomaps.de/tilegen/{z}/{x}/{y}.png"
79+
),
80+
ftm.SimpleAttribution(
81+
text="OpenStreetMap contributors",
82+
on_click=lambda e: e.page.launch_url(
83+
"https://www.openstreetmap.org/copyright"
84+
),
85+
),
86+
ftm.MarkerLayer(
87+
markers=[
88+
ftm.Marker(
89+
coordinates=ftm.MapLatitudeLongitude(52.52, 13.405),
90+
content=ft.Icon(ft.Icons.LOCATION_ON),
91+
),
92+
ftm.Marker(
93+
coordinates=ftm.MapLatitudeLongitude(
94+
35.6762, 139.6503
95+
),
96+
content=ft.Icon(ft.Icons.LOCATION_ON),
97+
),
98+
]
99+
),
100+
],
101+
),
102+
],
103+
)
104+
)
105+
106+
107+
ft.run(main)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import flet as ft
2+
import flet_map as ftm
3+
4+
IDLE_EVENT_TYPES = {
5+
ftm.MapEventType.MOVE_END,
6+
ftm.MapEventType.FLING_ANIMATION_END,
7+
ftm.MapEventType.FLING_ANIMATION_NOT_STARTED,
8+
ftm.MapEventType.DOUBLE_TAP_ZOOM_END,
9+
ftm.MapEventType.ROTATE_END,
10+
}
11+
12+
13+
def main(page: ft.Page):
14+
page.title = "Map camera idle pattern"
15+
page.padding = 16
16+
17+
async def handle_map_event(e: ftm.MapEvent):
18+
last_event.value = (
19+
"Last event: "
20+
f"type={e.event_type}, source={e.source.value}, zoom={e.camera.zoom:.2f}"
21+
)
22+
23+
if e.event_type in IDLE_EVENT_TYPES: # here the camera is settled/idle
24+
camera = await e.control.get_camera()
25+
settled_camera.value = (
26+
"Settled camera: "
27+
f"center=({camera.center.latitude:.3f}, "
28+
f"{camera.center.longitude:.3f}), "
29+
f"zoom={camera.zoom:.2f}, rotation={camera.rotation:.1f}, "
30+
f"trigger={e.event_type}"
31+
)
32+
33+
page.update()
34+
35+
page.add(
36+
ft.Column(
37+
expand=True,
38+
controls=[
39+
ft.Text("Camera idle pattern", size=20, weight=ft.FontWeight.BOLD),
40+
ft.Text(
41+
"Drag, fling, rotate, zoom and watch when the camera is settled.",
42+
size=12,
43+
),
44+
last_event := ft.Text(
45+
"Last event: -", selectable=True, font_family="monospace"
46+
),
47+
settled_camera := ft.Text(
48+
"Settled camera: -", selectable=True, font_family="monospace"
49+
),
50+
ftm.Map(
51+
expand=True,
52+
initial_center=ftm.MapLatitudeLongitude(
53+
latitude=52.52, longitude=13.405
54+
),
55+
initial_zoom=11,
56+
on_event=handle_map_event,
57+
interaction_configuration=ftm.InteractionConfiguration(
58+
flags=ftm.InteractionFlag.ALL
59+
),
60+
layers=[
61+
ftm.TileLayer(
62+
url_template="https://tile.memomaps.de/tilegen/{z}/{x}/{y}.png"
63+
),
64+
ftm.SimpleAttribution(
65+
text="OpenStreetMap contributors",
66+
on_click=lambda e: e.page.launch_url(
67+
"https://www.openstreetmap.org/copyright"
68+
),
69+
),
70+
],
71+
),
72+
],
73+
)
74+
)
75+
76+
77+
ft.run(main)
File renamed without changes.

0 commit comments

Comments
 (0)