是否可以锁定 openlayers 地图的中心?

Is it possible to lock the center of an openlayers map?

我想锁定我的 openlayers 地图的中心。所以在初始化时我将地图居中,但之后我希望地图保持居中但允许缩放。我禁用了 dragPan 交互,但缩放地图会缩放到鼠标的位置而不是地图的中心。是否可以更改此行为?

可以使用零宽度和高度中心范围约束来完成:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/css/ol.css" type="text/css">
    <style>
      html, body, .map {
        margin: 0;
        padding: 0;
        width: 100%;
        height: 100%;
      }
    </style>
    <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.9.0/build/ol.js"></script>
    <title>OpenLayers example</title>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script type="text/javascript">
      var center = ol.proj.fromLonLat([37.41, 8.82]);
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
          center: center,
          zoom: 4,
          extent: center.concat(center),
          constrainOnlyCenter: true,
          smoothExtentConstraint: false
        })
      });
    </script>
  </body>
</html>