Search by

cowegis / cowegis-contao-geocode-widget-bundle

netzmacht

Cowegis - Content Web GIS Geocode Widget For Contao

Package info

github.com/cowegis/cowegis-contao-geocode-widget-bundle

Type:contao-bundle

pkg:composer/cowegis/cowegis-contao-geocode-widget-bundle

Fund package maintenance!

dmolineus

Statistics

Installs: 4 345

Dependents: 1

Suggesters: 0

Stars: 2

Open Issues: 0

1.3.1 2026-12-04 11:00 UTC

README

Build Status Version License Downloads

This extension provides backend widgets for Contao to pick coordinates and an optional radius from a map. It uses the leaflet framework.

Requirements

  • PHP ^8.3
  • Contao ^5.3 || ^6.0
  • Symfony ^6.4 || ^7.4 || ^8.0

Install

1. Install using composer

composer require cowegis/cowegis-contao-geocode-widget-bundle

If you use the Contao Manager, the bundle is registered automatically.

2. Use the widget

Coordinates only

The widget is used to add a map icon which opens a map in a pop-up. You can enter an address in the search field and press Enter to resolve it. Either a list of suggested addresses is displayed or a marker is placed on the map. The marker can still be positioned using the mouse. Click on the ‘Apply’ button to apply the geocoordinates.

$GLOBALS['TL_DCA']['tl_example']['fields']['coordinates'] = [
    'label'     => ['Coordinates', 'Enter the coordinates - comma separated as \'latitude,longitude\'.'],
    'inputType' => 'cowegis_geocode',
    'eval'      => [
        'tl_class' => 'w50',
    ],
    'sql' => 'varchar(255) NOT NULL default \'\''
];

However, if you already have input fields for the address, such as postcode, town and street, you don't want to have to enter this information again in the search field in the pop-up.

With the appropriate information in the DCA, you can automatically transfer this information to the search field – here is an example for fields street, zip and city and country fixed as "Deutschland":

$GLOBALS['TL_DCA']['tl_example']['fields']['coordinates'] = [
    'label'     => ['Coordinates', 'Enter the coordinates - comma separated as \'latitude,longitude\'.'],
    'inputType' => 'cowegis_geocode',
    'eval'      => [
        'query_pattern'    => '#ctrl_street# #ctrl_zip# #ctrl_city#, Deutschland',
        'urlTemplate'      => 'https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png',
        'attribution'      => 'Tiles style by <a href="https://www.hotosm.org/" target="_blank">Humanitarian OpenStreetMap Team</a> hosted by <a href="https://openstreetmap.fr/" target="_blank">OpenStreetMap France</a>',
        'tl_class'         => 'w50',
    ],
    'sql' => 'varchar(255) NOT NULL default \'\''
];

The information is as follows:

  • query_pattern: Pattern as it appears in the search input in the pop-up - the field IDs with #<id>#
  • urlTemplate: Tile URL template of the map layer. Overrides the bundle configuration (see below).
  • attribution: Attribution of the map layer

Map options

The initial map view can be configured with the following eval options:

  • mapCenter: Initial center of the map as [latitude, longitude]. Default [0, 0].
  • mapDefaultZoom: Initial zoom level. Default 2.
  • mapMinZoom: Minimum zoom level. Default 2.
  • mapMaxZoom: Maximum zoom level. Default 18.

If coordinates are already set, the map is centered on them.

Default tile layer

The tile URL template can be configured globally in the bundle configuration, e.g. in config/config.yaml. If neither the bundle configuration nor eval.urlTemplate is set, https://{s}.tile.osm.org/{z}/{x}/{y}.png is used.

cowegis_contao_geocode_widget:
    url_template: 'https://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png'

Input and storage format

By default, the widget shows one input and stores the coordinates comma separated as latitude,longitude in a text column. Both the input and the storage format can be configured independently.

use Cowegis\Bundle\ContaoGeocodeWidget\Coordinates\GeoJsonStorage;
use Cowegis\Bundle\ContaoGeocodeWidget\Widget\CoordinateInput;

$GLOBALS['TL_DCA']['tl_example']['fields']['position'] = [
    'label'     => ['Position', 'Enter the latitude and the longitude.'],
    'inputType' => 'cowegis_geocode',
    'eval'      => [
        'coordinateInput'   => CoordinateInput::Split,
        'coordinateStorage' => GeoJsonStorage::class,
        'tl_class'          => 'w50',
    ],
    'sql' => 'json NULL',
];

eval.coordinateInput defines how the coordinates are entered, using the Cowegis\Bundle\ContaoGeocodeWidget\Widget\CoordinateInput enum:

  • CoordinateInput::Combined (default): One input containing latitude,longitude.
  • CoordinateInput::Split: Two inputs, one for the latitude and one for the longitude. Both have to be filled.

eval.coordinateStorage defines the class which converts the coordinates from and to the stored value:

  • Cowegis\Bundle\ContaoGeocodeWidget\Coordinates\TextStorage (default): Stores latitude,longitude as string.
  • Cowegis\Bundle\ContaoGeocodeWidget\Coordinates\GeoJsonStorage: Stores a GeoJSON point, e.g. {"type":"Point","coordinates":[13.404954,52.520008]}. Note that GeoJSON uses the order longitude, latitude. Define the column as string, e.g. 'sql' => 'json NULL'. Contao binds the value using sql.type, so the Doctrine JSON type of 'sql' => ['type' => 'json'] would encode the already encoded GeoJSON again.

To support another storage format, implement Cowegis\Bundle\ContaoGeocodeWidget\Coordinates\CoordinateStorage and configure the class name. The class must not require any constructor arguments.

The widget validates that the latitude is between -90 and 90 and the longitude between -180 and 180. The multiple option is only supported with the combined input and the text storage.

Coordinates and radius

To pick the radius in meters as well, you have to configure the eval.radius option for the related radius field. The radius field should be a simple text input. The default, minval and maxval flags are passed to the geocode widget so that only radius in that boundary can be chosen.

The radius field is optional. If it is not rendered, e.g. because it is not selected when editing multiple records, the map only picks the position.

$GLOBALS['TL_DCA']['tl_page']['fields']['coordinates'] = [
    'label'     => ['Coordinates', 'Enter the coordinates - comma separated as \'latitude,longitude\'.'],
    'inputType' => 'cowegis_geocode',
    'eval'      => [
        'tl_class' => 'w50',
        'radius'   => 'radius'
    ],
    'sql' => 'varchar(255) NOT NULL default \'\''
];

$GLOBALS['TL_DCA']['tl_page']['fields']['radius'] = [
    'label'     => ['Radius', 'Specification of the radius in metres.'],
    'inputType' => 'cowegis_radius', // Optional, you can use a text widget as well
    'eval'      => [
        'default'  => 500,
        'minval'   => 100,
        'maxval'   => 5000,
        'steps'    => 100, // Round value to the closest 100 m.
        'tl_class' => 'w50',
    ],
    'sql' => 'varchar(255) NOT NULL default \'\''
];

If you want to add a wizard icon to the radius field as well, you only have to reference the coordinate field.

$GLOBALS['TL_DCA']['tl_page']['fields']['radius'] = [
    'label'     => ['Radius', 'Specification of the radius in metres.'],
    'inputType' => 'cowegis_radius',
    'eval'      => [
        'rgxp'        => 'natural',
        'default'     => 500,
        'minval'      => 100,
        'maxval'      => 5000,
        'tl_class'    => 'w50 wizard',
        'coordinates' => 'coordinates'
    ],
    'sql' => 'varchar(255) NOT NULL default \'\''
];