View
Quick answer
View is ol.View, the object holding center, zoom or resolution, rotation and projection that defines what the map shows. Configure center in view projection coordinates, zoom, minZoom and maxZoom, extent, padding, constrainResolution and enableRotation. Move the map with setCenter, setZoom, fit or animate instead of recreating the view.
new ol.View( options )
Key points
- zoom and resolution express the same scale; resolution is projection units per pixel.
- getCenter returns projected coordinates, so convert with ol.proj.toLonLat before storing.
- animate accepts multiple segments, each with its own duration and easing.
Example
var view = new ol.View({
center: ol.proj.fromLonLat([116.391263, 39.907359]),
zoom: 12,
minZoom: 4,
maxZoom: 18
});
/* 缩放到一个矩形范围,留 40 像素边距并带过渡 */
view.fit([116.30, 39.85, 116.50, 39.98], {
padding: [40, 40, 40, 40],
duration: 600
});var resolution = view.getResolution();
var metersPerPixel = resolution; // EPSG:3857 下单位就是米
/* 反过来设一个固定分辨率,相当于锁定显示比例 */
view.setResolution(10);
Notes
- Treating getCenter or calculateExtent output as longitude and latitude causes offset data.
- fit expects an extent in view projection, so passing raw lon/lat bounds zooms to the wrong place.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). View. https://www.jingweizhi.com/en/openlayers/api/view.html (accessed 2026-11-03).