Leaflet
Accessing OS NGD API – Features via Leaflet
What you'll need
1
Set up your HTML file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OS NGD API – Features | Template (EPSG:3857) | Leaflet</title>
<!--Add the Ordnance Survey Styling-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/OrdnanceSurvey/[email protected]/os-api-branding.css" />
<script src="https://cdn.jsdelivr.net/gh/OrdnanceSurvey/[email protected]/os-api-branding.js"></script>
<!--Add the Leaflet libraries-->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<style>
/* Set the map container size and style */
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<!--Create a div element to hold the map-->
<div id="map"></div>
<!--Add your Javascript code below-->
<script>
// Your Javascript code will go here
</script>
</body>
</html>2
3
Add a basemap
// Initialize the map.
const mapOptions = {
minZoom: 7,
maxZoom: 20,
center: [ 50.727589, -3.541809 ],
zoom: 18,
maxBounds: [
[ 49.528423, -10.76418 ],
[ 61.331151, 1.9134116 ]
],
attributionControl: false
};
const map = L.map('map', mapOptions);
// Load and display ZXY tile layer on the map.
const basemap = L.tileLayer(`https://api.os.uk/maps/raster/v1/zxy/Light_3857/{z}/{x}/{y}.png?key=${apiKey}`, {
maxZoom: 20
}).addTo(map);4
Add an OS NGD API – Features layer
// Add layer group to make it easier to add or remove layers from the map.
const lyrGroup = new L.layerGroup().addTo(map);
// Define an asynchronous function to fetch and display the NGD Features API features.
async function fetchFeatures(bounds) {
// Generate a BBOX string for the map extent.
const bbox = bounds.toBBoxString();
// Construct the NGD Features API request URL.
const url = `https://api.os.uk/features/ngd/ofa/v1/collections/${collectionId}/items?key=${apiKey}&bbox=${bbox}`;
// Fetch features from the API endpoint.
const features = await fetch(url).then(response => response.json());
// Parse the GeoJSON data and display it on the map.
lyrGroup.clearLayers().addLayer(L.geoJSON(features));
}
// Get the visible map bounds (BBOX).
let bounds = map.getBounds();
// Initial fetch and display of features.
fetchFeatures(bounds);5
Load and update features on the map dynamically
// Add event which will be triggered when the map has finshed moving (pan + zoom).
// Implements a simple strategy to only request data when the map viewport invalidates
// certain bounds.
map.on('moveend', function() {
let bounds1 = new L.latLngBounds(bounds.getSouthWest(), bounds.getNorthEast()),
bounds2 = map.getBounds();
if( JSON.stringify(bounds) !== JSON.stringify(bounds1.extend(bounds2)) ) {
bounds = bounds2;
fetchFeatures(bounds);
}
});What's next?
Last updated
Was this helpful?