Skip to content

geolocate_control

Stable

A button that centres the map on the user's location, with optional tracking.

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