如何在 OpenLayers 6.4.2 中禁用地图或绘制对象上的点击事件?

How to disable click event on map or draw object in OpenLayers 6.4.2?

我试图以编程方式禁用通过绘制对象或单击按钮在地图上放置一个点(线或多边形的点)的能力。 查看 OpenLayers 6 draw object 的文档,可以通过添加以下选项来执行此操作:

stopClick boolean (defaults to false) : Stop click, singleclick, and doubleclick events from firing during drawing. to the draw object constructor which is also present in 6.4.2.

但在实施时它没有按预期工作。

我还在 GitHub 上搜索了这是否是一个错误或可能打开的错误,但我找不到针对这个特定版本的 Openlayers 的任何内容,even tho a bug similar was fixed in version 6.5.0 this bug was not present in Openlayers 6.4.2

我还尝试在 Whosebug 或 GitHub 中搜索无数 QA 问题,但它们要么针对旧版本,要么根本不起作用。 我的代码如下所示:

package.json

{
  "name": "min-zoom",
  "dependencies": {
    "ol": "6.4.2"
  },
  "devDependencies": {
    "parcel": "^2.0.0-beta.1"
  },
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --public-url . index.html"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>View Min-Zoom</title>
    <script src="https://unpkg.com/elm-pep"></script>
    <style>
      .map {
        width: 100%;
        height: 400px;
      }
    </style>
  </head>
  <body>
    <div id="map" class="map"></div>
    <button id="Off">Draw off</button>
    <script src="main.js"></script>
  </body>
</html>

main.js

import "ol/ol.css";
import Map from "ol/Map";
import View from "ol/View";
import "ol/ol.css";
import { Draw } from "ol/interaction";
import { OSM, Vector as VectorSource } from "ol/source";
import { Tile as TileLayer, Vector as VectorLayer } from "ol/layer";

var raster = new TileLayer({
  source: new OSM()
});

var source = new VectorSource({ wrapX: false });

var vector = new VectorLayer({
  source: source
});

var view = new View({
  center: [0, 0],
  zoom: [0]
});

var map = new Map({
  layers: [raster, vector],
  target: "map",
  view: view
});

var draw = new Draw({
  source: source,
  type: "Polygon",
  stopClick: true // does not work
});

map.addInteraction(draw);

map.removeEventListener("click");
draw.removeEventListener("click");

map.removeEventListener("singleclick");
draw.removeEventListener("singleclick");

map.un("click", () => {});
draw.un("click", () => {});

map.un("singleclick", () => {});
draw.un("singleclick", () => {});

map.on("click", (event) => {
  event.preventDefault();
  event.stopPropagation();
});

draw.on("click", (event) => {
  event.preventDefault();
  event.stopPropagation();
});
var singleclick_key = map.on("singleclick", function (evt) {
  console.log("not here");
});
// map.unByKey(singleclick_key); // this does not work with openlayer 6

也可以在这里找到: https://codesandbox.io/s/min-zoom-forked-ix2i6?file=/main.js

除了我已经尝试过的方法之外还有其他方法吗,或者确实是 OpenLayers 中的一个错误?

事实证明:

stopClick is not intended to prevent drawing, it prevents a click event handled by the draw being propagated to the map and firing any listeners you have set on the map https://codesandbox.io/s/min-zoom-forked-dz3h8

感谢这里的原始答案: https://github.com/openlayers/openlayers/issues/12232