I would like the markers on my leaflet map to bounce when hover over corresponding list-item in sidebar. How to connect markers and list-items. List-items and popups all generated dynamically, their properties all stored in the same geoJSON file.
`
var redFlag = L.icon({
iconUrl: 'images/mapmarker2.png',
iconSize: [34, 34],
iconAnchor: [17,34]
});
const myLayer = L.geoJSON(art, {
pointToLayer: function (feature, latlng) {
for (var i = 0; i < art.features.length; i++) {
var myMarker = L.marker(latlng, {icon: redFlag})
.setBouncingOptions({
bounceHeight : 30,
bounceSpeed : 54,
exclusive : true, })
.on('click', function() {
this.bounce(3);
});
feature.marker = myMarker;
return myMarker;
}
},
onEachFeature: function ( feature, layer) {
layer.on('click', function(e){
const element = document.getElementById('popup');
element.innerHTML =
`<div class= "wrapper" style="background-color:#fff">`+
`<div class="close">X</div>`+
`<div class="popUpContent" style="background-color:#e8f4ff">` +
`<div class='pic'><img src="images/${feature.properties.image}">
</div><p>`+
`<div class="puName">${feature.properties.name}</div>`+
`<div class="puTitle">"${feature.properties.title}"</div><p>` +
`<div class="extra3">${feature.properties.extra}</div>`+
`</div></div>`;
if (!feature.properties.title){
element.innerHTML =
`<div class= "wrapper" style="background-color:#fff">`+
`<div class="close">X</div>`+
`<div class="popUpContent" style="background-color:#e8f4ff">` +
`<div class='pic'><img src="images/${feature.properties.image}"></div>
<p>`+
`<div class="puName">${feature.properties.name}</div><p>`+
`<div class="extra3">${feature.properties.extra}</div>`+
`</div></div>`;
}
if (!feature.properties.extra){
element.innerHTML =
`<div class= "wrapper" style="background-color:#fff">`+
`<div class="close">X</div>`+
`<div class="popUpContent" style="background-color:#e8f4ff">` +
`<div class='pic'><img src="images/${feature.properties.image}">
</div><p>`+
`<div class="puName">${feature.properties.name}</div><p>`+
`<div class="puTitle">"${feature.properties.title}"</div><p>` +
`</div></div>`;
}
document.getElementById('popup').addEventListener( 'click', closePopup);
function closePopup(e){
if (document.querySelector('.wrapper').style.display = 'block'){
document.querySelector('.wrapper').style.display='none';
}
}
});
}
});
mymap.addLayer(myLayer);
function sortJson() {
var name = [];
for( const el of art.features){
name.push(el);
}
return name;
}
var artName = sortJson();
console.log(artName);
myArt = artName.sort((a,b)=> {
var x = a.properties.name.toLowerCase();
var y = b.properties.name.toLowerCase();
return x < y ? -1 : x > y ? 1 : 0;
})
console.log(myArt)
// Add items and build list
var i = 1;
const buildItemList = (data) => {
for ( const el of data) {
var prop = el.properties;
var myMarker = el.marker;
var listings = document.getElementById('listings');
var listing = listings.appendChild(document.createElement('div'));
listing.className = 'item';
listing.id = 'newItem_' + i++;
listing.marker = myMarker;
var content = document.createElement('div');
listing.appendChild(content);
content.className = 'content';
content.innerHTML= `<div class="space">${prop.name}</div>`+
`<span class="info">'Title: "${prop.title}"</span>
<br>` +
`<span class="extra">${prop.extra}<br>Adress:
${prop.adress}</span><br></div>`;
if (!prop.extra){
content.innerHTML = `<div class="space">${prop.name}</div>`+
`<span class="info">'Title: "${prop.title}"</span><br>` +
`<span class="extra" style="color:#fff">Adress: ${prop.adress}
</span></div>`;
}
if (!prop.title){
content.innerHTML = `<div class="space">${prop.name}</div>`+
`<span class="info"></span>` +
`<span class="extra2">${prop.extra}<br>Adress: ${prop.adress}</span>
<div>`;
}
}
}
buildItemList(myArt);