我现在正在使用Ol3来显示静态图像(如http://openlayers.org/en/latest/examples/static-image.html)。虽然当我放大并开始平移/拖动时,我可以看到如示例中所示的空白区域。我不想看到这个。
是否可以仅平移到图像的末尾,以便不显示空白?
答案 0 :(得分:1)
You can restrict the dragging of the image by restricting its extent.
此处是指向fiddle的内联链接。
map.on(' moveend',function(evt){
console.log(view.getZoom());
if(view.getZoom()<=2){
view.setZoom(3);
}
});
var constrainPan = function() {
console.log("move");
var extents=[0, 0, 1024, 968]
var visible = view.calculateExtent(map.getSize());
var centre = view.getCenter();
var delta;
var adjust = false;
if ((delta = extents[0] - visible[0]) > 0) {
adjust = true;
//console.log(delta);
centre[0] += delta;
} else if ((delta = extents[2] - visible[2]) < 0) {
adjust = true;
centre[0] += delta;
}
if ((delta = extents[1] - visible[1]) > 0) {
adjust = true;
centre[1] += delta;
} else if ((delta = extents[3] - visible[3]) < 0) {
adjust = true;
centre[1] += delta;
}
if (adjust) {
view.setCenter(centre);
}
};
view.on('change:resolution', constrainPan);
view.on('change:center', constrainPan);