beetlebug▾
/

Module ztcl::location

ztcl::location gives the position of a phone: one position with get, or a position at each move with watch.

ztcl::location get -command {apply {{d} {
    if {[dict exists $d error]} { puts "no position: [dict get $d error]" } else {
        puts "[dict get $d latitude], [dict get $d longitude] ±[dict get $d accuracy] m"
    }
    set ::done 1
}}}
vwait ::done
42.3314, -83.0458 ±5.0 m

This example runs with ztcl --allow location,event --fake-device: the fake host of the ztcl program is in Detroit, and gives its permission at once. On a phone, the same script asks the user, then gets the position of the phone.

How a reading arrives

ztcl::location get -command SCRIPT or ztcl::location watch -command SCRIPT gives a handle such as location1 at once, with no position: the first call also shows the dialog of the permission of the OS. The location service of the phone then sends each position, from a thread of the OS, to the event loop of the interpreter. For each one the event loop runs SCRIPT with one more argument, the position as a dict such as latitude 42.3314 longitude -83.0458 accuracy 5.0 time 1767225600000, or a failure such as error denied, so SCRIPT is a command prefix that takes one argument, such as apply {{d} {...}} or the name of a proc.

So no position arrives while the event loop does not run: a script needs vwait, update, or the event loop of Tk. get runs SCRIPT one time and ends, with a position or a failure. watch runs it at each move until a failure or ztcl::location cancel HANDLE.

The readings

Key Unit Meaning
latitude, longitude degrees The position, WGS 84: north and east are positive.
accuracy m The radius of the circle around the position that holds the true position, with a confidence of about 68 %.
altitude m The height. iOS gives it above sea level, and Android above the WGS 84 ellipsoid, which differ by up to about 100 m. The key exists only when the device has a height.
vaccuracy m How far altitude can be off. Only with altitude.
speed m/s The speed over the ground. Only when the device has it, mostly from the GPS.
course degrees The direction of the move from true north, clockwise, 0 to 360. Only when the device has it.
time ms The time of the position, in milliseconds since 1970-01-01 UTC, as clock milliseconds. A position can be older than the call.
error Only in a failure, in place of the other keys: denied, unavailable, or timeout.

request gives one dict with the key permission: granted or denied. status gives permission (granted, denied, or undetermined) and enabled (1 when the location service of the device is on) at once, with no dialog.

When there is no position

Case What the script gets
The capability location is off The command does not exist: invalid command name "ztcl::location", with the errorCode TCL LOOKUP COMMAND ztcl::location.
The host has no service (the ztcl program on a desktop) Each subcommand gives the error the location is not available on this platform, with the errorCode ZTCL LOCATION UNAVAILABLE.
The user says no, or said no before SCRIPT gets error denied, and status gives permission denied. get and watch show the dialog only while the permission is undetermined.
The location service of the device is off SCRIPT gets error unavailable, and status gives enabled 0.
No position before -timeout MS SCRIPT of get gets error timeout.

A capability lets a script ask; the OS asks the user. An app of ztk for iOS needs a text for the dialog in its Info.plist (NSLocationWhenInUseUsageDescription). On Android, ztk bundle writes the permissions of the fine and the coarse location for the capability location.

The rate and the battery

-accuracy best reads the GPS, balanced a position of about 100 m, and low about 3 km; the GPS uses the most battery. -distance M asks for a new position of a watch only after a move of M meters, and -interval MS for at most one each MS milliseconds. A watch that the app does not need, for example while its window is hidden, is a cost: cancel it. In the background, iOS stops a watch, and Android gives a few positions an hour.

Platforms

Platform The source of the position
iOS (ztk) CoreLocation
Android (ztk) The LocationManager: the GPS, the fused provider, or the network, by -accuracy
macOS (ztk) CoreLocation, mostly from Wi-Fi
The ztcl program, Linux, Windows none: each subcommand gives ZTCL LOCATION UNAVAILABLE
The web none

The ztcl program has no location; --fake-device gives it the fake host of the examples (src/device_fake.zig).

Commands

Command Description
ztcl::location status permission (granted, denied, or undetermined) and enabled (1 when the location service of the device is on). It shows no dialog.
ztcl::location request -command SCRIPT Shows the dialog of the OS when the permission is undetermined, and runs SCRIPT one time with {permission granted} or {permission denied}.
ztcl::location get ?-accuracy A? ?-timeout MS? -command SCRIPT Runs SCRIPT one time with a position, or with {error denied}, {error unavailable}, or {error timeout}.
ztcl::location watch ?-accuracy A? ?-distance M? ?-interval MS? -command SCRIPT Runs SCRIPT at each new position. An error ends it.
ztcl::location cancel HANDLE Stops a handle. A handle that ended is no error.

request, get, and watch give a handle such as location1. request picks the moment of the dialog; get and watch also ask when the permission is undetermined, so a short script works.

The command is not a package: it exists when the embedder turns on the capability location, and it is absent otherwise. A child interp has it only when its parent has it. While a handle is active, the event loop wakes at least every 20 milliseconds, since the host gives the positions from its own thread. The interpreter stops each handle when it ends.

Try it

Ask for the permission, then get one position:

puts [ztcl::location status]
ztcl::location request -command {apply {{d} {
    puts "permission: [dict get $d permission]"
    ztcl::location get -accuracy best -command {apply {{d} {
        set lat [dict get $d latitude]
        set lon [dict get $d longitude]
        puts "at $lat $lon, ±[dict get $d accuracy] m"
        set ::done 1
    }}}
}}}
vwait ::done
permission granted enabled 1
permission: granted
at 42.3314 -83.0458, ±5.0 m

Follow the position as the phone moves. An error, such as {error denied}, ends a watch:

set n 0
set h [ztcl::location watch -distance 10 -command {apply {{d} {
    if {[dict exists $d error]} {
        puts "error: [dict get $d error]"
        set ::done 1
        return
    }
    puts [format "%.4f, %.1f m/s" [dict get $d latitude] [dict get $d speed]]
    if {[incr ::n] == 3} { set ::done 1 }
}}}]
vwait ::done
ztcl::location cancel $h
42.3314, 0.0 m/s
42.3315, 11.1 m/s
42.3316, 11.1 m/s

A WebAssembly module gets the same positions through the import family location (the device APIs for WebAssembly).

The host

An embedder fills location_status and locate of Interp.device_host, the table of ztcl::device. locate gets a Sink, a handle, and what to start: request, get, or watch, with the accuracy, the distance, and the interval. The host posts a location, a permission, or a failure with Sink.post, from any thread, and stop ends the handle. ztcl ends a handle of get at its first position, a handle of request at its answer, and each handle at a failure or at the deadline of -timeout.