geolocate_control
StableA button that centres the map on the user's location, with optional tracking.
Set assigns
Press the location button in the map's top-right corner. The browser asks for permission first; the position it shares arrives in this LiveView as an event.
- @status
- waiting
- @tracking
- false
- @location
- —
- @accuracy
- —
<div id="geolocate-control-example">
<.example_frame error={@map_error} error_id="example-geolocate-map-error">
<.map
id="example-geolocate-map"
center={[0, 25]}
zoom={1.2}
style={basemap(@theme)}
events={["map:error"]}
class="h-full w-full"
/>
</.example_frame>
<%!-- With track_user_location the button keeps the map on the position as
it changes; pressing it again stops tracking. A new option replaces
the control in the same place, still tracking if it was. --%>
<.geolocate_control
id="example-geolocate"
map_id="example-geolocate-map"
track_user_location={@track_user_location}
show_user_location={@show_user_location}
show_accuracy_circle={@show_accuracy_circle}
fit_bounds_options={%{"maxZoom" => 14}}
events={[
"geolocate:location_found",
"geolocate:user_location_updated",
"geolocate:location_error",
"geolocate:tracking_started",
"geolocate:tracking_stopped"
]}
/>
<div class="space-y-4 border-t border-line px-4 py-4">
<.example_group id="example-geolocate-assigns" label={gettext("Set assigns")}>
<.form
for={@options}
id="example-geolocate-options"
phx-change="options"
class="flex flex-wrap gap-x-6 gap-y-2 py-1.5"
>
<.example_checkbox field={@options[:track_user_location]} label="track_user_location" />
<.example_checkbox field={@options[:show_user_location]} label="show_user_location" />
<.example_checkbox field={@options[:show_accuracy_circle]} label="show_accuracy_circle" />
</.form>
</.example_group>
<.example_note
:if={@status == "waiting"}
id="example-geolocate-hint"
icon="hero-cursor-arrow-rays-micro"
>
{gettext(
"Press the location button in the map's top-right corner. The browser asks for permission first; the position it shares arrives in this LiveView as an event."
)}
</.example_note>
<%!-- geolocate:location_error: the browser did not share a position. --%>
<div
:if={@error}
id="example-geolocate-error"
role="status"
class="flex items-start gap-2.5 rounded-ui border border-danger/40 bg-danger-soft px-3 py-2.5 text-ui"
>
<.icon name="hero-map-pin-mini" class="mt-px size-5 shrink-0 text-danger" />
<p>
<span class="font-bold">{gettext("Your location is not available.")}</span>
{explain(@error)}
<span class="mt-0.5 block font-mono text-label text-ink-muted">
{@error.code}: {@error.message}
</span>
</p>
</div>
<.example_note
:if={@status == "found"}
id="example-geolocate-found"
icon="hero-check-circle-micro"
>
{gettext("The browser shared its position.")}
<span :if={@track_user_location}>
{gettext("Press the button again to stop tracking it.")}
</span>
</.example_note>
</div>
<%!-- The control's events keep these current. --%>
<.example_readout values={[
status: @status,
tracking: @tracking,
location: location(@location),
accuracy: accuracy(@accuracy)
]} />
</div>
Generated from the component's definition in the library, so it always matches the version this demo runs.
| Attribute | Type | Default | Description |
|---|---|---|---|
| id required | string | — | Unique identifier for the control |
| map_id required | string | — | ID of the map to attach to |
| position | string |
"top-right"
|
Position of the control. One of: ["top-left", "top-right", "bottom-left", "bottom-right"]. Defaults to "top-right". A new value moves the control to that corner, still tracking if it was
|
| track_user_location | boolean |
false
|
If true, the control will actively track user's location. Defaults to false. MapLibre reads it only when it creates the control, so a new value replaces the control with a new one, in the same place. A control that was following the user goes on following: its replacement starts tracking as soon as MapLibre has set it up, with no event for the swap; one that stops tracking because track_user_location became false pushes geolocate:tracking_stopped
|
| show_accuracy_circle | boolean |
true
|
Show a circle representing location accuracy. Defaults to true. A new value replaces the control, as track_user_location does
|
| show_user_heading | boolean |
true
|
Meant to show the user's heading. Defaults to true. MapLibre GL JS 5 and 6 have no heading display, so it has no effect. (It used to hide the location dot when false; use show_user_location for that.)
|
| show_user_location | boolean |
true
|
Show the user's location as a dot on the map, as long as the control has one. Defaults to true. A new value replaces the control, as track_user_location does
|
| fit_bounds_options | map |
nil
|
Options for fitting map bounds when location is found. Map with keys like maxZoom, padding. Without it (or with %{}), MapLibre's own, %{maxZoom: 15}. Given, it replaces them, as in MapLibre: %{padding: 50} has no maximum zoom. A new value applies to the next location found
|
| position_options | map |
nil
|
Options for the browser's position requests (the Geolocation API's PositionOptions): enableHighAccuracy, timeout (milliseconds) and maximumAge (milliseconds). Without it (or with %{}), %{enableHighAccuracy: true, timeout: 6000, maximumAge: 0}: the most accurate position the device has, and MapLibre's timeout and maximum age. Each key given replaces its default, and the others keep theirs, so %{enableHighAccuracy: false} asks for a position of normal accuracy and keeps the 6 second timeout. A new value applies to the next position request: a control already tracking the user keeps the options it started with until it starts again
|
| events | any |
[]
|
a list of event names, or :all
|