Locate the user, and optionally follow them.
<HoloMap.Control.Geolocate track_user_location />Props
| Prop | Type | Notes |
|---|---|---|
position | string | Corner to place the control in. Default "top-right" |
track_user_location | boolean | Keep following after the first fix |
show_user_location | boolean | Draw a dot at the users position. Default true |
show_accuracy_circle | boolean | Draw the accuracy radius. Default true |
position_options | map | Passed to the browser Geolocation API |
fit_bounds_options | map | Camera options used when zooming to the user |
zoom_to_user_accuracy | boolean | Zoom to fit the position's accuracy when the camera moves to the user. Default true. false keeps the zoom and moves only the centre |
position_options takes enableHighAccuracy, timeout and maximumAge.
Geolocation requires a secure context and the users permission, and the
browser prompt only appears when they click the button, or when an action
calls HoloMap.API.trigger_geolocate/1. Nothing happens on page load, by
design.
Events
| Prop | MapLibre event | When |
|---|---|---|
on_geolocate | geolocate | The browser answered with the user's position |
on_error | error | The browser answered with an error |
on_out_of_max_bounds | outofmaxbounds | The position is outside the map's max_bounds. on_geolocate does not fire for it |
on_track_start | trackuserlocationstart | With track_user_location, the control starts following the user |
on_track_end | trackuserlocationend | It stops following them: the button was clicked again, or the camera was moved away |
on_user_location_focus | userlocationfocus | A click brought the camera back to the user, after a move had made it stop following |
on_user_location_lost_focus | userlocationlostfocus | The camera moved away while it followed the user |
on_geolocate and on_out_of_max_bounds receive the position, coords
and timestamp, milliseconds since the Unix epoch:
| Key | Type | Notes |
|---|---|---|
coords.longitude / coords.latitude | number | Degrees |
coords.accuracy | number | Metres, at a 95% confidence level |
coords.altitude / coords.altitude_accuracy | number or nil | Metres, when the device gives them |
coords.heading | number or nil | Degrees clockwise from true north, when the device is moving and can tell |
coords.speed | number or nil | Metres per second, when the device gives it |
timestamp | number | When the position was taken |
def action(:user_located, %{coords: %{longitude: lng, latitude: lat, accuracy: acc}}, component) do
put_state(component, :user, {lng, lat, acc})
endon_error receives the code and message of the browser's
GeolocationPositionError: 1 when the user or the browser denied
permission, 2 when the position is unavailable, 3 when it took longer
than position_options' timeout. After a 1, MapLibre disables the
button.
The other events carry no payload.
A change to a handler alone moves the listeners and keeps the control as it is, still following the user if it was. A change to any other prop rebuilds the control, as it does every control, and the handlers move to the new one, which starts with no position and does not follow the user.
HoloMap.API.trigger_geolocate/1 does what a click on the button does.
Summary
Functions
Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).
Returns the list of property definitions for the compiled component.
Functions
@spec __is_hologram_component__() :: boolean()
Returns true to indicate that the callee module is a component module (has "use Hologram.Component" directive).
Examples
iex> __is_hologram_component__()
true
Returns the list of property definitions for the compiled component.