简体   繁体   English

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

[英]openlayers 5: move map center by a pixels value

I have an openlayers 5 map generated by: 我有一个通过以下方式生成的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',
    }
);

I want move the center of the map by [10, 10] pixels clicking on an element, and I'm trying with an event like: 我想通过单击元素将地图的中心移动[10、10]像素,并且正在尝试类似的事件:

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

but it doesn't work, it breaks the map. 但它不起作用,它会破坏地图。

What is the correct way to achieve the move? 实现这一目标的正确方法是什么?

As stated in the example documentation Advanced View Positioning it should be done by view.centerOn(coordinate, size, position); 如示例文档“ 高级视图定位”中所述 ,应通过view.centerOn(coordinate, size, position); You can find detailed API in here CenterOn API 您可以在此处找到详细的API CenterOn API

map.view will cause an error, it should be map.getView() You need to multiply pixels by resolution (which is meters per pixel) to convert a move distance from pixels to meters 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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM