deploy-map quick start
deploy-map shows a set of points on a map and in a table, with filters, colours, a legend and a details panel. This page starts with the smallest possible example and adds one thing at a time.
Every map below is live, and is created by exactly the code shown above it. See also the demo.
Setting up
Add the stylesheet and the script to your page. The script contains
everything the widget needs, and creates one global, DeployMap.
<link rel="stylesheet" href="deploy-map.css">
<script src="deploy-map.js"></script>
Then add an element for the widget, and give it a height. The widget fills the element.
<div id="map" style="height: 400px"></div>
Your first map
Call DeployMap.mount() with the element and your points.
The points are GeoJSON: a
FeatureCollection of Point features, each with
any properties you like. Note that GeoJSON coordinates are longitude
first, then latitude.
DeployMap.mount('#first-map', {
data: {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: { type: 'Point', coordinates: [4.35, 50.85] },
properties: { name: 'Brussels', country: 'Belgium' },
},
{
type: 'Feature',
geometry: { type: 'Point', coordinates: [4.9, 52.37] },
properties: { name: 'Amsterdam', country: 'Netherlands' },
},
{
type: 'Feature',
geometry: { type: 'Point', coordinates: [2.35, 48.86] },
properties: { name: 'Paris', country: 'France' },
},
],
},
})
That is all that is required. data is the only option
without a default: the map zooms to fit your points, and the
Table button shows the same points as a table, with one
column per property.
Loading data from a URL
Usually the points live in a file. Give its URL instead of the data itself. The rest of this page uses a file of radar deployments in western Europe.
DeployMap.mount('#url-map', {
data: 'registry.json',
})
Have a look at the table: this file has many properties, so the table has many columns. Properties that hold a list or an object are left out.
Choosing the table columns
List the properties you want with table.columns, in the
order you want them. A header is the property name made readable
(radar_id becomes "Radar id"); to choose it yourself, give
an object with a label.
DeployMap.mount('#table-map', {
data: 'registry.json',
table: {
columns: [
{ property: 'radar_id', label: 'Code' },
'type',
'country',
'location',
'start',
'end',
],
},
})
Filters
Name a property in filters to let people filter on it. The
widget finds the values that occur in your data and offers them as
checkboxes. Untick "Germany" below and the German radars leave the map
and the table.
DeployMap.mount('#filter-map', {
data: 'registry.json',
filters: ['country'],
})
You can have several filters, and give each a label.
Within one filter the ticked values are alternatives: "Belgium" and
"France" shows both. Different filters narrow each other down:
"weather" and "Netherlands" shows only Dutch weather radars.
DeployMap.mount('#filters-map', {
data: 'registry.json',
filters: [{ property: 'type', label: 'Radar type' }, 'country'],
})
Hiding or dimming
By default, points that do not match the filters are hidden. With
matchBehaviour: 'dim' they stay on the map, faded, so the
matching ones are seen in context. The table lists matching points only
in both cases.
DeployMap.mount('#dim-map', {
data: 'registry.json',
filters: ['country'],
matchBehaviour: 'dim',
})
Colours and legend
style.colourBy names the property that decides the colour of
a marker. Each value gets a colour from a built-in palette that stays
readable for colour-blind people, and a legend appears on the map.
DeployMap.mount('#colour-map', {
data: 'registry.json',
style: { colourBy: 'type' },
})
To choose colours yourself, add a palette. You only need to
list the values you care about; the others keep a built-in colour.
DeployMap.mount('#palette-map', {
data: 'registry.json',
style: {
colourBy: 'type',
palette: { weather: '#7b3294', birdscan: '#008837' },
},
})
Selecting a point
Click a marker on any map above, or a row in its table: a panel opens with everything known about that point. That works without any option. The panel closes with its button or the Escape key. In the table, the first cell of each row is a button, so points can be opened from the keyboard too.
To choose what the panel shows, give it a title and sections.
titleProperty names the property used as the title, and
each section lists properties the same way as table columns: a name,
or a name with a label.
DeployMap.mount('#panel-map', {
data: 'registry.json',
panel: {
titleProperty: 'location',
sections: [
{
title: 'Radar',
properties: [
{ property: 'radar_id', label: 'Code' },
'type',
{ property: 'max_range', label: 'Max range (km)' },
],
},
{ title: 'Deployment', properties: ['country', 'start', 'end'] },
],
},
})
On a narrow screen, or in a narrow column, the panel covers the map instead of sitting next to it.
Friendlier values
Data files often hold short codes: this one says weather
and birdscan. valueLabels gives the text to
show instead, per property. It is used everywhere a value appears: the
filter, the legend, the table and the panel. Your data, and what the
callbacks receive, stay as they are.
DeployMap.mount('#labels-map', {
data: 'registry.json',
filters: ['type'],
style: { colourBy: 'type' },
valueLabels: {
type: { weather: 'Weather radar', birdscan: 'BirdScan radar' },
},
})
Your own panel content
When a list of properties is not enough, panel.content
takes a function. It receives the selected point as a GeoJSON feature
and returns the element to show. Build it with
textContent rather than innerHTML, so that
text from the data can never be read as HTML.
DeployMap.mount('#custom-map', {
data: 'registry.json',
panel: {
titleProperty: 'location',
content: (feature) => {
const p = document.createElement('p')
const count = feature.properties.datasets.length
p.textContent = feature.properties.radar_id + ' has ' + count + ' dataset(s).'
return p
},
},
})
Links to a point
With permalink: true the address of your page follows the
widget: the selected point, the table view and any narrowed filter are
written after the #, and restored when someone opens that
address. A link can then lead straight to one point with its panel
open. Try it on the demo.
DeployMap.mount('#map', {
data: 'registry.json',
permalink: true,
idProperty: 'deployment_id',
})
idProperty names the property that identifies a point in
the link. Without it the widget uses the GeoJSON id, then
a property called id, then the point's position in the
file, which breaks old links when the file is reordered.
This is off by default because the part after the #
belongs to your page, and there is only one per page: turn it on for
one widget only. That is also why this example is not live here.
Reacting to the user
onFilterChange and onViewChange are called
when someone changes a filter or switches between map and table, and
onSelect when they select a point or close its panel.
mount() also returns a handle: its update()
changes options such as matchBehaviour, style
or filters on the running widget, without losing the
selection or the map position.
Change a filter or the view below, or select a point, and watch the line under the buttons. Then untick a country and use the two buttons.
const log = document.querySelector('#events-log')
const widget = DeployMap.mount('#events-map', {
data: 'registry.json',
filters: ['country'],
onFilterChange: (state) => {
log.textContent = 'filterChange: ' + JSON.stringify(state)
},
onViewChange: (view) => {
log.textContent = 'viewChange: ' + view
},
onSelect: (feature) => {
log.textContent = 'select: ' + (feature ? feature.properties.radar_id : 'nothing')
},
})
document.querySelector('#events-hide').addEventListener('click', () => {
widget.update({ matchBehaviour: 'hide' })
})
document.querySelector('#events-dim').addEventListener('click', () => {
widget.update({ matchBehaviour: 'dim' })
})
Nothing yet.
Another background map
The default background is muted, so the markers stand out. To use
another one, give tileLayer the URL template of the tiles
and the attribution their provider asks for. Check the provider's terms
of use first.
DeployMap.mount('#tiles-map', {
data: 'registry.json',
tileLayer: {
url: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
attribution: '© OpenStreetMap contributors',
maxZoom: 19,
},
})
The scroll wheel
You may have noticed that scrolling this page does not zoom the maps you pass over. The scroll wheel only zooms a map once you have clicked it, and stops doing so when the pointer leaves it. The + and - buttons always work.
For a map that fills the whole page there is nothing else to scroll, so
let the wheel zoom straight away with scrollZoom: 'always'.
'never' turns wheel zooming off.
DeployMap.mount('#scroll-map', {
data: 'registry.json',
scrollZoom: 'always',
})
Styling
The buttons, checkboxes and table are written with Bootstrap 5.3 class names. You do not need to do anything about that:
- If your page already uses Bootstrap 5.3, like this one, the widget uses it and follows your theme.
- Otherwise the widget brings its own copy of the parts it needs. Those styles only apply inside the widget, so the rest of your page does not change. See the demo on a page without Bootstrap.
The bootstrap option overrides this: 'bundled'
always uses the widget's own copy, 'host' never adds it.
Putting it together
All of the above in one widget.
DeployMap.mount('#full-map', {
data: 'registry.json',
table: {
columns: [
{ property: 'radar_id', label: 'Code' },
'type',
'country',
'location',
'start',
'end',
],
},
filters: [{ property: 'type', label: 'Radar type' }, 'country'],
matchBehaviour: 'dim',
style: { colourBy: 'type' },
valueLabels: {
type: { weather: 'Weather radar', birdscan: 'BirdScan radar' },
},
panel: {
titleProperty: 'location',
sections: [
{ title: 'Radar', properties: [{ property: 'radar_id', label: 'Code' }, 'type'] },
{ title: 'Deployment', properties: ['country', 'start', 'end'] },
],
},
})
Advanced: using it in a Vue app
Everything above uses a script tag. If your site is a Vue 3 app with a build step, you can use the widget as a component instead. Install the package next to Vue (3.5 or later), which the widget then shares with your app:
npm install @aloftdata/deploy-map vue
Import the DeployMap component and its stylesheet. The
options of DeployMap.mount() are the component's props,
with the same names and the same defaults. As before, the widget fills
its container, so give that a height.
<script setup>
import { DeployMap } from '@aloftdata/deploy-map'
import '@aloftdata/deploy-map/dist/deploy-map.css'
const filters = [{ property: 'type', label: 'Radar type' }, 'country']
const style = { colourBy: 'type' }
</script>
<template>
<div style="height: 400px">
<DeployMap data="/registry.json" :filters="filters" :style="style" />
</div>
</template>
Props are reactive, so there is no update(): change a prop
and the widget follows. The onSelect,
onFilterChange and onViewChange callbacks
become the events select, filter-change and
view-change.
<script setup>
import { ref } from 'vue'
import { DeployMap } from '@aloftdata/deploy-map'
import '@aloftdata/deploy-map/dist/deploy-map.css'
const matchBehaviour = ref('hide')
const selected = ref(null)
</script>
<template>
<label>
<input v-model="matchBehaviour" type="checkbox" true-value="dim" false-value="hide">
Dim the points that do not match
</label>
<p>Selected: {{ selected ? selected.properties.location : 'nothing' }}</p>
<div style="height: 400px">
<DeployMap
data="/registry.json"
:filters="['type', 'country']"
:match-behaviour="matchBehaviour"
@select="selected = $event"
/>
</div>
</template>
The panel slot replaces the content of the details panel
with your own template. It receives the selected point as
feature, and the panel keeps its title and its Close button.
This is the Vue counterpart of the
content function.
<DeployMap data="/registry.json" :panel="{ titleProperty: 'location' }">
<template #panel="{ feature }">
<p>
{{ feature.properties.radar_id }}, in {{ feature.properties.country }}
since {{ feature.properties.start }}.
</p>
<RouterLink :to="`/radars/${feature.properties.radar_id}`">More about this radar</RouterLink>
</template>
</DeployMap>
In TypeScript, the option types are exported as well:
DeployMapConfig, FilterConfig,
StyleConfig, PanelConfig and others.