如何限制地图的滚动到图层(ImageStatic)的大小?

How to restrain the scroll of the map to the size of the layer (ImageStatic)?

我认为这就是问题所在,我正在寻找一种方法来将地图的可滚动区域固定为其图层的大小。

在我的例子中,我使用了一张宽度为 2722 像素、高度为 3850 像素的静态图像。

我也检查了 "extent" 属性 但我真的无法理解如何让它发挥作用,就我的目的而言。

就是这样,等不及一些线索或其他答案:)

祝你有愉快的一天和代码。

编辑:

var extent = [0, 0, 2722, 3850];
var projection = new ol.proj.Projection({
    code: 'xcd-image',
    units: 'pixels',
    extent: extent
});

var map = new ol.Map({
    interactions: ol.interaction.defaults({mouseWheelZoom: false, doubleClickZoom: false}),
    layers: [
      new ol.layer.Image({
        source: new ol.source.ImageStatic({
          url: 'http://www.tarsis.paris/bundles/tarsisfront/carte.png',
          projection: projection,
          imageExtent: extent
        })
      })
    ],
    controls: [],
    target: 'map',
    view: new ol.View({
      projection: projection,
      center: ol.extent.getCenter(extent),
      zoom: 3
    })
});

OpenLayers 尚不支持您想要实现的功能。请参阅一位 OL 开发人员的 this answer

如上述问题中的评论所示,您可以通过添加 minZoom 参数来限制缩放级别。您必须更改这部分代码:

controls: [],
target: 'map',
view: new ol.View({
  projection: projection,
  center: ol.extent.getCenter(extent),
  zoom: 3
})

对此:

target: 'map',
view: new ol.View({
  projection: projection,
  center: ol.extent.getCenter(extent),
  zoom: 3,
  minZoom: 2
})