Place a marker to continue
Use the interact plugin's placeMarker mode to let users drop a location pin on the map before continuing a journey. Two patterns work together in this example, depending on the screen size.
On mobile, the map opens fullscreen and the backAndContinue option adds Back and Continue buttons inside the map, with Continue enabled by continueEnabledWhen once a marker is placed. On tablet and desktop, the map is inline and a Continue button sits outside it. That button uses app:opened, app:closed and app:fullscreenchange to show and hide with the map, and interact:markerchange to enable once a marker is placed.
The map requires JavaScript to be enabled.
import InteractiveMap from '@defra/interactive-map'import createInteractPlugin from '@defra/interactive-map/plugins/interact'
const interactPlugin = createInteractPlugin({ interactionModes: ['placeMarker']})
const map = new InteractiveMap('my-map', { behaviour: 'hybrid', mapStyle: { url: 'https://your-tile-url/style.json', logo: '/path/to/logo.svg', logoAltText: 'Your logo alt text', attribution: 'Your tile attribution' }, center: [-0.1276, 51.5074], zoom: 12, containerHeight: '516px', plugins: [interactPlugin], // Back and Continue buttons shown inside the map when fullscreen (mobile) backAndContinue: { backLabel: 'Back', continueLabel: 'Continue', continueEnabledWhen: ({ mapState }) => mapState.markers.items.some(item => item.id === 'location') }})
map.on('map:ready', () => { interactPlugin.enable()})
// Fired when the built-in Continue button is clicked (mobile/fullscreen)map.on('app:continue', ({ mapState }) => { const marker = mapState.markers.items.find(item => item.id === 'location') // navigate to next step with marker.coords})
// --- External Continue button for inline (tablet/desktop) ---
const continueBtn = document.getElementById('continue-btn')const externalButtons = document.getElementById('external-buttons')
// Show/hide external buttons based on whether the map is inline or fullscreen.// app:opened fires on first load and when the map is shown after being hidden.// app:fullscreenchange fires when the viewport resizes while the map is already visible.const setInline = (isFullscreen) => { externalButtons.hidden = isFullscreen}
map.on('app:opened', ({ isFullscreen }) => { setInline(isFullscreen) })map.on('app:closed', () => { externalButtons.hidden = true })map.on('app:fullscreenchange', ({ isFullscreen }) => { setInline(isFullscreen) })
map.on('interact:markerchange', () => { continueBtn.disabled = false})
continueBtn.addEventListener('click', () => { // navigate to next step})<script defer src="/your-assets-path/interactive-map/index.js"></script><script defer src="/your-assets-path/interact-plugin/index.js"></script>
<div id="my-map"></div><div id="external-buttons" hidden> <button class="govuk-button" id="continue-btn" disabled>Continue</button></div>
<script>document.addEventListener('DOMContentLoaded', function () { const interactPlugin = defra.interactPlugin({ interactionModes: ['placeMarker'] })
const map = new defra.InteractiveMap('my-map', { behaviour: 'hybrid', mapStyle: { url: 'https://your-tile-url/style.json', logo: '/path/to/logo.svg', logoAltText: 'Your logo alt text', attribution: 'Your tile attribution' }, center: [-0.1276, 51.5074], zoom: 12, containerHeight: '516px', plugins: [interactPlugin], backAndContinue: { backLabel: 'Back', continueLabel: 'Continue', continueEnabledWhen: function (context) { return context.mapState.markers.items.some(function (item) { return item.id === 'location' }) } } })
map.on('map:ready', function () { interactPlugin.enable() })
map.on('app:continue', function (context) { const marker = context.mapState.markers.items.find(function (item) { return item.id === 'location' }) // navigate to next step with marker.coords })
const continueBtn = document.getElementById('continue-btn') const externalButtons = document.getElementById('external-buttons')
function setInline(isFullscreen) { externalButtons.hidden = isFullscreen }
map.on('app:opened', function (event) { setInline(event.isFullscreen) }) map.on('app:closed', function () { externalButtons.hidden = true }) map.on('app:fullscreenchange', function (event) { setInline(event.isFullscreen) })
map.on('interact:markerchange', function () { continueBtn.disabled = false })
continueBtn.addEventListener('click', function () { // navigate to next step })})</script>