对于我的地图应用程序,我想在不同的函数中使用ol.View.zoom-property以及ol.View.resolution属性。根据Open Layers 4 API文档,如果给出了解决方案,则会忽略zoom属性。 因此,我正在尝试应用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函数是不是已经实现了?或者我试图访问它的方式是否有错误?
答案 0 :(得分:0)
在这里工作得很好。 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));
你有一个不起作用的沙箱吗?