在OpenLayers3中限制WMS范围外的Pan [英] Restrict Pan outside WMS extent in OpenLayers3

查看:135
本文介绍了在OpenLayers3中限制WMS范围外的Pan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个面积较小的矩形WMS,并且希望限制WMS延伸范围之外的平移,因此地图外部根本看不到白色或黑色区域. 将extent添加到View对我不起作用,并且在有关此选项的文档中已撰写

I have rectangle WMS of small area and want to restrict panning outside WMS extends, so there aren't white or black area outside the map visible at all. Adding extent to View does not work for me and in documentation about this option is written

约束中心的范围,换句话说,中心不能 设置在此范围之外.

The extent that constrains the center, in other words, center cannot be set outside this extent.

但是据我了解,如果中心位于范围的区域中,但在最角落,它将在此范围之外显示白色区域,但是我根本不想看到白色区域.

But as I understand this if center is in the area of extent, but on the very corner, it will show white area outside this extent, but I don't want to see white area at all.

是否可以使用OL3做到这一点?

Is it possible to achieve this with OL3?

推荐答案

这是我的解决方案.我刚刚写了它,因此没有经过广泛测试.例如,如果您开始旋转地图,它可能会损坏,而如果缩小得太远,它可能会出现故障.

Here's my solution. I wrote it just now, and so it is not extensively tested. It would probably break if you start rotating the map, for example, and it may be glitchy if you zoom out too far.

var constrainPan = function() {
    var visible = view.calculateExtent(map.getSize());
    var centre = view.getCenter();
    var delta;
    var adjust = false;
    if ((delta = extent[0] - visible[0]) > 0) {
        adjust = true;
        centre[0] += delta;
    } else if ((delta = extent[2] - visible[2]) < 0) {
        adjust = true;
        centre[0] += delta;
    }
    if ((delta = extent[1] - visible[1]) > 0) {
        adjust = true;
        centre[1] += delta;
    } else if ((delta = extent[3] - visible[3]) < 0) {
        adjust = true;
        centre[1] += delta;
    }
    if (adjust) {
        view.setCenter(centre);
    }
};
view.on('change:resolution', constrainPan);
view.on('change:center', constrainPan);

这希望变量mapview(具有明显的含义)和extent(您希望显示的xmin,ymin,xmax,ymax)可用.

This expects the variables map, view (with obvious meanings) and extent (the xmin, ymin, xmax, ymax you want to be visible) to be available.

这篇关于在OpenLayers3中限制WMS范围外的Pan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆