在openlayers 4中返回画布(图像) [英] Return canvas (image) in openlayers 4

查看:140
本文介绍了在openlayers 4中返回画布(图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该示例显示了如何将按钮单击事件绑定到画布,然后返回图像

the example shows how to bind a button click event to the canvas and then it returns the image Example. How can I change it, that when I use a call openlayers with a permalink, that it automatically returns me that image? I would like to use a simple get request from an c++ programm to get the image. I have the e.g. "#map=12/1085115.28/6035092.46/0" as parsing parameters. Any ideas? Thanks and Greetings Melina

到目前为止,我已经进行了参数解析

So far I have the parameter parsing

<!DOCTYPE html>
<html>
  <head>
    <title>OpenStreetMap</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v4.2.0/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="https://openlayers.org/en/v4.2.0/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script>
        var center = [0,0];
        var zoom = 0;
        var rotation = 0;
        if (window.location.has !== '')
        {
         var hash = window.location.hash.replace('#map=', '');
         var parts = hash.split('/');
         console.log (parts);
         if (parts.length === 4)
         {
            zoom = parseInt(parts[0],10);
            center = [
             parseFloat(parts[1]),
             parseFloat(parts[2])
             ];
            rotation = parseFloat(parts[3]);
         var rotation = 0;
        }
    }
      var openStreetMapLayer = new ol.layer.Tile({
        source: new ol.source.OSM({
          attributions: [
            'All maps © <a href="http://www.openstreetmap.org">openStreetMapLayer</a>',
            ol.source.OSM.ATTRIBUTION
          ],
          opaque: false,
        //  url: '<myosmserver>/hot/{z}/{x}/{y}.png'
        })
      });


      var map = new ol.Map({
        layers: [
          openStreetMapLayer
        ],
        target: 'map',
        controls: ol.control.defaults({
          attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
          })
        }),
        view: new ol.View({
          maxZoom: 20,
          center: center,
          zoom: zoom
        })
      });

    </script>
  </body>
</html>

推荐答案

您无法添加将以某种方式将地图下载为图像的链接.您需要将其渲染到某个地方.

You cannot add a link that will somehow download the map as an image. You will need to render it somewhere.

这是它的工作方式.当Openlayers渲染地图时,它将在HTML canvas元素中渲染地图.下载功能不是Openlayers功能,而是HTML画布功能.画布具有用于获取当前画布快照的API.您可以将其下载为图像.

This is how it works. When Openlayers renders the map, it renders it in a HTML canvas element. The download feature is not a Openlayers feature but a HTML canvas feature. The canvas has API to take a snapshot of the current canvas. You can download it as an image.

您可以在浏览器中呈现地图,也可以在服务器端呈现地图.我没有尝试在服务器上渲染Openlayers地图,但是应该可以.

You can either render the map in a browser or render it server-side. I have not tried rendering the Openlayers map on the server but it should be possible.

这篇关于在openlayers 4中返回画布(图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆