要在 OpenLayers 中导出整幅地图为 PNG 图片并保存下载,可以使用 HTML5 Canvas 的 toDataURL
方法。这个方法将地图渲染到一个 Canvas 元素,然后将该 Canvas 转换为一个图片数据 URL,并提供一个下载链接供用户保存图片。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>OpenLayers Export Map to PNG</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/css/ol.css" type="text/css" />
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script>
<style>
#map {
width: 100%;
height: 90vh;
}
#export-button {
margin: 10px;
padding: 10px;