|
| 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) |
0 commit comments