Having used Leaflet.js as the mapping component for my Trig Point App project I now see it cropping up everywhere. Two rather neat features that I see on many websites are the ability to switch between different map layers and to be able to filter which icons are being displayed. I was especially interested in the first feature as I seem to be having trouble with the availability of OpenTopoMap so I wanted to offer OpenStreetMap as an alternative option.
Leaflet.js Layers Control
The solution is to be found in the Leaflet.js layers control options, described rather neatly in this documentation page. The steps are:
- Create an array of the different map base layers using the Leaflet tileLayer() function
- Create an array of the different filtering groups using the Leaflet layerGroup() function. In my case I wanted three groups: “available”, “not available” and “destroyed”.
- Create the map as normal but with an extra parameter called layers: [ … ] which defines which base layer and ( optionally ) which filtering groups to show on startup.
- Add a filtering control to the map with the control.layers() function.
- Place the icons on the map by adding them to the relevant group rather than directly to the map.
This was remarkably straightforward and needed only a small amount of code refactoring. Now when the map is displayed there is an extra control as follows:
Ordnance Survey Maps Overlay
The other reason for exploring the map layers options was that I had recently come across this project to add an OS overlay to a Leaflet.js map. I’d investigated OS overlays before but come to the conclusion that it was not possible to get the extremely informative Landranger ( 1:50000 ) or Explorer ( 1:25000 ) scale maps without paying a great deal of money. However the example page ( right ) clearly shows the Landranger zoom to be available so I got quite enthused, especially as my friend Richard had managed to get this to work as well.
But ( you knew this wasn’t going to end well 🙂 ) when I tried to get an API key to test an OS map overlay things went a bit pear shaped. It appears that the API that’s required by the “os-leaflet” project is no longer supported even though it still works for existing keys. Further digging reveals:
- OpenSpace is no longer being developed. See https://www.ordnancesurvey.co.uk/forums/discussion/1010339/is-this-forum-active
- The page at https://www.ordnancesurvey.co.uk/business-government/tools-support/openspace-support states: “Existing customers can continue to use OS OpenSpace Free but the product is now closed to new users. If you’re looking for a web map, please take a look at our new Zoomstack product.” ( There seems to be no information as to how long it will continue working for existing users either )
- The Zoomstack product doesn’t appear to have an API and you have to download the tiles and manage them yourself. Could be interesting but it’s a fairly major project in itself not to mention finding web hosting for it.
- There seems to be a new product called OS Maps API for Enterprise but unsurprisingly it doesn’t have a free plan. From a quick check you can’t even get a trial key if you’re not a company.
( The full gory details can be found in this GitHub issue. )
The Ordnance Survey is quite rightly regarded as a British institution and its paper maps are excellent. However its digital strategy seems to be completely random with products being setup and then dropped some time later. Its Android app doesn’t escape criticism either as per this rather sorry tale from The Register. At the time of writing the app is still borked as I can’t view my offline maps unless I’m online 😦 Even the most rudimentary testing should have caught that.
Postscript
As a result of the previous posts on the Trig Point App a reader of this blog contacted me and we had a good discussion about mapping libraries. His website at http://www.mainjoin.eu/ has got quite a few examples but I particularly like the weather app. There are plenty of interesting options to investigate.