JSFiddle - React, Tailwind, and code Playground
by tomik23
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>multi-layer serch</title>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tomik23/[email protected]/dist/css/autocomplete.min.css">
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://cdn.jsdelivr.net/gh/tomik23/[email protected]/dist/js/autocomplete.min.js"></script>
</head>
<body>
<!-- search container -->
<div class="container">
<div class="row">
<h4>Find restaurant, bar or pharmacy</h4>
<div class="auto-search-wrapper loupe max-height">
<input type="text" autocomplete="off" id="multi-layer-serch" placeholder="enter the letter">
</div>
</div>
</div>
<!-- map -->
<div id="map"></div>
<script src="script.js"></script>
</body>
</html>
CSS
*,
:after,
:before {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html {
height: 100%;
}
body,
html,
#map {
width: 100%;
height: 100%;
}
body {
position: relative;
display: flex;
justify-content: center;
flex-direction: column;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial,
sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
min-height: 100%;
margin: 0;
padding: 0;
background-color: #f1f1f1;
}
.container {
display: flex;
padding: 5px;
}
.container {
width: 100%;
max-width: 400px;
margin: 20px auto;
}
.row {
width: 100%;
}
.row h4 {
margin-bottom: 10px;
}
.leaflet-marker-icon {
display: flex !important;
justify-content: center;
align-items: center;
line-height: 1;
color: #fff;
font-size: 11px;
box-shadow: 0px 0px 10px rgba(0, 132, 255, 1);
border-radius: 100%;
border: 1px solid #fff;
padding: 10px;
}
.search-tip b {
color: #fff;
}
.bar,
.bar.search-tip b,
.bar.leaflet-marker-icon {
background: #be4dff;
}
.pharmacy,
.pharmacy.search-tip b,
.pharmacy.leaflet-marker-icon {
background: #ff8146;
}
.restaurant,
.restaurant.search-tip b,
.restaurant.leaflet-marker-icon {
background: #ff3939;
}
.search-tip {
white-space: nowrap;
}
.search-tip b {
display: inline-block;
padding: 0 4px;
margin-left: 4px;
}
.place {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border-bottom: 1px solid #eee;
}
.place-item {
color: #fff;
padding: 5px;
font-size: 70%;
border-radius: 3px;
text-transform: uppercase;
}
.legend {
background: #fff;
padding: 8px;
border-radius: 5px;
}
.legend .row {
display: flex;
line-height: 30px;
}
.legend i {
display: block;
width: 30px;
height: 30px;
margin-right: 5px;
}
JavaScript
/* eslint-disable no-undef */
/**
* multi-layer serch
*/
// config map
let config = {
minZoom: 7,
maxZoom: 18,
};
// magnification with which the map will start
const zoom = 18;
// co-ordinates
const lat = 52.22977;
const lng = 21.01178;
// calling map
const map = L.map("map", config).setView([lat, lng], zoom);
// Used to load and display tile layers on the map
// Most tile servers require attribution, which you can set under `Layer`
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map);
// --------------------------------------------------
// async function to load geojson
async function fetchData(url) {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (err) {
console.error(err);
}
}
// center map on the clicked marker
function clickZoom(e) {
map.setView(e.target.getLatLng(), zoom);
}
let geojsonOpts = {
pointToLayer: function(feature, latlng) {
return L.marker(latlng, {
icon: L.divIcon({
className: feature.properties.amenity,
iconSize: L.point(16, 16),
html: feature.properties.amenity[0].toUpperCase(),
popupAnchor: [3, -5],
}),
})
.bindPopup(
feature.properties.amenity +
"<br><b>" +
feature.properties.name +
"</b><br>" +
fileName +
"</b>"
)
.on("click", clickZoom);
},
onEachFeature: function(feature, layer) {
let name = fileName;
layer.on("click", function(e) {
console.log(e.target);
// this is where you have access to name from json
console.log(name);
});
},
};
// fetching data from geojson
const poiLayers = L.layerGroup().addTo(map);
let fileName;
// add data to geoJSON layer and add to LayerGroup
["bar", "pharmacy", "restaurant"].map((json) => {
...