【问题标题】:OpenLayers 3: Trying to add image underlay to a vector layerOpenLayers 3:尝试将图像底图添加到矢量图层
【发布时间】:2015-08-10 14:55:10
【问题描述】:

目前我有一个使用 KML 文件作为矢量源显示的矢量地图。

我想要做的是在这张矢量图上放置一张图片。

该地图是室内平面图,图像与矢量图完全相同,只是其中有更多细节,上面写有文字等。我需要的是地图图像在矢量图下方,以便矢量地图的墙壁与图像的墙壁完美对齐。之所以会发生这种情况,是因为 KML 是通过使用 QGIS 在图像之上进行跟踪而创建的。

到目前为止,我已经能够让 KML 矢量地图和 png 图像出现在地图上,但是它们彼此不对齐并且大小不一样。这就是我需要帮助的地方!

这里有一些我目前拥有的代码:

创建地图,还没有图层(从下拉框中选择地图)

var map = new ol.Map({
  layers: [],  
  target: 'floormap',
  interactions: ol.interaction.defaults({mouseWheelZoom:false}),
  view: new ol.View({
      center: [0, 0],          
      zoom: 19,
      minZoom: 15,
      maxZoom: 30
    })    
});   

将所选地图 (KML) 添加到地图

map.removeLayer(vector);

vector = new ol.layer.Vector({
    source: new ol.source.Vector({
        url: MAPS_URL + maps[map_id],
        format: new ol.format.KML()
    })
});        

map.addLayer(vector);
setMapExtent(vector);  

现在我尝试添加图像,该图像有效但未对齐

// this part here i feel may be the problem, 
// i just copied and pasted from an example om openlayers.org, 
// i dont actually know much about the extent and how to match it to 
// the vector map
var extent = [0,0,1024,684];

var projection = new ol.proj.Projection({
    code: 'xkcd-image',
    units: 'pixels',
    extent: extent
});

image = new ol.layer.Image({
    source: new ol.source.ImageStatic({
        attributions: [
            new ol.Attribution({
                html: '&copy; <a href="http://xkcd.com/license.html">xkcd</a>'
            })
        ],
        url: MAPS_URL + images[map_id],
        projection: projection,
        imageExtent: extent
    })
});

map.addLayer(image);

setMapExtent 方法

function setMapExtent(vectorMap) {
    var vectorSource = vectorMap.getSource();
    var listenerKey = vectorSource.on('change', function () {
        if (vectorSource.getState() === 'ready') {
            var extent = vectorSource.getExtent();
            map.getView().fitExtent(extent, map.getSize());
            vectorSource.unByKey(listenerKey);
        }
    });
}  

此时,我有一张矢量图,其图像位于地图上方,而且图像似乎也更小。

谁能帮我解决这个问题?

*** 解决方案! ***

一个可行的解决方案,虽然可能不是最好的方法,但它仍然有效。

var map = new ol.Map({
   layers: [],  
   target: 'floormap',
   interactions: ol.interaction.defaults({mouseWheelZoom:false}),
   view: new ol.View({
   center: [0, 0],          
   zoom: 19,
   minZoom: 15,
   maxZoom: 30
 })    

});

添加新的地图层

map.removeLayer(vector);

vector = new ol.layer.Vector({
    source: new ol.source.Vector({
        url: MAPS_URL + maps[map_id],
        format: new ol.format.KML()
    })
});        

map.addLayer(vector);
setMapExtent(vector);   

// call image adding function pass in vector
// to get its extend
addImage(vector);

addImage 函数

function addImage(vectorMap) {

    var vectorSource = vectorMap.getSource();

    // listen for one change on the vector to get the extent of it
    // for use in setting the image extent. tried to use on.('load')
    // but it didnt work
    var listenerKey = vectorSource.once('change', function () {

        var extent = vectorSource.getExtent();

        var projection = new ol.proj.Projection({
            code: 'xkcd-image',
            units: 'pixels',
            extent: extent
        });

        image = new ol.layer.Image({
            source: new ol.source.ImageStatic({
                attributions: [],
                url: MAPS_URL + images[map_id],
                projection: projection,
                imageExtent: extent
            })
        });

        // remove vector layer else they keep stacking up
        map.removeLayer(vector);
        // add image
        map.addLayer(image); 
        // re-add vector only push so it goes above the image
        map.getLayers().push(vector); 

    });
}

似乎工作得很好!任何人都可以帮助我进行图层排序吗?

【问题讨论】:

    标签: javascript vector maps kml openlayers-3


    【解决方案1】:

    您的静态图像必须使用视图的投影进行正确的地理参考。

    默认视图的投影是EPSG:3857 (Spherical Mercator).,这个投影的范围是[-20026376.39, -20048966.10, 20026376.39, 20048966.10]

    在您的代码中,您可以为静态图层指定以像素为单位的投影。您需要使用视图的投影,如下所示:

        // Here the extent of your layer in EPSG:3857  -> [minx, miy, max, mayy]
        var extent = [-10000000, -10000000, 10000000, 10000000];
    
        image = new ol.layer.Image({
            source: new ol.source.ImageStatic({
                attributions: [
                    new ol.Attribution({
                        html: '&copy; <a href="http://xkcd.com/license.html">xkcd</a>'
                    })
                ],
                url: MAPS_URL + images[map_id],
                imageSize: [1024, 684],  // Don't forget the image size here
                imageExtent: extent
            })
        });
    
        map.addLayer(image);
    

    更新:

    如果您希望矢量图层位于顶部,则对于图层排序,请使用 push:

    http://openlayers.org/en/v3.8.2/apidoc/ol.Collection.html#push

    map.getLayers().push(vector)
    

    【讨论】:

    • 我可以使用getExtent 之类的东西来获取矢量图层范围,然后将该范围用作图像范围吗?我真的需要地理参考图像(如在 qgis 或其他东西中)还是在你地理参考图像的方式上方的代码中获得正确的扩展?
    • 我试过 getExtent 但它给了我全方位的“无限”。使用它没有加载图像
    • 可能您有infinity,因为您的数据尚未加载。通常,您需要对图像的每个角落进行地理参考。
    • 谢谢,在您指出这一点后,我能够找到解决方案!您可以在上面看到它,也许您可​​能对作为上述解决方案一部分的层排序问题有建议。谢谢。
    • 谢谢,我也更新了我的解决方案,我必须在推送之前删除矢量图层,这样它们就不会堆积起来
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2015-09-04
    • 1970-01-01
    • 2013-01-28
    相关资源
    最近更新 更多