HoloMap.Control.Geolocate (HoloMap v0.3.0)

Copy Markdown View Source

Locate the user, and optionally follow them.

<HoloMap.Control.Geolocate track_user_location />

Props

PropTypeNotes
positionstringCorner to place the control in. Default "top-right"
track_user_locationbooleanKeep following after the first fix
show_user_locationbooleanDraw a dot at the users position. Default true
show_accuracy_circlebooleanDraw the accuracy radius. Default true
position_optionsmapPassed to the browser Geolocation API
fit_bounds_optionsmapCamera options used when zooming to the user
zoom_to_user_accuracybooleanZoom 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

PropMapLibre eventWhen
on_geolocategeolocateThe browser answered with the user's position
on_errorerrorThe browser answered with an error
on_out_of_max_boundsoutofmaxboundsThe position is outside the map's max_bounds. on_geolocate does not fire for it
on_track_starttrackuserlocationstartWith track_user_location, the control starts following the user
on_track_endtrackuserlocationendIt stops following them: the button was clicked again, or the camera was moved away
on_user_location_focususerlocationfocusA click brought the camera back to the user, after a move had made it stop following
on_user_location_lost_focususerlocationlostfocusThe 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:

KeyTypeNotes
coords.longitude / coords.latitudenumberDegrees
coords.accuracynumberMetres, at a 95% confidence level
coords.altitude / coords.altitude_accuracynumber or nilMetres, when the device gives them
coords.headingnumber or nilDegrees clockwise from true north, when the device is moving and can tell
coords.speednumber or nilMetres per second, when the device gives it
timestampnumberWhen the position was taken
def action(:user_located, %{coords: %{longitude: lng, latitude: lat, accuracy: acc}}, component) do
  put_state(component, :user, {lng, lat, acc})
end

on_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

__is_hologram_component__()

@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

__props__()

@spec __props__() :: [{atom(), atom(), keyword()}]

Returns the list of property definitions for the compiled component.