3
votes

I'm trying to export an 2.13.1 openLayers map into a canvas, but the methods to do so seem to have vanished.

The old methods would work like:

var exportMapControl = new OpenLayers.Control.ExportMap();
map.addControl(exportMapControl);

var canvas = OpenLayers.Util.getElement("exportedImage");
exportMapControl.trigger(canvas);   

Does anyone know how this works for 2.13.1?

Thanks in advance,

2
I am facing the same issue... This is not working for my code, I have added ExportMap.js and Grid.js, I am using WMS layers only, but answered solution is not working for me... :( - prem30488

2 Answers

2
votes

The ExportMap control was actually never a part of the official OpenLayers distribution (you can confirm that by checking out the project on github and browsing history). The example above comes from a sandbox that seems to be a user-submitted enhancement proposal (demo here).

The source of this custom control is available HERE. Simply import it after loading OL 2.13.1 and it should be available as OpenLayers.Control.ExportMap again. Unless some other part of OL it depends on changed in the meantime it should work as it used to.

0
votes

I won't write the whole function here (as it's already on two stack sites), but this solution using html2canvas allows you to export a dataURL for your OpenLayers 2 map.

In my app - I then use the dataURL to add the map to a pdf using jsPDF.

WMS, Vector and base layers are exported in this solution.

There appears to be better canvas support in OL3, as mentioned in my linked answer - I've seen a pretty convincing toBlob demo for OL3.