MapLibre GL JS
Accessing OS NGD API – Features via MapLibre GL JS
Last updated
Accessing OS NGD API – Features via MapLibre GL JS
Last updated
MapLibre GL JS is a free and powerful JavaScript library for displaying interactive maps on the web. It's based on Mapbox GL JS and provides a wide range of features for creating maps with custom styles, markers and interactivity.
OS Maps API and OS NGD API – Features added to an API project in the OS Data Hub with an API Key. See for more information.
A text editor like Visual Studio Code or Notepad to edit and save your HTML and JavaScript files.
Create a new HTML file with a text editor (for example, Notepad, Visual Studio Code).
Add the basic HTML structure to your file with a placeholder <div>
for the map.
To enable access to OS APIs an API Key is required. Inside the <script>
tag, add a variable called apiKey
, replacing 'INSERT_API_KEY_HERE'
with the API Key from your project.
Add a variable called collectionId
, replacing 'INSERT_COLLECTIONID_HERE'
with the collection ID for the desired OS NGD feature type and version (for example, bld-fts-buildingpart-1).
To add the OS Maps API, you will need to define the map style using MapLibre GL JS's format. This specifies the source of map tiles, which will be retrieved from OS Maps API in the 'Light' raster tiles style.
Initialise the map object using the maplibregl.Map
class to configure the basemap layer and define its properties – container
, minZoom
, maxZoom
, maxBounds
, style
, center
and zoom
.
Add navigation controls to the map, excluding the compass button and disabling map rotation.
The above code creates the main map instance using the MapLibre GL JS library where you can specify various properties:
container
: Defines where the map should be displayed. In this instance, it is set to the id
of the <div>
element.
minZoom
and maxZoom
: Sets the minimum and maximum zoom level for the map. Users will not be able to go beyond these levels.
maxBounds
: Defines the maximum bounds and restricts panning the map.
style
: Defines the style of the map, configured via a URL pointing at the style specified.
center
: Sets the initial centre point of the map.
zoom
: Sets the initial zoom level of the map.
Create an empty GeoJSON placeholder to hold the feature objects called by OS NGD API – Features.
Create a function called fetchFeatures
that fetches the API based on the current map extent (bounding box) by generating a bbox string.
Construct the API request URL to fetch OS NGD data from OS NGD API – Features. The URL includes the collectionId
, bbox
and apiKey
.
Once the features have been returned in JSON, update the source data of the map's layers to display the features.
Event listeners are triggered when the map loads and finishes moving (panning or zooming) to load and update features based on the map's updated extent. Inside the map.on('load',...)
event handler, we add styles for various types of features, including polygons, linestrings and points so that any collectionId
specified will render.
The map.on('moveend',...)
event handler will then fetch and update the features based on the map's current extent.
Features within the viewport extent will load initially (first 100 features) and will continue to load as you pan and zoom across the map.
Congratulations! You've successfully created a map using MapLibre GL JS and added an OS NGD layer using OS NGD API – Features in a few steps.
Now you can continue to explore Ordnance Survey's code examples to learn more about advanced features and functionality, such as adding markers, pop-ups, and additional layers.