Dojo + OpenLayers = New Challenges

Recently, the new Dojo 1.7 was released with many improvements and important changes.

As a developer, I’m pleased to work with Dojo on RIA application because simple reasons:

  • It offers almost all I need: module organization, an extensive set of widgets to create interfaces, manipulation of DOM elements, AJAX communications, etc.
  • It is relatively easy to use and learn. Here I found the declarative way to work really useful because helps developers and designers work together but on different things.
  • Creating custom widgets, after some relatively easy learning curve, brings you more power and flexibility.

Yes, Dojo is a great project, but as I mentioned at the beginning I like it for RIA projects, that is, for big or complex CRUD applications. For small things, like nice web pages, I prefer jQuery plus some plugins.

Looking at the new features of Dojo 1.7 I found a new module dojox.geo.openlayers which acts as a wrapper around OpenLayers GIS library and extends with some Dojo capabilities.

Now, add Dojo GFX shapes or Dojo widgets on top OpenLayers is possible, opining new possibilities. With this merge we can do things like add pie charts to our maps to show to statistics information. It is awesome !!!

A very short introdutcion to dojox.geo.openlayers package

Before making a little demo playing with all this new things, we need a short description.

The main class in the package is dojox.geo.openlayers.Map which acts as a kind of wrapper around the OpenLayers.Map class. You can access at any time to the OpenLayers underline object with the getOLMap() method.

Next, there is the dojox.geo.openlayers.Layer class which is a wrapper around the OpenLayers.Layer class. It allows to attach to the map any Dojo element.

The dojox.geo.openlayers.GfxLayer is a subclass of the previous class specially designed to work with GFX shapes and render them on top of OpenLayers map.

The dojox.geo.openlayers.Feature class represents the features we can attach to a dojox.geo.openlayers.Layer. The subclass dojox.geo.openlayers.GeometryFeature is designed to render LineString, Points and Collections of geometries.

Because GeometryFeature works with dojox.gfx package to render the geometries you need to add it always to a dojox.geo.openlayers.GfxLayer layer.

Hands on code

We are going to create a map with three features:

  • a dojox.gfx shape feature, with an animation attached to it to change it fill color,
  • a pie chart and
  • a stacked area chart.

You can see the demo in action here, and it should looks something like:

Dojo + OpenLayers

Lets go. Start creating new HTML page and paste the next code, which correspond to the skeleton of our demo:

I’m using Dojo 1.7 and the new way to work using require. As you can see I’m referencing the modules my code requires from dojo and dojox packages

Once Dojo is loaded the ready is executed showing the Dojo version in the console and executing the code shown next, which really creates the map and the features.

In the “here put the magic” section we can place the code that creates a new dojox.geo.openlayers.Map instance:

Next create a GFX layer. On it we can attach geometry features or any other Dojo widget feature.

Remember you only can attach geometry features to a GFX layer.

Now create a geometry feature, defined by a LineString with some points, and add it to the layer:

We want to animate the fill color of the geometry feature so we need to create a couple of animation. The first one will change the fill color from the original color to transparent, while the second will change from transparent to the original color again.

The trick is once the first animation ends we start the second animation (using the ‘onEnd‘ method) and the same for the second animation. This way we create an infinite animation effect.

Now, we are going to create a pie chart widget feature. We are going to use the dojox.geo.openlayers.WidgetFeature class which required an object in the constructor defining the widget to create, the lat/lon where to place the widget and some more parameters. You can find all the available parameters in the source code documentation:

The pie chart widget have a 100×100 size and will we located at (lon,lat)=(5, 40). The createWidget property must point to a function responsible to create the appropriate widget:

Similarly, we create a second widget feature with a stacked area chart with different dimensions and location:

Once we have all the features in the layer, it is a good idea to refresh it:

Conclusions

I think the fusion of Dojo with OpenLayers opens new possibilities to create rich content and behavior mapping applications. We can create dojo features and make them really amazing using effects, animation and events (like this).

I usually work with OpenLayers and, because Dojo simply acts as a wrapper on some OpenLayers classes, we can access them and work directly adding data from WFS server, GML files, etc

Is it a perfect solution? As always it depends on your needs but for all of us which work in complex RIA applications that needs some GIS features the dojox.geo.openlayers package offers a new spot light :)

9 Responses

  1. Al Reno February 14, 2012 / 18:22

    Thanks for that post (although it’s mostly an explanation of what’s going on in dojox/geo/openlayers/tests/widgetFeature/test_widgetFeature.html). It’s strange that we can do amazing things with those frameworks and sometimes there are absurd limitations. For instance, I want to show a georeferenced png version of an svg icon (because Android does not support SVG) so I did something like this, and it does not show anything (and no errors either); Any idea how to do this?
    var markers = new OpenLayers.Layer.Markers( “Markers” );
    map.map.getOLMap().addLayer(markers);
    var size = new OpenLayers.Size(210,250);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon(‘js/OpenLayers/img/marker.png’,size,offset);
    markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(-72.1003,48.4003),icon));
    markers.redraw();

  2. asantiago February 14, 2012 / 20:22

    Thanks Al.
    Really I can’t help you too much.
    I test your code on desktop Chrome and Firefox browsers and it works fine, in my case without using a Map widget which make the code change in line “map.getOLMap().addLayer(markers);”.
    Maybe you can put the question on Dojo or OpenLayers forums if it seems a problem on mobiles.
    Let me know if you found the problem !!!

    • Al Reno February 15, 2012 / 17:59

      I want to thank you because your taking the time to do that test with success pushed me to revisit that coding. At first, I thought the widget/not-widget nature of the map was the explanation since it appeared to be the only difference. After trying that change without more success, I had a flash (I must say I had that doubt before). It turns out the answer is more trivial: OpenLayers doc says you provide a LongLat object (+icon) to create the marker; that’s WRONG. What you provide is a coordinate in WHATEVER system the map is currently in; so the solution was as trivial as this (given I use Google as provider):
      po1=new OpenLayers.LonLat(-72.1,48.4).transform(geo,mercator);
      markers.addMarker(new OpenLayers.Marker(po1,icon));
      Bad documentation again, that’s the plague. Thanks again for your help, A.R.

  3. asantiago February 15, 2012 / 20:10

    Yes, you must take into account always express lonlat values (on markers, points, etc) in the same coordinate system of the layer it contains or, if the layer has no projection indicated, in the map’s projection.
    Thanks for your comments !!!

  4. Bart van Deenen August 2, 2012 / 15:28

    Hi.
    Thanks for the nice explanation, it helped me a lot in putting dijits inside an openlayer map. There’s one really stupid problem that I can’t seem to solve directly:
    If I don’t use the animated blob, the two graph widgets are no longer locked to the map! Dragging the map will cause the widgets to return to their previous screen position after you let go of the mouse. You can very easily test this by commenting out animA.play() (dojoGeo.html:101).
    I’m trying to find out what’s actually happening here, but any help would be appreciated.

  5. Java Experience December 29, 2012 / 18:42

    this tutorial is very detailed and helpful. I have also posted a jsfiddle for dojo animation, some people may be coming here looking for a working simple demo for the same.

  6. lely April 3, 2013 / 09:53

    Hi,

    Thanks much for your post. It helped me solved my problem. I was wondering, you mentioned that for simple web pages, it is preferrable to use jQuery and some plugin instead. Specifically, I am into merging map and pie charts. I want to use OpenLayers for the map. As for the pie chart, I am not sure what to use. I looked for several alternatives and ended up in jqPlot, but haven’t figured out how to merge with the OpenLayers.
    Do you think this is a good idea?, otherwise, please kindly point me to some useful resources to learn from.

    Thanks!

    Regards.

  7. asantiago April 3, 2013 / 18:56

    Hi lely,
    that simply question is hard to answer :) Usually there is no silver bullet.
    I think the combination of jQuery + jqPlot is good enough, all depends on your needs.

    Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">