JS
const tileUrl =
"https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png";
const attribution =
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>';
const initialZoom = 13;
const map = L.map("map").setView([0, 0], initialZoom);
const locations = [
{
id: 1,
name: "Espresso House Knalleland",
address: {
street: "Lundbygatan 1",
city: "Borås",
postcode: "506 38",
},
coordinates: {
latitude: "57.73296",
longitude: "12.93726",
},
ammenities: {
changeroom: true,
toys: true,
books: true,
playground: true,
garden: true,
},
},
{
id: 2,
name: "Espresso House Borås Station",
address: {
street: "Stationsgatan 16",
city: "Borås",
postcode: "503 38",
},
coordinates: {
latitude: "57.72057",
longitude: "12.93258",
},
ammenities: {
changeroom: true,
toys: false,
books: true,
playground: true,
garden: false,
},
},
{
id: 3,
name: "Café Viskan",
address: {
street: "Södra Strandgatan 6",
city: "Borås",
postcode: "503 35",
},
coordinates: {
latitude: "57.71984",
longitude: "12.94025",
},
ammenities: {
changeroom: true,
toys: true,
books: true,
playground: false,
garden: true,
},
},
{
id: 4,
name: "Waynes Coffee",
address: {
street: "Drottninggatan 33",
city: "Stockholm",
postcode: "111 51",
},
coordinates: {
latitude: "59.33151",
longitude: "18.06369",
},
ammenities: {
changeroom: true,
toys: false,
books: true,
playground: false,
garden: true,
},
},
{
id: 5,
name: "Caffetteria del corso",
address: {
street: "Drottninggatan 56",
city: "Stockholm",
postcode: "111 21",
},
coordinates: {
latitude: "59.33029",
longitude: "18.06489",
},
ammenities: {
changeroom: true,
toys: true,
books: true,
playground: true,
garden: false,
},
},
{
id: 6,
name: "Viktors Kaffe",
address: {
street: "Geijersgatan 7",
city: "Göteborg",
postcode: "411 34",
},
coordinates: {
latitude: "57.69775",
longitude: "11.97663",
},
ammenities: {
changeroom: true,
toys: true,
books: true,
playground: true,
garden: false,
},
},
{
id: 7,
name: "Café Berlin",
address: {
street: "Vasagatan 46",
city: "Göteborg",
postcode: "411 37",
},
coordinates: {
latitude: "57.69981",
longitude: "11.97188",
},
ammenities: {
changeroom: true,
toys: true,
books: true,
playground: false,
garden: true,
},
},
];
const icon = L.icon({
iconUrl: "/img/baby.png",
iconSize: [30, 30],
iconAnchor: [15, 30],
popupAnchor: [0, -25],
});
function getLocation() {
navigator.geolocation.getCurrentPosition(success, error);
}
function success(pos) {
const crd = pos.coords;
map.setView([crd.latitude, crd.longitude], initialZoom);
}
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
const getAmmenities = (item) => {
const list = [];
for (const [key, value] of Object.entries(item.ammenities)) {
if (value === true) {
list.push(key);
}
}
const letList = list
.map(
(item) =>
`<img class="ammenities-icons" src=img/${item}.png alt=${item} />`
)
.join("");
document.addEventListener("DOMContentLoaded", getIcons);
let content = document.querySelectorAll("img.ammenities-icons");
content.forEach((el) => {
el.addEventListener("click", () => {
if (el.target.className === "ammenities-icons") {
console.log("clicked");
}
});
});
return letList;
};
function getCaffees() {
locations.forEach((item) => {
L.marker(
[Number(item.coordinates.latitude), Number(item.coordinates.longitude)],
{ icon }
)
.bindPopup(
`
<h3>${item.name}</h3>
<h5 class="street-address">${item.address.street}</h5>
<h5 class="other-address">${item.address.postcode} ${
item.address.city
}</h5>
${getAmmenities(item)}
`
)
.openPopup()
.addTo(map);
});
}
getLocation();
getCaffees();
L.tileLayer(tileUrl, {
attribution,
maxZoom: 20,
}).addTo(map);
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- leaflet css -->
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css"
integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ=="
crossorigin=""
/>
<!-- leaflet javascript -->
<script
src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js"
integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ=="
crossorigin=""
></script>
<!-- custom css -->
<link rel="stylesheet" href="style.css" />
<title>Little Sippers</title>
</head>
<body>
<div id="map"></div>
<script src="leaflet.js"></script>
</body>
</html>
Can share repository if you want…