我正在遵循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');
}
});
任何人都可以说出这段代码有什么问题吗?