如何在 MapBox 地图上的动画 canvas 点上获取点击处理程序?

How to get click handler on animated canvas dot on MapBox map?

我正在从 MapBox 查看 this demo

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Add an animated icon to the map</title>
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <link href="https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.css" rel="stylesheet">
  <script src="https://api.mapbox.com/mapbox-gl-js/v2.3.1/mapbox-gl.js"></script>
  <style>
    body { margin: 0; padding: 0; }
    #map { position: absolute; top: 0; bottom: 0; width: 100%; }
  </style>
</head>

<body>
  <div id="map"></div>
  <script>
    mapboxgl.accessToken = 'pk.thekey.12345';
    var map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/mapbox/streets-v9'
    });
    var size = 200;
    // This implements `StyleImageInterface`
    // to draw a pulsing dot icon on the map.
    var pulsingDot = {
      width: size,
      height: size,
      data: new Uint8Array(size * size * 4),
      // When the layer is added to the map,
      // get the rendering context for the map canvas.
      onAdd: function() {
        var canvas = document.createElement('canvas');
        canvas.width = this.width;
        canvas.height = this.height;
        this.context = canvas.getContext('2d');
      },
      // Call once before every frame where the icon will be used.
      render: function() {
        var duration = 1000;
        var t = (performance.now() % duration) / duration;
        var radius = (size / 2) * 0.3;
        var outerRadius = (size / 2) * 0.7 * t + radius;
        var context = this.context;
        // Draw the outer circle.
        context.clearRect(0, 0, this.width, this.height);
        context.beginPath();
        context.arc(
          this.width / 2,
          this.height / 2,
          outerRadius,
          0,
          Math.PI * 2
        );
        context.fillStyle = 'rgba(255, 200, 200,' + (1 - t) + ')';
        context.fill();
        // Draw the inner circle.
        context.beginPath();
        context.arc(
          this.width / 2,
          this.height / 2,
          radius,
          0,
          Math.PI * 2
        );
        context.fillStyle = 'rgba(255, 100, 100, 1)';
        context.strokeStyle = 'white';
        context.lineWidth = 2 + 4 * (1 - t);
        context.fill();
        context.stroke();
        // Update this image's data with data from the canvas.
        this.data = context.getImageData(
          0,
          0,
          this.width,
          this.height
        ).data;
        // Continuously repaint the map, resulting
        // in the smooth animation of the dot.
        map.triggerRepaint();
        // Return `true` to let the map know that the image was updated.
        return true;
      }
    };
    map.on('load', function() {
      map.addImage('pulsing-dot', pulsingDot, {
        pixelRatio: 2
      });
      map.addSource('dot-point', {
        'type': 'geojson',
        'data': {
          'type': 'FeatureCollection',
          'features': [{
            'type': 'Feature',
            'geometry': {
              'type': 'Point',
              'coordinates': [0, 0] // icon position [lng, lat]
            }
          }]
        }
      });
      map.addLayer({
        'id': 'layer-with-pulsing-dot',
        'type': 'symbol',
        'source': 'dot-point',
        'layout': {
          'icon-image': 'pulsing-dot'
        }
      });
    });
  </script>
</body>

</html>

如何制作它以便我可以处理点上的点击,并在将光标悬停在它上面时使光标成为指针?

事情其实并没有看起来那么难。正如其他人所建议的那样,您可以通过一些技巧来做到这一点 - 但不需要它,因为所需的功能已内置到 Mapbox 本身中! 如果您在 API reference 中查找它自己的 map 对象,您会注意到有几个有趣的事件:

clickmouseentermouseleave

通常这些绑定到实际地图并添加如下:

map.on('click', function() {
console.log('clicked');
});

现在您可能会问自己,什么对您有好处。如果我们回顾一下您提供的示例代码,我们可以看到动画点已添加到地图上它自己的图层 layer-with-pulsing-dot。 API 参考文献没有真正提到的一件事是,.on() 方法还有一个额外的第二个参数,让您可以为特定层指定一个 ID。

所以我们要做的就是向 layer-with-pulsing-dot 层添加一些监听器。

点击事件当然很明显,mouseentermouseleave 事件用于将鼠标光标转换为指针并返回。

尝试在 map.addLayer({ ... });

之后添加此代码块
  map.on('click', 'layer-with-pulsing-dot', function(e) {
    alert('Someone clicked long:' + e.lngLat.lng + ' lat:' + e.lngLat.lat)
  });

  map.on('mouseenter', 'layer-with-pulsing-dot', function() {
    map.getCanvas().style.cursor = 'pointer';
  });

  map.on('mouseleave', 'layer-with-pulsing-dot', function() {
    map.getCanvas().style.cursor = '';
  });

您的 canvas 中有圆点的坐标。 在鼠标移动事件中,检查鼠标坐标是否在点的坐标中。