I am new to Leaflet, HTML, CSS and Java. I am using leaflets maps and I want to create a sidebar that is active when a marker is clicked or active when a clicked in the sidebar, so far I have had no luck at all. I have tried to do similar to this but I want defined markers lat and long http://jsfiddle.net/BPuQ9/8/ but have had no luck
I have a JSfiddle file here: https://jsfiddle.net/Niall2110/yphxqzu2/1/
<!DOCTYPE html>
<html>
<head>
<title>Quick Start - Leaflet</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js" integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og==" crossorigin=""></script>
<style>
html, body, #map {
width:100%;
height:100%;
margin:0;
margin-left:10%;
padding:0;
}
#overlay {
position:absolute;
width:30%;
height:100%;
left:0;
top:0;
background-color:rgba(255, 255, 255, 0.8);
}
</style>
</head>
<body>
<div id = "map"></div>
<div id="overlay"></div>
<script>
var map = L.map('map', {
center: [48,14],
zoom: 7,
animate: true, duration: 1
});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a
href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(map);
L.marker([49, 14]).addTo(map)
.bindPopup("<b><font size='6'>Headline</font></b><br />info<br //> <img src=''> <a href= '' target='_blank'><button>Source</button></a> ").openPopup();
$('#overlay').append(
'<div class="item" id="' + marker._leaflet_id + '">marker ' + marker._leaflet_id + ' - <a href="#" class="remove" id="' + marker._leaflet_id + '">remove</a></div>');
</script>
</body>
</html>
Best Answer
Solution below is not complete solution with all the required bells and whistles (specially CSS styles are lame), it's just a first step in the right direction.
There is simple function
createMarker
for creating markers with all the desired properties (position, title, text, image, source link). Function creates marker inmarkerLayer
with the following properties:Remove
element which upon click removes the whole element from the sidebar.Working JSFiddle is available at https://jsfiddle.net/TomazicM/ox43cbth/5/
The code: