This demo uses the version of OpenLayers API current at the time of writing, .
You can also load an Ordnance Survey Map using OpenLayers, but see the notes on that page concerning the restrictions that now apply to free access.
This section describes how some of the less obvious aspects of the demo work.
There are two interactions that select objects, and one that drags them.
You can select objects using either an ol.interaction.Select or an ol.interaction.DragSelect, the former being unaltered from the OL original, the latter being a custom extension of ol.interaction.DragBox. The former has the multi option enabled, which means that you can select multiple objects by Shift clicking. The latter is invoked by Shift dragging. The former selection is cancelled by clicking anywhere in the map outside of a feature, the latter by Shift dragging anywhere in the map outside of a feature. It's important to understand that the collections of features so selected are now properties of the interaction objects themselves, not of the layer or the map, and consequently are different selections. This can get sufficiently confusing to the user that an end-programmer may decide that, although both are included here for demonstration purposes, only one or the other should be used in production, not both.
Features are dragged by an ol.interaction.DragVector, which is an extension of ol.interaction.Pointer. Where no objects are selected as above, a simple drag drags just one feature, but if Shift is applied all features beneath the start point of the drag are dragged together, while if objects are selected as above and Alt is applied, both the above selections will be dragged. This latter is achieved by the above two interaction objects being parameters to the drag interaction's constructor.
This section describes some aspects of the current OpenLayers API
This author hasn't liked any OL version since and including 3, because even the most basic, fundamental objects either disappeared or changed radically in name and properties between OL2 and OL3, necessitating a wearisome rewrite of one's entire codebase, and, although fundamental aspects of the coding, such as encapsulation, are markedly improved, there are more aspects of it that either prevent or make very tedious the coding of some things that were fairly easy to do in v2 - such as the lack of a layer-switcher control, and there is no easy way to get the mouse position control to display the map centre when the mouse is outside of the map - while the tendency with each new version to create unwelcome 'features', aka bugs, apparently still remains. For example, just in writing this demo, I found the following bugs in the then current operational version:
mouseoutis by directly stuffing the value into
undefinedHTML_while in v6.9.0, the map will display its centre correctly on loading, but thereafter will always display that initial value for the centre whenever the mouse is outside of it, regardless of how the map has been moved since, and debugging the code reveals that, despite the
placeholderproperty supposedly being a settable option, it is name mangled in this version, and there is no way provided of updating it once the control has been created.
The coding of this demo contains workarounds for those that easily can be fixed, the others need proper code bug fixes.
The end-programmer must ensure not to rely on serendipitous discoveries from examining the debug code, because, as we have already seen with the mouse position control, subsequent testing may show failure with the live script, where many of the property and method names are mangled.
OL since 3 is more highly compressed code. While this has some advantages, for example, a reduction in overall code size, it also has some serious disadvantages, the most notable of which, the name-mangling, has already been highlighted, but besides the points already made, sometimes also this has negative consequences for the bug-fixing, extensibility, and the open exchange of techniques that should arise from Open Source software.
We seem to have lost the behaviour of the v2 scaleline control in giving both m/km and ft/mi scalelines, which is perhaps regrettable. It would be a good idea to provide this as an option, say triggered by the units option being given as a comma separated list.
|04/01/2022||First fully working version, based on sister Ordnance Survey page UKOSOpenLayers.shtml.|