【问题标题】:Map Size not updating on map.udpateSize()地图大小未在 map.udpateSize() 上更新
【发布时间】:2021-02-20 05:47:23
【问题描述】:

我正在使用开放图层发布地图。但我无法弄清楚根据屏幕大小使地图大小动态化的方法。因为无论屏幕大小如何,每个屏幕上的地图都保持不变。我也尝试了 map.updatesize()。但这也不起作用。 我的代码是

        layer = new ol.layer.Tile({
        title: 'Basemap',
        baseLayer: true,
        visible: true,
        
        

        source: new ol.source.TileWMS({
            url: 'http://mlinfomaps.in/geoserver/wms',
            params: { 'LAYERS': 'MSSDS_WS:MSSDS_BASEMAP', 'TILED': true },
            serverType: 'geoserver',
          
            // Countries have transparency, so do not fade tiles:
            transition: 0,
            crossOrigin: "anonymous"
        })
    });


    var mousePositionControl = new ol.control.MousePosition({
        coordinateFormat: ol.coordinate.createStringXY(4),
        projection: 'EPSG:4326',
        // comment the following two lines to have the mouse position
        // be placed within the map.
        //className: 'custom-mouse-position',
        target: document.getElementById('mouse-position'),
        //undefinedHTML: ' '
    });


    var projection = new ol.proj.Projection({
        //code: 'EPSG: 4326', // *code says: 5261 ...try to see if this is right
        //extent: [71.6142, 22.3645, 81.8754, 15.1508],
        extent:[7970828.0670139585, 1681517.1260521673, 9127778.927138386, 2541280.8202038296]
    })
    function scaleControl() {
        control = new ol.control.ScaleLine({
            units: 'metric',
            bar: true,
            steps: 4,
            text: true,
            
            minWidth: 100
        });
        return control;
    }

    var sourceMeasure = new ol.source.Vector({
        crossOrigin: "anonymous",

        
    });
    var vectorMeasure = new ol.layer.Vector({
       
        source: sourceMeasure,
        style: new ol.style.Style({
            fill: new ol.style.Fill({
                color: 'rgba(255, 255, 255, 0.2)'
            }),
            stroke: new ol.style.Stroke({
                color: '#df1c29',
                width: 2
            }),
            image: new ol.style.Circle({
                radius: 6,
                fill: new ol.style.Fill({
                    color: '#df1c29'
                })
            })
        }),
        title: 'Measure Overlay',
    });

    // The Map
    //var overlay = new ol.Overlay
    //    ({
    //        element: container,
    //        autoPan: true,
    //        autoPanAnimation: {
    //            duration: 250
    //        }
    //    });


    var view = new ol.View
        ({
            center: ol.proj.transform([76.7997, 18.6298], 'EPSG:4326','EPSG:3857'),
            zoom: 7,
            extent: projection.getExtent(),
            //minZoom:7,
            maxZoom: 12

        });

    var map = new ol.Map
        ({
            target: 'map',
            controls: ol.control.defaults().extend([mousePositionControl, scaleControl()]),
            view: view,   
            //fit: view.fit(),
            //overlays: [overlay],
            layers: [layer]
            
        });
    const extent = projection.getExtent()
    map.getView().fit(extent);
    map.updateSize();


    window.onresize = function()
        {
    setTimeout( function() { map.updateSize();}, 200);
        }

    '''

不同屏幕上的地图如下所示: 在我的屏幕上看起来像 https://i.stack.imgur.com/KIYio.png

在我同事的屏幕上看起来像 https://i.stack.imgur.com/6c4zk.png

请帮我解决这个问题。

提前致谢!

【问题讨论】:

  • 您希望将同一区域放入任何可用的屏幕空间,而不是在两个屏幕上使用相同的1 : 4,138,966 比例?如果是这样,您应该在创建地图后调用 view.fit() openlayers.org/en/latest/apidoc/module-ol_View-View.html#fit 并提供该区域的范围。
  • 对不起迈克,但我不明白你想说什么。我做了一个有效范围的投影并调用 view.fit() ,但没有成功!
  • 请大家帮忙!

标签: javascript html asp.net openlayers


【解决方案1】:

你好,我有同样的问题,我通过这样做来解决它

ts:

html:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多