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>
The events this component sends to the LiveView, as the library documents them. Each arrives in handle_event/3. Use the preview; the last payload of each event the example receives appears here. A component sends only the events its events attribute names, so the example receives those its LiveView handles.
-
geolocate:location_found
Fired when user's location is found, with
coords(see the module documentation); for every position, also while trackingNot received yet.
-
geolocate:location_error
Fired when there's an error getting location, with
codeandmessageNot received yet.
-
geolocate:tracking_started
Fired when location tracking starts: the camera follows the user's location
Not received yet.
-
geolocate:tracking_stopped
Fired when location tracking stops, or the camera stops following the user's location because the user moved the map
Not received yet.
-
geolocate:user_location_updated
Fired for every position while the control tracks the user's location (
track_user_location), also while the camera no longer follows it, with the samecoordsasgeolocate:location_foundNot received yet.