Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map Control #3093

Merged
merged 37 commits into from
May 23, 2024
Merged

Map Control #3093

merged 37 commits into from
May 23, 2024

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Apr 24, 2024

Closes #1193

Test Code:

import random

import flet as ft
import flet.map as map


def main(page: ft.Page):
    page.theme_mode = ft.ThemeMode.LIGHT

    marker_layer_ref = ft.Ref[map.MarkerLayer]()
    circle_layer_ref = ft.Ref[map.CircleLayer]()

    def handle_tap(e: map.TapEvent):
        print(
            f"Name: {e.name} - Location: {e.coordinates} - Local: ({e.local_x}, {e.local_y}) - Global: ({e.global_x}, {e.global_y})"
        )
        if e.name == "tap":
            marker_layer_ref.current.markers.append(
                map.Marker(
                    content=ft.Icon(
                        ft.icons.LOCATION_ON, color=ft.cupertino_colors.DESTRUCTIVE_RED
                    ),
                    coordinates=e.coordinates,
                )
            )
        elif e.name == "secondary_tap":
            circle_layer_ref.current.circles.append(
                map.CircleMarker(
                    radius=random.randint(5, 10),
                    coordinates=e.coordinates,
                    color=ft.colors.random_color(),
                    border_color=ft.colors.random_color(),
                    border_stroke_width=4,
                )
            )
        page.update()

    def handle_event(e: map.MapEvent):
        print(
            f"{e.name} - Source: {e.source} - Center: {e.center} - Zoom: {e.zoom} - Rotation: {e.rotation}"
        )

    page.add(
        ft.Text("Click anywhere to add a Marker, right-click to add a CircleMarker."),
        map.Map(
            expand=True,
            configuration=map.MapConfiguration(
                initial_center=map.MapLatitudeLongitude(15, 10),
                initial_zoom=4.2,
                interaction_configuration=map.MapInteractionConfiguration(
                    flags=map.MapInteractiveFlag.ALL
                ),
                on_init=lambda e: print(f"Initialized Map"),
                on_tap=handle_tap,
                on_secondary_tap=handle_tap,
                on_long_press=handle_tap,
                on_event=handle_event,
            ),
            layers=[
                map.TileLayer(
                    url_template="https://tile.openstreetmap.org/{z}/{x}/{y}.png",
                    on_image_error=lambda e: print("TileLayer Error"),
                ),
                map.RichAttribution(
                    popup_border_radius=ft.border_radius.all(10),
                    popup_initial_display_duration=5,
                    attributions=[
                        map.TextSourceAttribution(
                            text="OpenStreetMap Contributors",
                            on_click=lambda e: e.page.launch_url(
                                "https://openstreetmap.org/copyright"
                            ),
                        ),
                        map.TextSourceAttribution(
                            text="Flet",
                            on_click=lambda e: e.page.launch_url("https://flet.dev"),
                        ),
                    ]
                ),
                map.SimpleAttribution(
                    text="Flet",
                    alignment=ft.alignment.top_right,
                    on_click=lambda e: print("Clicked SimpleAttribution"),
                ),
                map.MarkerLayer(
                    ref=marker_layer_ref,
                    markers=[
                        map.Marker(
                            content=ft.Icon(ft.icons.LOCATION_ON),
                            coordinates=map.MapLatitudeLongitude(30, 15),
                        ),
                        map.Marker(
                            content=ft.Icon(ft.icons.LOCATION_ON),
                            coordinates=map.MapLatitudeLongitude(10, 10),
                        ),
                        map.Marker(
                            content=ft.Icon(ft.icons.LOCATION_ON),
                            coordinates=map.MapLatitudeLongitude(25, 45),
                        ),
                    ],
                ),
                map.CircleLayer(
                    ref=circle_layer_ref,
                    circles=[
                        map.CircleMarker(
                            radius=10,
                            coordinates=map.MapLatitudeLongitude(16, 24),
                            color=ft.colors.RED,
                            border_color=ft.colors.BLUE,
                            border_stroke_width=4,
                        ),
                    ],
                ),
                map.PolygonLayer(
                    polygons=[
                        map.PolygonMarker(
                            label="Popular Touristic Area",
                            label_text_style=ft.TextStyle(
                                color=ft.colors.BLACK,
                                size=15,
                                weight=ft.FontWeight.BOLD,
                            ),
                            color=ft.colors.with_opacity(0.3, ft.colors.BLUE),
                            coordinates=[
                                map.MapLatitudeLongitude(10, 10),
                                map.MapLatitudeLongitude(30, 15),
                                map.MapLatitudeLongitude(25, 45),
                            ],
                        ),
                    ],
                ),
                map.PolylineLayer(
                    polylines=[
                        map.PolylineMarker(
                            border_stroke_width=3,
                            border_color=ft.colors.RED,
                            gradient_colors=[ft.colors.BLACK, ft.colors.BLACK],
                            color=ft.colors.with_opacity(0.6, ft.colors.GREEN),
                            stroke_pattern=map.DottedStrokePattern(5),
                            coordinates=[
                                map.MapLatitudeLongitude(10, 10),
                                map.MapLatitudeLongitude(30, 15),
                                map.MapLatitudeLongitude(25, 45),
                            ],
                        ),
                    ],
                ),
            ],
        ),
    )


ft.app(target=main)

@ndonkoHenri ndonkoHenri marked this pull request as ready for review April 30, 2024 16:14
@FeodorFitsner FeodorFitsner merged commit d97a135 into main May 23, 2024
2 checks passed
@ndonkoHenri ndonkoHenri deleted the map branch May 25, 2024 13:07
@MrGoosee
Copy link

Hello, in flet it is possible to add a map to Hello, in flet it is possible to add a map to controls=[]?

@ndonkoHenri
Copy link
Contributor Author

Hello, in flet it is possible to add a map to Hello, in flet it is possible to add a map to controls=[]?

Map is a Flet control, so yeah, you can basically use it anywhere.

@MrGoosee
Copy link

Thank you

zrr1999 pushed a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
* Map: initial commit

* SafeArea: deprecate minimum and rename to minimum_padding

* parseDoubleFromJson, parseIntFromJson, parseBoolFromJson

* parseColorFromJson

* Map utils: parseMapOptions, parseMapChildren

* Map: MapOption, MapTileLayer

* RichAttribution: initial commit

* TextSourceAttribution: initial commit

* MapTileLayer: separate from map

* Map: update create_control.dart

* alignment utils: add defValue

* Stack: create before_update()

* MapSimpleAttribution: initial commit

* MapRichAttribution: renamed

* MapCircleLayer: initial commit

* MapMarkerLayer: initial commit

* MapTileLayer: update props

* MapTextSourceAttribution: renamed

* Cleanup

* MapOption: renamed to MapConfiguration

* Create map module

* TileLayer: error_image_src, on_image_error

* MapConfiguration: implemented as control separate module

* MapEventSource enum

* rename point to location

* rename Circle to CircleMarker, add docstrings, remove opacity

* remove parseIntFromJson, parseBoolFromJson, parseDoubleFromJson

* MapInteractionConfiguration and parseInteractionOptions

* add default values

* TileLayer: enable_retina_mode, tile_bounds, evict_error_tile_strategy, subdomains, additional_options

* MapInteractionConfiguration: flags, rotation_win_gestures, pinch_move_win_gestures, pinch_zoom_win_gestures

* move StrokeCap to types.py | create StrokeJoin

* implement Polyline* and Polygon*

* Bump deps

---------

Co-authored-by: Feodor Fitsner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Map control
3 participants