场景
Leaflet中使用leaflet.browser.print插件实现导出图片:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/123996714
上面用leaflet.browser.print插件实现导出图片,该插件主要是用来进行打印/导出为pdf的。
插件地址:
https://github.com/Igor-Vladyka/leaflet.browser.print
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
1、插件打印在线演示效果地址
https://igor-vladyka.github.io/leaflet.browser.print/
2、引入插件
<script type="text/javascript" src="./js/leaflet.browser.print.min.js"></script>
3、复制官方示例代码
var customActionToPrint = function(context, mode) {
return function() {
window.alert("We are printing the MAP. Let's do Custom print here!");
context._printMode(mode);
}
};
L.control.browserPrint({
title: 'Just print me!',
documentTitle: 'Map printed using leaflet.browser.print plugin',
printLayer: tiles,
closePopupsOnPrint: false,
printModes: [
L.BrowserPrint.Mode.Landscape("Tabloid",{title: "Tabloid VIEW"}),
L.browserPrint.mode("Alert",{title:"User specified print action",pageSize: "A6", action: customActionToPrint, invalidateBounds: false}),
L.BrowserPrint.Mode.Landscape(),
"Portrait",
L.BrowserPrint.Mode.Auto("B4",{title: "Auto"}),
L.BrowserPrint.Mode.Custom("B5",{title:"Select area"})
],
manualMode: false
}).addTo(map);
4、完整示例代码
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>leaflet-browser-print实现打印</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<style>
html,
body,
#map {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
</style>
</head>
<body >
<div id="map"></div>
<script type="text/javascript" src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<script type="text/javascript" src="./js/leaflet.browser.print.min.js"></script>
<script type="text/javascript">
var map = L.map('map').setView([36.09, 120.35], 13);
var tiles =
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: ''
});
tiles.addTo(map);
L.marker([36.09, 120.35]).addTo(map)
.bindPopup("<b>Hello world!</b><br />I am a popup.").openPopup();
var customActionToPrint = function(context, mode) {
return function() {
window.alert("We are printing the MAP. Let's do Custom print here!");
context._printMode(mode);
}
};
L.control.browserPrint({
title: 'Just print me!',
documentTitle: 'Map printed using leaflet.browser.print plugin',
printLayer: tiles,
closePopupsOnPrint: false,
printModes: [
L.BrowserPrint.Mode.Landscape("Tabloid",{title: "Tabloid VIEW"}),
L.browserPrint.mode("Alert",{title:"User specified print action",pageSize: "A6", action: customActionToPrint, invalidateBounds: false}),
L.BrowserPrint.Mode.Landscape(),
"Portrait",
L.BrowserPrint.Mode.Auto("B4",{title: "Auto"}),
L.BrowserPrint.Mode.Custom("B5",{title:"Select area"})
],
manualMode: false
}).addTo(map);
</script>
</body>
</html>
5、效果
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/136033.html