Geolocator
Examples#
Example 1#
from typing import Callable
import flet as ft
import flet_geolocator as ftg
async def main(page: ft.Page):
page.scroll = ft.ScrollMode.ADAPTIVE
page.appbar = ft.AppBar(title=ft.Text("Geolocator Tests"))
def handle_position_change(e: ftg.GeolocatorPositionChangeEvent):
page.add(ft.Text(f"New position: {e.position.latitude} {e.position.longitude}"))
def get_dialog(handler: Callable):
return ft.AlertDialog(
adaptive=True,
title="Opening Location Settings...",
content=ft.Text(
"You are about to be redirected to the location/app settings. "
"Please locate this app and grant it location permissions."
),
actions=[ft.TextButton("Take me there", on_click=handler)],
actions_alignment=ft.MainAxisAlignment.CENTER,
)
def show_snackbar(message):
page.show_dialog(ft.SnackBar(ft.Text(message)))
async def handle_permission_request(e: ft.Event[ft.OutlinedButton]):
p = await geo.request_permission(timeout=60)
page.add(ft.Text(f"request_permission: {p}"))
show_snackbar(f"Permission request sent: {p}")
async def handle_get_permission_status(e: ft.Event[ft.OutlinedButton]):
p = await geo.get_permission_status()
show_snackbar(f"Permission status: {p}")
async def handle_get_current_position(e: ft.Event[ft.OutlinedButton]):
p = await geo.get_current_position()
show_snackbar(f"Current position: ({p.latitude}, {p.longitude})")
async def handle_get_last_known_position(e):
p = await geo.get_last_known_position()
show_snackbar(f"Last known position: ({p.latitude}, {p.longitude})")
async def handle_location_service_enabled(e):
p = await geo.is_location_service_enabled()
show_snackbar(f"Location service enabled: {p}")
async def handle_open_location_settings(e: ft.Event[ft.OutlinedButton]):
p = await geo.open_location_settings()
page.close(location_settings_dlg)
if p is True:
show_snackbar("Location settings opened successfully.")
else:
show_snackbar("Location settings could not be opened.")
async def handle_open_app_settings(e: ft.Event[ft.OutlinedButton]):
p = await geo.open_app_settings()
page.close(app_settings_dlg)
if p:
show_snackbar("App settings opened successfully.")
else:
show_snackbar("App settings could not be opened.")
location_settings_dlg = get_dialog(handle_open_location_settings)
app_settings_dlg = get_dialog(handle_open_app_settings)
geo = ftg.Geolocator(
configuration=ftg.GeolocatorConfiguration(
accuracy=ftg.GeolocatorPositionAccuracy.LOW
),
on_position_change=handle_position_change,
on_error=lambda e: page.add(ft.Text(f"Error: {e.data}")),
)
page.services.append(geo)
page.add(
ft.Row(
wrap=True,
controls=[
ft.OutlinedButton(
content="Request Permission",
on_click=handle_permission_request,
),
ft.OutlinedButton(
content="Get Permission Status",
on_click=handle_get_permission_status,
),
ft.OutlinedButton(
content="Get Current Position",
on_click=handle_get_current_position,
),
ft.OutlinedButton(
content="Get Last Known Position",
visible=not page.web,
on_click=handle_get_last_known_position,
),
ft.OutlinedButton(
content="Is Location Service Enabled",
on_click=handle_location_service_enabled,
),
ft.OutlinedButton(
content="Open Location Settings",
visible=not page.web, # (1)!
on_click=lambda e: page.open(location_settings_dlg),
),
ft.OutlinedButton(
content="Open App Settings",
visible=not page.web, # (1)!
on_click=lambda e: page.open(app_settings_dlg),
),
],
)
)
ft.app(main)
Geolocator
#
Bases: Service
A control that allows you to fetch GPS data from your device.
This control is non-visual and should be added to
Page.overlay
list.
on_error
#
on_error: ControlEventHandler[Geolocator] | None = None
Fires when an error occurs.
The data
property of the event
handler argument contains information on the error.
on_position_change
#
on_position_change: (
EventHandler[GeolocatorPositionChangeEvent] | None
) = None
Fires when the position of the device changes.
parent
#
parent: BaseControl | None
The direct ancestor(parent) of this control.
It defaults to None
and will only have a value when this control is mounted
(added to the page tree).
The Page
control (which is the root of the tree) is an exception - it always
has parent=None
.
position
#
position: GeolocatorPosition | None = field(
default=None, init=False
)
The current position of the device. (read-only)
Starts as None
and will be updated when the position changes.
before_update
#
This method is called every time when this control is being updated.
/// details | Note
Make sure not to call/request an update()
here.
///
build
#
Called once during control initialization to define its child controls. self.page is available in this method.
distance_between
#
distance_between(
start_latitude: Number,
start_longitude: Number,
end_latitude: Number,
end_longitude: Number,
timeout: float = 10,
)
Calculates the distance between the supplied coordinates in meters.
The distance between the coordinates is calculated using the Haversine formula (see https://en.wikipedia.org/wiki/Haversine_formula).
PARAMETER | DESCRIPTION |
---|---|
start_latitude
|
The latitude of the starting point, in degrees.
TYPE:
|
start_longitude
|
The longitude of the starting point, in degrees.
TYPE:
|
end_latitude
|
The latitude of the ending point, in degrees.
TYPE:
|
end_longitude
|
The longitude of the ending point, in degrees.
TYPE:
|
timeout
|
The maximum amount of time (in seconds) to wait for a response.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
The distance between the coordinates in meters. |
RAISES | DESCRIPTION |
---|---|
TimeoutError
|
If the request times out. |
get_current_position
#
get_current_position(
configuration: GeolocatorConfiguration | None = None,
timeout: float = 30,
) -> GeolocatorPosition
Gets the current position of the device with the desired accuracy and settings.
Note
Depending on the availability of different location services,
this can take several seconds. It is recommended to call the
get_last_known_position
method first to receive a
known/cached position and update it with the result of the
get_current_position
method.
PARAMETER | DESCRIPTION |
---|---|
configuration
|
Additional configuration for the location request.
If not specified, then the
TYPE:
|
timeout
|
The maximum amount of time (in seconds) to wait for a response.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
GeolocatorPosition
|
The current position of the device as a |
RAISES | DESCRIPTION |
---|---|
TimeoutError
|
If the request times out. |
get_last_known_position
#
get_last_known_position(
timeout: float = 10,
) -> GeolocatorPosition
Gets the last known position stored on the user's device.
The accuracy can be defined using the
Geolocator.configuration
property.
Note
This method is not supported on web plaform.
PARAMETER | DESCRIPTION |
---|---|
timeout
|
The maximum amount of time (in seconds) to wait for a response.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
GeolocatorPosition
|
The last known position of the device as a |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If invoked on a web platform. |
TimeoutError
|
If the request times out. |
get_permission_status
#
get_permission_status(
timeout: float = 10,
) -> GeolocatorPermissionStatus
Gets which permission the app has been granted to access the device's location.
PARAMETER | DESCRIPTION |
---|---|
timeout
|
The maximum amount of time (in seconds) to wait for a response.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
GeolocatorPermissionStatus
|
The status of the permission. |
RAISES | DESCRIPTION |
---|---|
TimeoutError
|
If the request times out. |
is_location_service_enabled
#
Checks if location service is enabled.
PARAMETER | DESCRIPTION |
---|---|
timeout
|
The maximum amount of time (in seconds) to wait for a response.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
RAISES | DESCRIPTION |
---|---|
TimeoutError
|
If the request times out. |
open_app_settings
#
Attempts to open the app's settings.
Note
This method is not supported on web plaform.
PARAMETER | DESCRIPTION |
---|---|
timeout
|
The maximum amount of time (in seconds) to wait for a response.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If invoked on a web platform. |
TimeoutError
|
If the request times out. |
open_location_settings
#
Attempts to open the device's location settings.
Note
This method is not supported on web plaform.
PARAMETER | DESCRIPTION |
---|---|
timeout
|
The maximum amount of time (in seconds) to wait for a response.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If invoked on a web platform. |
TimeoutError
|
If the request times out. |
request_permission
#
request_permission(
timeout: int = 60,
) -> GeolocatorPermissionStatus
Requests the device for access to the device's location.
PARAMETER | DESCRIPTION |
---|---|
timeout
|
The maximum amount of time (in seconds) to wait for a response.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
GeolocatorPermissionStatus
|
The status of the permission request. |
RAISES | DESCRIPTION |
---|---|
TimeoutError
|
If the request times out. |