首页 文章

如何下载 Map 选定部分的OSM图块

提问于
浏览
-1

我想使用Openlayer OSM层的单一缩放级别离线下载 Map 的选定部分的 Map . 我有 Map 的四个角,即 Map 的显示部分 .

但需要在四个角之间获取所有瓷砖图像或瓷砖 . 我已经回顾了一些例子:

Openlayers get the image url of the tile under the mouse

https://gis.stackexchange.com/questions/167792/how-to-retrieve-the-tile-url-in-openlayers-3

但我需要在客户按钮点击下载瓷砖 . 谁能帮帮我吗 .

1 回答

  • 0

    这是一个将tile保存为数据URL供以后使用的简单示例 . 如果在关闭并重新打开浏览器后需要保存的磁贴保持可用,请将sessionStorage替换为localStorage .

    // load OSM zoom level 8 tiles for Switzerland to data urls
    
    var extent = ol.proj.transformExtent([5.9,45.8,10.55,47.85],'EPSG:4326','EPSG:3857');
    var zoom = 8;
    
    var source = new ol.source.OSM();
    
    source.getTileGrid().forEachTileCoord(extent, zoom, function(tileCoord) {
        var img = document.createElement("img");
        img.onload = function() {
            var canvas = document.createElement("canvas");
            canvas.width = source.getTileGrid().getTileSize(zoom);
            canvas.height = source.getTileGrid().getTileSize(zoom);
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0);
            sessionStorage.setItem('OSM_' + tileCoord[0] + '_' + tileCoord[1] + '_' + (-tileCoord[2]-1), canvas.toDataURL());
            img.remove();
            canvas.remove();
        };
        img.crossOrigin = "Anonymous";
        img.src = source.getTileUrlFunction()(tileCoord);
    });
    
    // wait a few seconds to ensure data urls are ready, then create a map to use them
    
    setTimeout(function(){
    
        map = new ol.Map({
            target: 'map',
            layers: [
                new ol.layer.Tile({
                    extent: extent,
                    source: new ol.source.XYZ({
                        attributions: ol.source.OSM.ATTRIBUTION,
                        maxZoom: 8,
                        minZoom: 8,
                        tileUrlFunction: function(tileCoord) {
                            return sessionStorage.getItem('OSM_' + tileCoord[0] + '_' + tileCoord[1] + '_' + (-tileCoord[2]-1));
                        }
                    }),
                })
            ],
            view: new ol.View({
                center: ol.extent.getCenter(extent),
                zoom: 8
            }),
            controls: ol.control.defaults({
                attributionOptions: { collapsible: false },
            })
        });
    
    }, 3000);
    

相关问题