因此,我一直在尝试让我的应用更易于访问。其中一项功能位于选项卡(焦点)上,以显示弹出窗口。我终于让它“工作”起来了,因为重点是显示了一个弹出窗口。但是,出于某种原因,它会为每个单独的标记显示一个弹出窗口。
// Create popups and call createMarker to create the marker with the associated popup
initializeMarkers = (venues) => {
const allMarkers = venues.map(venue => {
popup = new mapboxgl.Popup({ offset: 25 })
venueId = venue.name.replace(/\s+/g, '');
latLng = [venue.location.lng, venue.location.lat];
popup.setHTML(
`<div tabindex="0"> <p class="popup-text">${venue.name}</p>
<p class="popup-text">${venue.location.formattedAddress[0]}</p>
<p class="popup-text">${venue.location.formattedAddress[1]}</p></div>`
);
this.createMarker(latLng, popup, venueId)
})
}
// Create Mapbox markers and add correct event listeners
createMarker = (latLng, popup, venueId) => {
marker = new mapboxgl.Marker({color: '#40798C'})
.setLngLat(latLng)
.setPopup(popup)
.addTo(map)
marker.getElement().classList.add(`${venueId}`)
marker.getElement().data = venueId;
marker.getElement().addEventListener('focus', () => {
marker.togglePopup()
})
marker.getElement().addEventListener('click', this.animateMarker)
marker.getElement().setAttribute('tabindex', 0)
}
// Animate the markers on click
animateMarker = (event) => {
if(selectedMarker[0] !== event.currentTarget) {
selectedMarker.push(event.currentTarget)
selectedMarker[0].classList.remove("animateMarker")
selectedMarker[0].classList.remove("changeColor")
selectedMarker.splice(0, 1, event.currentTarget)
event.currentTarget.classList.toggle("animateMarker")
event.currentTarget.classList.toggle("changeColor")
// event.stopPropagation()
} else {
event.currentTarget.classList.toggle("animateMarker")
event.currentTarget.classList.toggle("changeColor")
}
}
我了解到,很可能没有采用特定标记的实例/弹出窗口,但是我不确定为什么/如何修复。任何帮助都非常感谢!
而且,对于整个上下文,这是github:(不幸的是,如果我需要一个非常低的api限制,请谨慎使用)