在 OpenLayers 中以 100% 显示静态图像

Displaying Static Images at 100% in OpenLayers

我想使用 ol3 中的 StaticImage 图层将静态图像显示为地图,以 100% 的图像大小(以像素为单位)显示。我相信这应该由范围和缩放变量定义,但显示的图像并不总是正确的大小,具体取决于图像,所以我显然误解了一些东西,尽管我已经阅读了所有论坛条目。

这里 fiddle 展示了不正确的图像尺寸。参考图为128x128像素,但显示的图片在缩放0时略大。

如果在我的 fiddle 中使用,provided example from OpenLayers 工作得很好,我们似乎了解到,如果范围变量是 [0,0,wid,len],那么 zoom: 2 是100%。但这并不适用于所有图像。

静态图像如何以 100% 的像素大小可靠地显示?

在此先感谢您的帮助。

var extent = [0,0,128,128]  // image size is 128x128 px
var projection = new ol.proj.Projection({
    code: 'local_image',
    units: 'pixels',
    extent: extent
});

var map = new ol.Map({
    target: 'map',
    view: new ol.View({
        projection: projection,
        center: ol.extent.getCenter(extent),
        zoom: 0,
    }),
    controls: [],
});

var im_layer = new ol.layer.Image({
    source: new ol.source.ImageStatic({
        url: 'http://img4.wikia.nocookie.net/__cb20071014061100/freeciv/images/1/1c/Crystal_128_penguin.png',  // image size is 128x128 px
        projection: projection,
        imageExtent: extent
    })
})
map.addLayer(im_layer)

我终于找到了这个问题的答案。不要在地图视图中使用 "zoom" 变量,而是使用 "resolution" 参数。

除上述设置外,还设置以下设置可使图像以 100% 显示:

var map = new ol.Map({
    view: new ol.View({
        resolution: 1,        // important for 100% image size!
        maxResolution: 2,     // must be >= 1
        //minResolution: .5,  // also set-able
        //zoom: 0,            // don't use this
        ...
    })
    ...
})

这是一个updated fiddle.