CurrentLocation
Group: Place
Description
CurrentLocation asks the visitor to share where they are. It
shows a "Use my location" button; when the visitor agrees, the
browser asks for permission, and the resulting position is
submitted with the form under the component's name as a Location
value. The visitor can always say no, so your route must handle
both answers: a Location carries a status field telling you
whether coordinates are actually there.
Syntax
CurrentLocation(name)
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
name |
str |
required | The field's name, matching a parameter of the receiving route. |
show |
bool |
True |
Display the permission button and status text. |
show_coordinates |
bool |
False |
Also display the latitude and longitude once granted. |
enable_high_accuracy |
bool |
True |
Prefer the most precise position the device can give. |
timeout |
int |
10000 |
How long to wait for a position, in milliseconds. |
maximum_age |
int |
0 |
Accept a cached position up to this many milliseconds old. |
on_locate |
route name or route function | none | Route to call when a position (or a failure) is resolved. |
on_error |
route name or route function | none | Route to call when finding the location fails for any reason. |
on_denied |
route name or route function | none | Route to call when the visitor refuses permission. |
on_timeout |
route name or route function | none | Route to call when the request runs out of time. |
Examples
The route checks status before using the coordinates, and the
component never raises an error, so no special error handling is
needed:
from drafter import *
from dataclasses import dataclass
@dataclass
class State:
report: str
@route
def index(state: State) -> Page:
return Page(state, [
Header("Lost Cat Reporter"),
"Spotted Captain? Share where you are.\n",
CurrentLocation("where", show_coordinates=True),
"\n",
Button("Report sighting", "report"),
"\n" + state.report
])
@route
def report(state: State, where: Location) -> Page:
if where.status == "granted":
state.report = ("Sighting recorded at "
+ str(where.latitude) + ", "
+ str(where.longitude))
else:
state.report = "No location shared: " + where.status
return index(state)
start_server(State(""))
Notes
- The
statusfield is one of"prompt"(not asked yet),"pending"(waiting),"granted","denied","error", or"unavailable"(the device cannot report locations). Coordinates are only present when it is"granted"; otherwiselatitudeandlongitudeareNoneandmessagemay explain why. - A granted
Locationalso carriesaccuracy(in meters) and, when the device knows them,altitude,heading, andspeed. - The
on_locateroute can receive the parts of the result as parameters by name:statusandmessage(strings), andlatitude,longitude,accuracy,altitude,heading,speed, andtimestamp(floats). Onlystatusis always sent. The coordinates arrive only when the status is"granted", andaltitude,heading, andspeedonly when the device knows them, so give each of those parameters a default value. A route that needs the whole result can instead take one parameter with the component's name, annotatedLocation, asreportdoes above. - The
on_error,on_denied, andon_timeoutroutes can receivestatusandmessage, both strings. - Each
on_parameter takes the route's name as a string or the route function itself. Route targets on the Button page compares the two. - Browsers only allow location requests on secure pages. This is
fine for deployed Drafter sites and local testing, but the
request can fail as
"unavailable"in unusual setups. - Privacy matters here: ask for location only when your app genuinely uses it, show the visitor what you do with it (as the example does), and never display one visitor's location to others without clearly saying so.
Related components
- Map: show the reported position on a map.