openlayers 5:将地图中心移动一个像素值

openlayers 5: move map center by a pixels value

我有一张 openlayers 5 地图,由:

// create the map with the proper center
var map = new ol.Map(
    {
        view: new ol.View(
            {
                center: ol.proj.fromLonLat([center.long, center.lat]),
                zoom: zoom
            }
        ),
        layers: [
            new ol.layer.Tile(
                {
                    source: new ol.source.OSM()
                }
            )
        ],
        target: 'mapdiv',
    }
);

我想通过点击一个元素将地图的中心移动 [10, 10] 像素,我正在尝试这样的事件:

document.getElementById('mover').onclick = function() {
  center = map.getView().getCenter();
  map.view.setCenter(center[0] + 10, center[1] + 10);
  return false;
};

但是不管用,它破坏了地图。

实现移动的正确方法是什么?

如示例文档中所述Advanced View Positioning it should be done by view.centerOn(coordinate, size, position); You can find detailed API in here CenterOn API

map.view会报错,应该是map.getView()你需要用像素乘以分辨率(即米/像素)来将移动距离从像素转换为米

var center = map.getView().getCenter();
var resolution = map.getView().getResolution();
map.getView().setCenter([center[0] + 10*resolution, center[1] + 10*resolution]);