【发布时间】:2019-12-13 04:10:10
【问题描述】:
我正在关注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');
}
});
谁能说出这段代码有什么问题?
【问题讨论】:
-
这里有人用 Mapbox 吗?
-
我实际上不明白你为什么说它不起作用。您可能会自定义代码的某些部分,所以会有一些不匹配的代码?
标签: listener mapbox mapbox-gl-js