UK Open Street Map With Draggable Features

OpenLayers Version

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.

Help

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.

OpenLayers' API

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:

  1. One certain bug, one probable one, and a further inconsistency in the attribution control   the first is the hard-coding of a spelling mistake in the '-collapse' class name suffix, the second is that a layer's source getAttributions function returns not the attributions themselves as one would naturally expect, but another function that itself returns them, the third is an inconsistency in image handling, in that in some places and image is specified as HTML text which is then passed to OL, while in others it must be created as a DOM element and then passed to OL.
  2. One in the z-order styling of the zoom-slider control, such that when the mouse pointer is moved over the track, the increase in its opacity overlays the button. 
  3. Depending on which version you use, the MousePosition control is just as inflexible as it used to be in v3, in that it is still impossible to have it reliably display the centre of the map when the mouse pointer is outside of it.  You can do it in v6.5.0, but the initial display on loading is blank because, in the constructor, the element is drawn before all the relevant options that could affect its appearance have been read from the option parameters, and further the only way to make it display anything on mouseout is 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 placeholder property 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.

Updated Description
04/01/2022 First fully working version, based on sister Ordnance Survey page UKOSOpenLayers.shtml.
01/03/2022 Created replacement mouse position control and found styling method to remove its background when there is no text being displayed.