【问题标题】:Is getZoomForResolution defined in Open Layers 4? And if so, how do I get it working?getZoomForResolution 是否在 Open Layers 4 中定义?如果是这样,我该如何让它工作?
【发布时间】:2018-03-30 03:17:07
【问题描述】:

对于我的地图应用程序,我想在不同的功能中使用 ol.View.zoom-property 和 ol.View.resolution 属性。根据 Open Layers 4 API 文档,如果给出分辨率,则忽略缩放属性。 因此,我正在尝试应用 getZoomForResolution,它记录在此处的 API 中:

https://openlayers.org/en/latest/apidoc/ol.View.html#getZoomForResolution

这是我使用的一些测试代码:

var map, view;
    //initial definition of the map
    function initializeMap(){
        //define osm source and layer
        var osmSource = new ol.source.OSM();
        var osmLayer = new ol.layer.Tile({source: osmSource});
        //define new map object
        map = new ol.Map({
            target:'map',
            view: new ol.View({
                center: ol.proj.fromLonLat([9.83276, 52.36554]),
                resolution:300, // is required in some function
                zoom:10 //ignored as expected according to API, also required in another function
            }),
            layers:[osmLayer]
        });
        var currentResolution = map.getView().getResolution(); // returns 300 as expected
        console.log(map.getView().getZoomForResolution(currentResolution));
    }   

不幸的是,我只收到错误“map.getView().getZoomForResolution 不是函数”。我也看过this的帖子,但它指的是OL 3,没有给出证据。

所以,我的问题是:getZoomForResolution 功能不再实现了吗?还是我尝试访问它的方式有误?

【问题讨论】:

    标签: openlayers


    【解决方案1】:

    在这里工作得很好。 https://codepen.io/anon/pen/vePjxa?editors=1011

     var osmSource = new ol.source.OSM();
        var osmLayer = new ol.layer.Tile({source: osmSource});
        //define new map object
        map = new ol.Map({
            target:'map',
            view: new ol.View({
                center: ol.proj.fromLonLat([9.83276, 52.36554]),
                resolution:300, // is required in some function
                zoom:10 //ignored as expected according to API, also required in another function
            }),
            layers:[osmLayer]
        });
        var currentResolution = map.getView().getResolution(); // returns 300 as expected
        console.log(map.getView().getZoomForResolution(currentResolution));
    

    您是否有一个沙盒无法正常工作?

    【讨论】:

    • 啊,应该早点尝试过 - 在不同的环境中工作。谢谢!
    猜你喜欢
    • 2021-03-13
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 2010-11-13
    • 2020-12-19
    相关资源
    最近更新 更多