how to export openlayer3 map to png with its scale bar using js(如何使用js将Open layer3地图及其比例尺导出为PNG)
本文介绍了如何使用js将Open layer3地图及其比例尺导出为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用js中的画布将Openlayer3地图导出为PNG。实际上,地图div和比例尺div是不同的,所以在将地图导出为PNG时,如何在同一个PNG上绘制比例尺及其图例。
推荐答案
我们需要创建控件并从html获取控件,然后add it to canvas如下所示
InsertToCanvas = (function() {
//get the canvas element
var canvas = $('canvas').get(0);
//get the Scaleline div container the style-width property
var olscale = $('.ol-scale-line-inner');
//Scaleline thicknes
var line1 = 6;
//Offset from the left
var x_offset = 10;
//offset from the bottom
var y_offset = 30;
var fontsize1 = 15;
var font1 = fontsize1 + 'px Arial';
// how big should the scale be (original css-width multiplied)
var multiplier = 2;
/* go for it */
function WriteScaletoCanvas(e) {
var ctx = e.context;
var scalewidth = parseInt(olscale.css('width'),10)*multiplier;
var scale = olscale.text();
var scalenumber = parseInt(scale,10)*multiplier;
var scaleunit = scale.match(/[Aa-zZ]{1,}/g);
//Scale Text
ctx.beginPath();
ctx.textAlign = "left";
ctx.strokeStyle = "#ffffff";
ctx.fillStyle = "#000000";
ctx.lineWidth = 5;
ctx.font = font1;
ctx.strokeText([scalenumber + ' ' + scaleunit], x_offset + fontsize1 / 2, canvas.height - y_offset - fontsize1 / 2);
ctx.fillText([scalenumber + ' ' + scaleunit], x_offset + fontsize1 / 2, canvas.height - y_offset - fontsize1 / 2);
//Scale Dimensions
var xzero = scalewidth + x_offset;
var yzero = canvas.height - y_offset;
var xfirst = x_offset + scalewidth * 1 / 4;
var xsecond = xfirst + scalewidth * 1 / 4;
var xthird = xsecond + scalewidth * 1 / 4;
var xfourth = xthird + scalewidth * 1 / 4;
// Stroke
ctx.beginPath();
ctx.lineWidth = line1 + 2;
ctx.strokeStyle = "#000000";
ctx.fillStyle = "#ffffff";
ctx.moveTo(x_offset, yzero);
ctx.lineTo(xzero + 1, yzero);
ctx.stroke();
//sections black/white
ctx.beginPath();
ctx.lineWidth = line1;
ctx.strokeStyle = "#000000";
ctx.moveTo(x_offset, yzero);
ctx.lineTo(xfirst, yzero);
ctx.stroke();
ctx.beginPath();
ctx.lineWidth = line1;
ctx.strokeStyle = "#FFFFFF";
ctx.moveTo(xfirst, yzero);
ctx.lineTo(xsecond, yzero);
ctx.stroke();
ctx.beginPath();
ctx.lineWidth = line1;
ctx.strokeStyle = "#000000";
ctx.moveTo(xsecond, yzero);
ctx.lineTo(xthird, yzero);
ctx.stroke();
ctx.beginPath();
ctx.lineWidth = line1;
ctx.strokeStyle = "#FFFFFF";
ctx.moveTo(xthird, yzero);
ctx.lineTo(xfourth, yzero);
ctx.stroke();
}
function postcompose() {
map.on('postcompose', function (evt) {
WriteScaletoCanvas(evt);
});
}
return {
postcompose : postcompose
};
})();
InsertToCanvas.postcompose();
//Now export map as png
var exportMap = function () {
canvas = document.getElementsByTagName('canvas')[0];
canvas.toBlob(function (blob) {
alert("jsp page export map function");
saveAs(blob, 'map.png');
})
}
这样将向PNG添加比例尺。
图例的第2步
function WriteLegendtoCanvas(e) {
var ctx = e.context;
var x=50,y=50;
var arr = $('div#legendId img');
for(i=0;i<arr.length;i++)
{
var img = new Image();
img.src = $(arr[i]).attr('src');
img.onload = function() { ctx.drawImage(img, x, y); }
ctx.drawImage(img, x, y);
ctx.beginPath();
ctx.lineWidth = line1;
ctx.strokeStyle = "#FFFFFF";
ctx.moveTo(x, y);
y+=20;
ctx.lineTo(x, y);
ctx.stroke();
}
}
并使用postcompose调用此函数
function postcompose() {
map.on('postcompose', function (evt) {
WriteScaletoCanvas(evt);
WriteLegendtoCanvas(evt);
});
}
这篇关于如何使用js将Open layer3地图及其比例尺导出为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何使用js将Open layer3地图及其比例尺导出为PNG
猜你喜欢
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01