Skip to content

geolocate_control

Stable

A 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
—
lib/maplibrex_demo_web/examples/geolocate_control_example.html.heex
<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>