dcc.Geolocation

The dcc.Geolocation component can be used to ask for permission from a user for access to location data.

In this example, the first callback updates the location data when the button is selected. The second callback then triggers with the local_date and position data and outputs it to a div.

from dash import Dash, dcc, html, Input, Output, callback

app = Dash(__name__)

app.layout = html.Div(
    [
        html.Button("Update Position", id="update_btn"),
        dcc.Geolocation(id="geolocation"),
        html.Div(id="text_position"),
    ]
)


@callback(Output("geolocation", "update_now"), Input("update_btn", "n_clicks"))
def update_now(click):
    return True if click and click > 0 else False


@callback(
    Output("text_position", "children"),
    Input("geolocation", "local_date"),
    Input("geolocation", "position"),
)
def display_output(date, pos):
    if pos:
        return html.P(
            f"As of {date} your location was: lat {pos['lat']},lon {pos['lon']}, accuracy {pos['accuracy']} meters",
        )
    return "No position data available"


if __name__ == "__main__":
    app.run(debug=True)

Geolocation running example


Geolocation Properties

Access this documentation in your Python terminal with:
```python

help(dash.dcc.Geolocation)
```

Our recommended IDE for writing Dash apps is Dash Enterprise’s
Data Science Workspaces,
which has typeahead support for Dash Component Properties.
Find out if your company is using
Dash Enterprise
.

id (string; optional):
The ID used to identify this component in Dash callbacks.

local_date (string; optional):
The local date and time when the device position was updated. Format:
MM/DD/YYYY, hh:mm:ss p where p is AM or PM.

timestamp (number; optional):
The Unix timestamp from when the position was updated.

position (dict; optional):
The position of the device. lat, lon, and accuracy will always
be returned. The other data will be included when available,
otherwise it will be NaN. lat is latitude in degrees.
lon is longitude in degrees. accuracy is the accuracy of the
lat/lon in meters. * alt is altitude above mean sea level
in meters. alt_accuracy is the accuracy of the altitude in
meters. heading is the compass heading in degrees.
speed is the speed in meters per second.

position is a dict with keys:

  • accuracy (number; optional)

  • alt (number; optional)

  • alt_accuracy (number; optional)

  • heading (number; optional)

  • lat (number; optional)

  • lon (number; optional)

  • speed (number; optional)

position_error (dict; optional):
Position error.

position_error is a dict with keys:

  • code (number; optional)

  • message (string; optional)

show_alert (boolean; default False):
If True, error messages will be displayed as an alert.

update_now (boolean; default False):
Forces a one-time update of the position data. If set to True in a
callback, the browser will update the position data and reset
update_now back to False. This can, for example, be used to update
the position with a button or an interval timer.

high_accuracy (boolean; default False):
If True and if the device is able to provide a more accurate position,
it will do so. Note that this can result in slower response times or
increased power consumption (with a GPS chip on a mobile device for
example). If False (the default value), the device can save resources
by responding more quickly and/or using less power.

maximum_age (number; default 0):
The maximum age in milliseconds of a possible cached position that is
acceptable to return. If set to 0, it means that the device cannot use
a cached position and must attempt to retrieve the real current
position. If set to Infinity the device must return a cached position
regardless of its age. Default: 0.

timeout (number; default Infinity):
The maximum length of time (in milliseconds) the device is allowed to
take in order to return a position. The default value is Infinity,
meaning that data will not be return until the position is available.