As a learning exercise, I been trying to “migrate” my recent #indyref map from OpenLayers 2.13.1 to the very new version 3.0.0 of the popular mapping API. It seemed a good time to learn this, because the OpenLayers website now shows v3 as the default version for people to download and use. Much of my output in the last few years has been maps based on OpenLayers, so I have considerable interest in the new version. There are some production sites using OpenLayers 3 already – for example, the official Swiss map.
I use the term “migrate” in inverted commas, because, really, OpenLayers 3 is pretty much a rewrite, with an altered object model, and accordingly requires coding from scratch a new map rather than just changing a few lines. It has so far taken me four times as long to do the conversion, as it did to create the original map, although that is an inevitable consequence of learning as I go along.
I’ll update this blogpost as I discover workarounds.
Shortcomings in v3 that I have come across so far:
- No Permalink control. This is unfortunate, particularly as “anchor” style permalinks, which update as you move around the map, are very useful for visualisations like DataShine where people share specific views and places, and I can inject extra parameters in. The site linked above suggests this is a feature that should not be in the core mapping library, but instead an additional library can query/construct necessary parameters. Perhaps, but I think layer/zoom/lat/lon parameters are such a key part of a map (as opposed to other interactive content) that they still deserve to be treated specially.
- The online documentation, particularly the apidoc, is very sparse in places. As mentioned above, there is also some mismatching in functionality suggested in the online tutorials, to what is actually available. Another example, the use of “font” instead of “fontSize” and “fontStyle” for styles. This will improve I am sure, and there is at least one book available on OpenLayers 3, but it’s still a little frustrating at this stage.
- Label centering on the circle vectors is not as good as with OL 2. This is possibly due to antialiasing of the circle itself. You can see the labels “jump” slightly when comparing the two versions – see links below.
- Much, much slower on my iPhone 4 (and also on a friend’s Android phone). This is not what I was expecting! This is the “killer” problem for me which means I’ve kept my map on OL 2 for now. Wrapping my vector layer in an Image layer is supposed to speed things up, but causes the layer not to display on my iPhone. Disabling the potentially expensive mousemove listener did not make a difference. Adding a viewport meta tag with width=device-width speeded things up a lot so that it was almost as fast as OL 2 (without the meta tag) but then I would need to rewrite my own UI for mobile – something I don’t need to do with the OL 2 version!
- No support (yet) for UTFGrids. These are a form of vector tiles, for metadata rather than geographic features, which I use on the DataShine project.
Things which I like about the new version:
- Smooth vector resizing/repositioning when zooming in/out on a computer. (N.B. This is only when using a Vector layer and a Vector source, rather than Image layer with an ImageVector source that itself uses a Vector source.)
- Attribution is handled better, it looks nicer.
- No need to have a 100% width/height on the map div any more.
- Resolution-specific styling. I’ve used this to hide the labels when zoomed out beyond a certain amount.
- Can finally specify (in a straightforward fashion) a minimum zoom level.
- Point coordinates and extents/bounds are specified in a much simpler way.
- On a more general note, the new syntax is more complete and feels less “hacky”. The developers have taken the opportunity to do it “right” and remove inconsistencies, misplaced functionality and other quirks from the old version. For example, separating out visual UI controls and interaction management controls into two separate classes.
- Drag-and-drop addition of KML/GeoJSON vector features. Example (use this file as a test).
Some gotchas, which got me for a bit, but I was able to solve:
- You need to link in a new ol.css stylesheet, not just the Javascript library, in order to get the default controls to display and position correctly.
- Attribution information is attached to a source object now, not directly to the layer. A layer contains a source.
- Attribute-based vector styling is a lot more complicated to specify. You need to create a function which you feed in to an attribute. The function has to return a style wrapped in an array – this may be the closure syntax in Javascript that I have not come across before.
- Hover/mouseover events are not handled directly by OpenLayers any more – but click events are, so the two event types require quite different setups.
- Minor differences between the debug and regular versions of the library. The example I noticed is that the debug version allows ol.control.ScaleLineUnits.METRIC to be specified as an attribute for the ScaleLine control, but the non-debug version needs to use an explicit string “metric”.
No opacity control on individual styles – only on layers. This means I can’t have the circles with the fill at 80% opacity but the text at 100% opacity.Opacity can be set on styles, but has to be specified as part of the colour, in RGBA format (where A is the alpha, i.e. opacity, you want) rather than as a separate attribute. This is contrary to the tutorials on the website. Layer opacity can continue to be specified as seperate attributes.
My OpenLayers 3 version of the #indyref map is here – compare with the OpenLayers 2 one. Note that, since first writing this blogpost, I’ve subsequently updated the OpenLayers 2 one to change the cartography there further.
6 replies on “OpenLayers 3”
[…] change, but OL3 is a major rewrite and as such it was quite an effort to migrate to it. I’ve run into issues with OL3 before, many of which have since been resolved by the library authors or myself. (I was a bit grumbly in […]
[…] Bu blogpost‘ta Açıktabakalar2 ve 3 arasındaki farklılığı ve eski versiyondan en yeni versiyona taşınmayla ilgili bazı düşünceleri bulabilirsiniz . […]
[…] În acest blog puteți vedea deosebirea dintre Open Layers2 și Open Layers3, de asemenea tot aici puteți găsi câteva refecții cu privire la trecerea către noua versiune. […]
[…] În acest blog puteți vedea deosebirea dintre Open Layers2 și Open Layers3, de asemenea tot aici puteți găsi câteva refecții cu privire la trecerea către noua versiune. […]
[…] En este blogpost puedes ver la diferencia entre OpenLayers 2 y 3 and algunas consideraciones sobre el trasvase desde la más antigua versión a la más reciente. […]
[…] In this blogpost you can see the difference between OpenLayers 2 and 3 and some thoughts about the migration from the older version to the newest one. […]