map_draw() form()
Draw and edit points, lines and polygons on a map (maps plugin, Terra Draw): one hidden input posts the shapes as a GeoJSON FeatureCollection.
form()->map_draw($name = '', $desc = '', $extra = [], $replace = [])Options
| Option | Meaning |
|---|---|
$name | Default shapes: the hidden input with the GeoJSON. |
types | Drawing tools: point, linestring, polygon, rectangle (default the maps.DRAW_TYPES setting: the first three). |
max_features | Most shapes (default and upper bound maps.DRAW_MAX_FEATURES, 500). |
map | Map spec keys (_class('maps')->render()): tiles, zoom, center, height, consent, ...; without center the map fits the shapes. |
disable_edit_mode, readonly | Show the shapes only (no Terra Draw; the input is not posted). |
Example
Code
$value = '{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[30.2,50.3],[30.8,50.3],[30.8,50.6],[30.2,50.6],[30.2,50.3]]]},"properties":{"name":"Kyiv area"}}]}';
return form(['area' => $value])->map_draw('area', 'Area', ['types' => ['point', 'polygon', 'rectangle'], 'max_features' => 5, 'map' => ['tiles' => 'pmtiles', 'center' => [50.45, 30.52], 'zoom' => 5, 'height' => 280]]);Notes
- Needs the maps plugin, separately distributed since it left the framework (admin docs misc/plugins_moved#plugin-maps): without it the call keeps working and draws an empty box of the map's height (on admin pages it names the install command) with one E_USER_NOTICE per request; form fields keep their inputs and values, a table column its coordinates.
- Read the value back with
_class('maps')->draw_features($_POST['area'], ['types' => [...], 'max' => 5]): a list of['geometry', 'properties'], every geometry validated; it throwsInvalidArgumentExceptionwith the reason.draw_value($features)gives the JSON back for the field. - The toolbar: one icon button per tool, Select (move, reshape, name in the field next to it), Delete the selected shape. A line or polygon ends on a double click or on its first point.
- The properties of the shapes the field was given stay with them; new shapes get none, or a
name. - A stored shape Terra Draw cannot edit (a polygon with holes, a multi-part shape) makes the map read-only, and the input posts the value it came with.
- With tiles that are not self-hosted the map starts as a click-to-load placeholder. The example uses the bundled world basemap (
tiles=>pmtiles).
See also: form/map_select, html/map