OS OpenSpace Maps in OpenLayers 6
Updated: Jan 26
This post is probably of extremely niche interest :) But if you need it, you need it.
That API is now closed to new users and has been replaced by a new, vector-tile based API. However, the API is still in use for applications that were registered before about Sept 2019 and they have not announced end-of-life yet - it will be difficult given that maps provided by the new API are totally different to the old maps and the change is a big, breaking change.
One of the problems with the OpenSpace API was the official support for OpenLayers only supported v2. I needed to update an OpenSpace based application to OpenLayers v6 (latest version) and this is the details of how I did that.
OpenLayers v2 is a very old version and there was a major rewrite from v2 to v3 that changed fundamentally the structure of the API - which is probably why the OS never migrated.
Luckily, there was a community-supported port to V3 that was very good - openspace-ol3.
However - there have been some changes since v3 that stopped this port working.
The complete code is shown in the following gist. The major changes from openspace-ol3 are around the imports and around the details in the hack to fix the resolution called on the API - where the mechanisms to call the API have changed.
The interface is used by creating a new OpenSpaceOL6 instance and calling methods on that. The most important method being .getLayer() to get the layer to be used in the map.
A working example is shown in the following stackblitz sample. (or will be working when a problem with the API key is sorted)