【问题标题】:How to restrict OpenLayers 5 map during mouse drag如何在鼠标拖动期间限制 OpenLayers 5 地图
【发布时间】:2019-05-18 22:56:45
【问题描述】:

在openlayers.js(当前版本5.3.0)中如何在鼠标拖动时限制地图? lat 的范围应为 [-90,90]。

与下面的两个例子相比,

https://openlayers.org/en/latest/examples/drag-rotate-and-zoom.html?q=drag

https://docs.mapbox.com/mapbox-gl-js/example/add-image-animated/

如果将地图拖过lat[-90,90],mapbox中的地图会自动限制范围,OpenLayers中的地图会显示空白区域。

这是同样的问题,但没有完美的答案。 https://gis.stackexchange.com/questions/222698/openlayers3-how-to-limits-the-drag-range-of-the-map

我尝试过的肮脏解决方案是 1. 添加 map.on('moveend'...) 监听器 2.用缩放检测地图中心 3. 如果超出范围,执行 map.getView().animate({ center:[...],duration:100})

但是动画的过程,你知道的,就是从某个空白区域到指定中心的过渡。

Map 构造函数中是否有任何选项? 或者在 OpenLayers 中可以做一些 hack 代码的源代码的任何位置?

【问题讨论】:

    标签: openlayers


    【解决方案1】:

    Limit Panning OpenLayers 5

    这是具有全局范围的相同演示:

    var view = new ol.View();
    
    var map = new ol.Map({
        layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ],
        target: 'map'
    });
    
    function openMap() {
    
        map.setView(view);
        var extent = ol.proj.transformExtent([ -540, -85, 540, 85], 'EPSG:4326', 'EPSG:3857');
    
        // fit the extent horizontally or vertically depending on screen size
        // to determine the maximum resolution possible
        var size = map.getSize();
        var width = ol.extent.getWidth(extent);
        var height = ol.extent.getHeight(extent);
        if (size[0]/size[1] > width/height) {
            view.fit([extent[0],(extent[1]+extent[3])/2,extent[2],(extent[1]+extent[3])/2], { constrainResolution: false });
        } else {
            view.fit([(extent[0]+extent[2])/2,extent[1],(extent[0]+extent[2])/2,extent[3]], { constrainResolution: false });
        }
        var maxResolution = view.getResolution();
        var resolution = 0;
    
        function resChange() {
            var oldView = map.getView();
            if (resolution == 0 || oldView.getZoom()%1 == 0) {
                resolution = oldView.getResolution();
                var width = map.getSize()[0] * resolution;
                var height = map.getSize()[1] * resolution;
                var newView = new ol.View({
                    projection: oldView.getProjection(),
                    extent: [extent[0]+(width/2), extent[1]+(height/2), extent[2]-(width/2), extent[3]-(height/2)],
                    resolution: resolution,
                    maxResolution: maxResolution,
                    rotation: oldView.getRotation() 
                });
                newView.setCenter(newView.constrainCenter(oldView.getCenter()));
                newView.on('change:resolution', resChange);
                map.setView(newView);
            }
        }
        resChange();
    
    }
    map.on('change:size', openMap);
    openMap();
    <link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
    <script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <div id="map" class="map"></div>

    如果您可以等到 OpenLayers 6 发布,或者不介意使用 beta 版本,extent 选项将限制可见范围,除非指定 multiWorld 选项,否则范围默认为一个世界https://github.com/openlayers/openlayers/blob/master/changelog/upgrade-notes.md

    【讨论】:

    • 谢谢。关于'change:size',如果用户进行了zoomToExtent等交互,代码sn -p中的'extent'是否应该跟随用户操作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    相关资源
    最近更新 更多