为什么缩放级别返回小数?

Mic*_*ael 1 openlayers

我在我的项目中使用 openlayers 6。地图上的图层根据缩放级别隐藏/显示。

这是地图和视图定义:

view = new View({
    center: mapCenter,
    projection: israeliTM.getCode(),
    zoom: 0,
    resolutions: resolutions // resolutions array 
});

map = new Map({
    layers: layers,
    target: 'map',
    view: view
});
Run Code Online (Sandbox Code Playgroud)

当缩放更改时会触发此行:

var curRes = map.getView().getResolution()[map.getView().getZoom()];
Run Code Online (Sandbox Code Playgroud)

正如您在定义为数组的视图定义分辨率中看到的,当缩放更改时,我希望从这个map.getView().getZoom()整数中获取显示分辨率数组中的索引并返回当前分辨率。一般来说,它按预期工作,但有时 map.getView().getZoom()会返回十进制数,例如 3.56989。

知道为什么map.getView().getZoom()返回一些小数时间吗?

Mik*_*ike 5

如果您使用的是 OpenLayers 6,请constrainResolution在视图选项中指定

view = new View({
    center: mapCenter,
    projection: israeliTM.getCode(),
    zoom: 0,
    constrainResolution: true,
    resolutions: resolutions // resolutions array 
});
Run Code Online (Sandbox Code Playgroud)

对于 OpenLayers 5,请constrainResolution在交互选项中指定

map = new Map({
    layers: layers,
    target: 'map',
    interactions: defaultInteractions({
        constrainResolution: true
    }),
    view: view
});
Run Code Online (Sandbox Code Playgroud)