Form element "Maps2PoiPosition" for the form framework 

New in version 12.3.0

maps2 provides the form element Maps2PoiPosition for forms based on the TYPO3 form framework (EXT:form).

The form element Maps2PoiPosition shows a map with a draggable marker in a frontend form. Frontend users can correct a position, for example the position, which was found by geocoding the address they have entered on a previous page of the form. Dragging the marker or clicking into the map moves the marker and updates the position.

This chapter describes, how integrators add the element to a form definition and configure it. The chapter Integrate the form element "Maps2PoiPosition" in code describes the value of the element and how an extension geocodes an address and stores the position.

When to use the form element "Maps2PoiPosition" 

Use the element, if frontend users create or edit records with an address and the position on the map matters, for example companies, locations or events. A geocoded position is not always exact: new buildings, large premises or ambiguous addresses result in a marker at the wrong place. With the element the user moves the marker to the correct place before the form is submitted.

The element does not geocode an address and does not store anything. Your extension sets the initial position and stores the submitted position. See Integrate the form element "Maps2PoiPosition" step by step.

Requirements for the form element "Maps2PoiPosition" 

  • The system extension EXT:form (typo3/cms-form) is installed.
  • The site set Maps2 - Default Set (jweiland/maps2-default) is included in the site. It is also included as dependency of the site sets Maps2 - Google Maps (jweiland/maps2-googlemaps) and Maps2 - OpenStreetMap (jweiland/maps2-openstreetmap).

The site set registers the form setup of maps2:

EXT:maps2/Configuration/Sets/Maps2/setup.typoscript
plugin.tx_form.settings.yamlConfigurations {
    1763380200 = EXT:maps2/Configuration/Form/FormSetup.yaml
}
Copied!

There is no static TypoScript template, which registers the form setup. Use the site sets.

If your extension ships a site set with a form, which uses the element, add jweiland/maps2-default to the dependencies of your site set:

EXT:my_extension/Configuration/Sets/MyExtension/config.yaml
name: my-vendor/my-extension
label: 'My extension'
dependencies:
  - typo3/form
  - jweiland/maps2-default
Copied!

Add the form element "Maps2PoiPosition" to a form definition 

Add an element of type Maps2PoiPosition to the form definition (YAML file). Add the validator NotEmpty, if the user has to place the marker:

EXT:my_extension/Resources/Private/Forms/Company.form.yaml
renderables:
  - type: Page
    identifier: page-map
    label: 'Position on the map'
    renderables:
      - type: Maps2PoiPosition
        identifier: position
        label: 'Position on the map'
        properties:
          markerTitle: 'Your company'
          zoom: 15
          height: 400
          elementDescription: 'Move the marker, if the position is not correct.'
        validators:
          - identifier: NotEmpty
Copied!

The element works without an initial position, too. Then the marker is shown at the default position of the extension settings (defaultLatitude and defaultLongitude) and the user has to place it. As long as the marker was not placed, the element has no value and NotEmpty fails.

maps2 adds its own validator Maps2PoiPosition to each element automatically. See Validation of the form element "Maps2PoiPosition".

Properties of the form element "Maps2PoiPosition" 

Set these properties below properties of the element. Common properties of form elements like elementDescription work as usual. maps2 ignores properties it does not know, so your extension can add properties of its own.

Name Type Default
string (empty)
int 15
int|string 400
string (empty)
string https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png
string &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors
mapProvider
Type
string
Default
(empty)

The map provider of the map: gm (Google Maps) or osm (OpenStreetMap).

The property is respected only, if the extension setting mapProvider is both. Else the map provider of the extension settings is used. An empty or invalid value uses the extension setting defaultMapProvider. See Extension Settings.

zoom
Type
int
Default
15

The zoom level of the map, from 0 to 20. Other values use the default 15. The TypoScript setting plugin.tx_maps2.settings.zoom is not used for this element.

height
Type
int|string
Default
400

The height of the map. Use a number (pixels) or a number with one of the units px, em, rem, vh or %, for example 300, 25rem or 50vh. Invalid values use the default 400.

The width of the map is always 100% of the surrounding element.

markerTitle
Type
string
Default
(empty)

The title of the marker, which is shown as tooltip on mouse over.

defaultMapTile
Type
string
Default
https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png

The tile server of OpenStreetMap, which is used, if the TypoScript setting plugin.tx_maps2.settings.mapTile is empty, for example because the site set "Maps2 - OpenStreetMap" is not included.

defaultMapTileAttribution
Type
string
Default
&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors

The attribution of defaultMapTile.

The default values of all properties are defined in the form setup of maps2 (EXT:maps2/Configuration/Form/FormSetup.yaml, below prototypes.standard.formElementsDefinition.Maps2PoiPosition.properties). Change them in the form setup of your site package for all elements or in the form definition for a single element.

Map settings of the form element "Maps2PoiPosition" 

The element uses the following TypoScript settings of plugin.tx_maps2.settings. mapTile, mapTileAttribution and googleMapsJavaScriptApiKey are set by the site settings of the site sets of maps2:

  • mapTile and mapTileAttribution: the tile server of OpenStreetMap and its attribution. Without a tile server, the properties defaultMapTile and defaultMapTileAttribution of the element are used.
  • googleMapsJavaScriptApiKey: the API key for Google Maps. Without this setting, the API key of the extension settings is used.
  • mapTypeId, zoomControl, mapTypeControl, scaleControl, streetViewControl, fullScreenControl, activateScrollWheel and styles: the same settings as for the maps of the maps2 plugins.

See TypoScript.

JavaScript and style sheets of the map 

The partial Maps2PoiPosition/LoadAssets of the form element loads the JavaScript and the style sheets of the map provider with the ViewHelpers f:asset.script and f:asset.css. It is rendered by the partial Maps2PoiPosition, if requests to the map provider are allowed, and is used for both template variants of EXT:form.

The partial gets these arguments:

mapProvider
The map provider of the element: gm or osm.
googleMapsJavaScriptApiKey
The TypoScript setting plugin.tx_maps2.settings.googleMapsJavaScriptApiKey or, if empty, the extension setting googleMapsJavaScriptApiKey.

The asset identifiers are the same as in the partial LoadAssets of the maps2 plugins (maps2-google-maps-api, maps2-google-maps, maps2-osm, maps2-osm-leaflet). So the assets are loaded only once, if a maps2 plugin is on the same page.

To load the assets from other locations, for example a Leaflet version of your own or a CDN, copy the partial to your site package:

Copy of the partial
EXT:my_sitepackage/Resources/Private/Form/Partials/Maps2PoiPosition/LoadAssets.html
Copied!

Register the partial root path with a key higher than 16 in the form setup of your site package:

EXT:my_sitepackage/Configuration/Form/FormSetup.yaml
prototypes:
  standard:
    formElementsDefinition:
      Form:
        renderingOptions:
          partialRootPaths:
            100: 'EXT:my_sitepackage/Resources/Private/Form/Partials/'
Copied!

Or register it for a single form in its form definition:

EXT:my_sitepackage/Resources/Private/Forms/company.form.yaml
renderingOptions:
  partialRootPaths:
    100: 'EXT:my_sitepackage/Resources/Private/Form/Partials/'
Copied!

The same way you can override the partials Maps2PoiPosition, Maps2PoiPosition/Overlay and Maps2PoiPosition/SummaryValue.

Summary page and emails 

maps2 overrides the partial SummaryPage of EXT:form and shows the position as text, for example:

Latitude: 48.663012, Longitude: 9.218945
Copied!

The override is registered in the form setup of maps2 with the partialRootPaths key 15 and, for the template variant version2, with the key 16.

The email templates of the email finishers of EXT:form are not overridden. They render the value like the value of other elements with multiple values: as list with the latitude and the longitude without labels.

Styling of the form element "Maps2PoiPosition" 

The map and the consent information are wrapped in a div with the CSS class maps2-poi-position. If the validation fails, the error class of the form (fieldProperties.errorClassAttribute) is added to this div. With the template variant version2, the surrounding container has the classes form-element form-element-maps2-poi-position mb-3.

Limitations of the form element "Maps2PoiPosition" 

The element is not available in the backend form editor
The form editor does not offer the element. Add the element to form definitions, which your extension or site package ships as YAML files.
An own SummaryPage partial with a higher key wins

If your site package overrides SummaryPage.html with a higher partialRootPaths key than maps2, the position is shown as list again. Add a condition for the element type to your partial:

EXT:my_sitepackage/Resources/Private/Form/Partials/SummaryPage.html
<f:if condition="{0: formValue.element.type} == {0: 'Maps2PoiPosition'}">
    <f:then>
        <f:render partial="Maps2PoiPosition/SummaryValue"
                  arguments="{position: formValue.value}"/>
    </f:then>
    <f:else>
        <!-- Rendering of all other values -->
    </f:else>
</f:if>
Copied!

The partial Maps2PoiPosition/SummaryValue is found as long as the partial root path of maps2 (key 15) is registered.

No geocoding and no storage
The element shows and returns a position only. Geocoding an address and storing the position in a record are the job of your extension. See Integrate the form element "Maps2PoiPosition" step by step.