教程中事件监听器的代码错误

我正在遵循this tutorial来构建带有Mapbox地图的商店定位器页面。

我不想添加自定义标记,因为我已经有自定义地图标签(符号?),这意味着我不需要教程的最后一部分,就可以在Add Event Listeners之后停下来。

完成此操作后,页面应对侧面板列表以及地图(2个事件侦听器)上的单击做出反应。但是,在该特定步骤的demo provided in the tutorial中,您可以告诉第二个事件侦听器的代码无法运行,该事件使映射可单击,从而使我相信所提供的代码有误:

// Add an event listener for when a user clicks on the map
map.on('click',function(e) {
  // Query all the rendered points in the view
  var features = map.queryRenderedFeatures(e.point,{ layers: ['locations'] });
  if (features.length) {
    var clickedPoint = features[0];
    // 1. Fly to the point
    flyToStore(clickedPoint);
    // 2. Close all other popups and display popup for clicked store
    createPopUp(clickedPoint);
    // 3. Highlight listing in sidebar (and remove highlight for all other listings)
    var activeItem = document.getElementsByClassname('active');
    if (activeItem[0]) {
      activeItem[0].classList.remove('active');
    }
    // Find the index of the store.features that corresponds to the clickedPoint that fired the event listener
    var selectedFeature = clickedPoint.properties.address;

    for (var i = 0; i < stores.features.length; i++) {
      if (stores.features[i].properties.address === selectedFeature) {
        selectedFeatureIndex = i;
      }
    }
    // Select the correct list item using the found index and add the active class
    var listing = document.getElementById('listing-' + selectedFeatureIndex);
    listing.classList.add('active');
  }
});

任何人都可以说出这段代码有什么问题吗?

ww19983ww 回答:教程中事件监听器的代码错误

结果是代码不完整,因为当您将鼠标悬停在地图标签/标记上时,光标不会变为指针,因此它不会提示您意识到可以单击它,因此我认为它不是“根本没有工作。我认为,除非指针出现,否则将面对地图的一般用户都会受到同样的欺骗。因此,在本教程中,如果确实要继续操作并单击该标记,尽管没有显示任何指针,它将具有预期的行为并显示弹出窗口。

以下是基于小提琴创建指针的方法:https://jsfiddle.net/Twalsh88/5j70wm8n/25/

map.on('mouseenter','locations',function(e) {
   // Change the cursor style as a UI indicator.
   map.getCanvas().style.cursor = 'pointer';

 });
 map.on('mouseleave',function() {
   map.getCanvas().style.cursor = '';
 });
本文链接:https://www.f2er.com/3128814.html

大家都在问