Map
Map shows places on an interactive OpenStreetMap map, with popups rendered by Angular, two-way center and zoom, and a list of the places as its accessible alternative. It uses Leaflet, an optional peer dependency loaded in the browser only: no API key, no account and no stylesheet to add.
Import #
import { UIMap } from '@exhaustive-ui/core/map';Map draws its map with Leaflet 1.9 (BSD-2-Clause), an optional peer dependency: install it, and that is all. There is no API key, no account, no token and no stylesheet to add: the Leaflet layout rules ship with the Map styles and follow the theme, light and dark. Leaflet is loaded with a dynamic import, in the browser only, when a map is displayed, so it weighs nothing on the pages without a map nor during server-side rendering.
npm install leafletLeaflet 1.9 is published as CommonJS: add leaflet to allowedCommonJsDependencies in angular.json to silence the build warning about it. In unstyled mode the Leaflet layout rules are left out with the rest of the styles: import leaflet/dist/leaflet.css and style the classes yourself. Clustering of many markers is not built in.
The class is named UIMap, so that it does not hide the JavaScript Map in the files that import it; the element stays p-map. MapModule exports it as well.
Basic #
Give the map a center, as a latitude and a longitude, and a zoom level; markers adds places, each with an id, a position and a title. The tiles come from OpenStreetMap, whose attribution is always shown. The scroll wheel zooms with ctrl or ⌘ held, so that the page keeps scrolling over the map.
Markers and Popups #
A click on a marker, or enter on a focused one, opens its popup. The default popup shows the title and the description of the marker; the popup template replaces it with your own content, rendered by Angular when the popup opens, with its bindings, components and events, never as an HTML string. The data of a marker carries anything else you need.
Center, Zoom and Events #
center and zoom are two-way: move or zoom the map and they follow, set them and the map moves. onMoveEnd also gives the area shown, as north, south, east and west bounds, to load the places of that area. onMarkerClick is emitted by a click on a marker, enter or space on a focused one, or a choice in the list of places, and onMapClick gives the position of a click elsewhere on the map.
- Center
- 48.8566, 2.3522
- Zoom
- 12
- Last event
- Click a marker or the map
Fit to Markers #
With fitMarkers, the map shows all the markers each time they change, instead of using center and zoom. A map without center also fits its markers at first, and shows the world when it has none. The fitToMarkers method does the same on demand.
Tile Source #
By default the map shows the standard OpenStreetMap tiles, https://tile.openstreetmap.org/{z}/{x}/{y}.png, with the attribution their license requires, © OpenStreetMap contributors, which cannot be removed with these tiles. No key and no account are needed.
These tiles are run by volunteers, and the OpenStreetMap tile usage policy forbids heavy traffic: they are fine for development, demos and light use. For an application with real traffic, point tileUrl at your own tile server, which you can build from the OpenStreetMap data, or at another tile provider whose terms you accept, and give its attribution, as trusted HTML. minZoom and maxZoom follow the zoom levels of the source, and a nulltileUrl shows no tiles, for a map whose layers you add yourself.
<p-map
[center]="{ lat: 48.8566, lng: 2.3522 }"
[zoom]="13"
tileUrl="https://tiles.example.com/{z}/{x}/{y}.png"
attribution="© <a href='https://www.openstreetmap.org/copyright'>OpenStreetMap</a> contributors, tiles by Example Maps"
[maxZoom]="18"
/>Scroll Wheel and Static Map #
scrollWheelZoom decides when the wheel zooms the map. By default, 'ctrl', it zooms with ctrl or ⌘ held, and a plain wheel scrolls the page with a short hint over the map, so that a long page never gets stuck on it; true always zooms and false never does.
With interactive set to false, the map is a static view: it cannot be dragged nor zoomed, has no zoom buttons and leaves the tab sequence, while its markers still open their popups. When the user prefers reduced motion, the map moves and zooms without animation.
Template #
The marker template replaces the default pin, anchored on the position of the marker by its bottom center, and the item template the content of a place in the list. Both receive the marker. The marker stays a button named by its title, so the visible text of the template has to be part of the title, as the line numbers here.
Leaflet API #
onReady gives the Leaflet map and the Leaflet namespace once the map is ready, for everything the inputs do not cover: lines, areas, GeoJSON or any Leaflet plugin. Install @types/leaflet to type them. Here a walk along the Seine is drawn with a polyline.
Accessibility #
List of Places
A map is hard to use without sight, so the places it shows are also given as text: a list below the map, shown and hidden by a List of places button with aria-expanded. Each place is a button with its title and its description; choosing one moves the map to it and opens its popup, and with the keyboard the focus goes into the popup. list set to 'visible' always shows the list, and 'none' removes it, when the page already lists the places elsewhere. The list is rendered on the server too, before Leaflet is loaded.
Screen Reader
The map is a region with the map role description, named by ariaLabel, ariaLabelledBy or the map label (give each map of a page its own name), and described by keyboard instructions. Each marker is a button named by its title, with aria-haspopup and aria-expanded; its popup is a dialog named by the same title, whose close button is labelled. The zoom buttons are named Zoom in and Zoom out, from the zoomIn and zoomOut aria texts of the translation configuration, and a polite live region announces the new zoom level. While Leaflet loads, the map is aria-busy with a loading status, and an alert tells when it could not be loaded.
Every text comes from the labels input: the map name and role description, the instructions, the zoom buttons, the close button, the zoom announcement, the scroll hint, the loading and error messages, and the list texts.
<h2 id="stores-title">Our stores</h2>
<p-map [markers]="stores" ariaLabelledBy="stores-title" list="visible" />
<p-map [markers]="stores" [labels]="{ map: 'Carte', roleDescription: 'carte', zoomIn: 'Zoomer', zoomOut: 'Dézoomer', closePopup: 'Fermer', listToggle: 'Liste des lieux ({0})', list: 'Lieux sur la carte' }" />Map Keyboard Support
| Key | Function |
|---|---|
| tab | Moves the focus to the map, then the zoom buttons, the markers, the open popup and the attribution links. |
| arrow keys | Moves the map when it has the focus; with shift, by a larger step. |
| + / - | Zooms in or out when the map has the focus; with shift, by three levels. |
| enter / space | On a marker, opens its popup, moves the focus into it and emits onMarkerClick. |
| escape | Closes the open popup and returns the focus to the marker or the place of the list that opened it. |
List Keyboard Support
| Key | Function |
|---|---|
| enter / space | On the List of places button, shows or hides the list. On a place, moves the map to it and opens its popup with the focus inside. |