code Snippetlanguage CDNpackage_2 NPM
content_copy Copyopen_in_new Open
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.42 , 50.08 ],
zoom: 5
});
let marker, timeout;
const input = document. getElementById ( 'search-box' );
const results = document. getElementById ( 'results' );
input. addEventListener ( 'input' , () => {
clearTimeout (timeout);
const q = input.value. trim ();
if (q. length < 3 ) { results.style.display = 'none' ; return ; }
timeout = setTimeout ( async () => {
const res = await fetch ( 'https://api.maptiler.com/geocoding/' + encodeURIComponent (q) + '.json?key=YOUR_MAPTILER_KEY&limit=5' );
const data = await res. json ();
results.innerHTML = '' ;
results.style.display = data.features. length ? 'block' : 'none' ;
data.features. forEach ( f => {
const div = document. createElement ( 'div' );
div.textContent = f.place_name;
div. onclick = () => {
const [ lng , lat ] = f.center;
if (marker) marker. remove ();
marker = new maplibregl. Marker ({ color: '#2196F3' })
. setLngLat ([lng, lat])
. setPopup ( new maplibregl. Popup (). setHTML ( '<b>' + f.place_name + '</b>' ))
. addTo (map);
marker. togglePopup ();
map. flyTo ({ center: [lng, lat], zoom: 14 });
results.style.display = 'none' ;
input.value = f.place_name;
};
results. appendChild (div);
});
}, 300 );
}); content_copy Copyopen_in_new Open
<! DOCTYPE html >
< html >
< head >
< meta charset = "utf-8" />
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" />
< link rel = "stylesheet" href = "https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.css" />
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
#search-box { position : absolute ; top : 10 px ; left : 10 px ; z-index : 1 ; padding : 8 px 12 px ; font-size : 12 px ; font-family : monospace ; background : #141414 ; color : #e0e0e0 ; border : 2 px solid #2a2a2a ; width : 280 px ; }
#results { position : absolute ; top : 42 px ; left : 10 px ; z-index : 1 ; background : #141414 ; border : 2 px solid #2a2a2a ; border-top : none ; width : 280 px ; max-height : 200 px ; overflow-y : auto ; display : none ; }
#results div { padding : 8 px 12 px ; cursor : pointer ; font-size : 11 px ; font-family : monospace ; color : #c0c0c0 ; border-bottom : 1 px solid #1e1e1e ; }
#results div :hover { background : #1a1a1a ; color : #e0e0e0 ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< input id = "search-box" type = "text" placeholder = "Search address..." />
< div id = "results" ></ div >
< script type = "module" >
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.mjs' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.42 , 50.08 ],
zoom: 5
});
let marker, timeout;
const input = document. getElementById ( 'search-box' );
const results = document. getElementById ( 'results' );
input. addEventListener ( 'input' , () => {
clearTimeout (timeout);
const q = input.value. trim ();
if (q. length < 3 ) { results.style.display = 'none' ; return ; }
timeout = setTimeout ( async () => {
const res = await fetch ( 'https://api.maptiler.com/geocoding/' + encodeURIComponent (q) + '.json?key=YOUR_MAPTILER_KEY&limit=5' );
const data = await res. json ();
results.innerHTML = '' ;
results.style.display = data.features. length ? 'block' : 'none' ;
data.features. forEach ( f => {
const div = document. createElement ( 'div' );
div.textContent = f.place_name;
div. onclick = () => {
const [ lng , lat ] = f.center;
if (marker) marker. remove ();
marker = new maplibregl. Marker ({ color: '#2196F3' })
. setLngLat ([lng, lat])
. setPopup ( new maplibregl. Popup (). setHTML ( '<b>' + f.place_name + '</b>' ))
. addTo (map);
marker. togglePopup ();
map. flyTo ({ center: [lng, lat], zoom: 14 });
results.style.display = 'none' ;
input.value = f.place_name;
};
results. appendChild (div);
});
}, 300 );
});
</ script >
</ body >
</ html > content_copy Copyopen_in_new Open
import maplibregl from 'maplibre-gl' ;
import 'maplibre-gl/dist/maplibre-gl.css' ;
const map = new maplibregl. Map ({
container: 'map' ,
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY' ,
center: [ 14.42 , 50.08 ],
zoom: 5
});
let marker, timeout;
const input = document. getElementById ( 'search-box' );
const results = document. getElementById ( 'results' );
input. addEventListener ( 'input' , () => {
clearTimeout (timeout);
const q = input.value. trim ();
if (q. length < 3 ) { results.style.display = 'none' ; return ; }
timeout = setTimeout ( async () => {
const res = await fetch ( 'https://api.maptiler.com/geocoding/' + encodeURIComponent (q) + '.json?key=YOUR_MAPTILER_KEY&limit=5' );
const data = await res. json ();
results.innerHTML = '' ;
results.style.display = data.features. length ? 'block' : 'none' ;
data.features. forEach ( f => {
const div = document. createElement ( 'div' );
div.textContent = f.place_name;
div. onclick = () => {
const [ lng , lat ] = f.center;
if (marker) marker. remove ();
marker = new maplibregl. Marker ({ color: '#2196F3' })
. setLngLat ([lng, lat])
. setPopup ( new maplibregl. Popup (). setHTML ( '<b>' + f.place_name + '</b>' ))
. addTo (map);
marker. togglePopup ();
map. flyTo ({ center: [lng, lat], zoom: 14 });
results.style.display = 'none' ;
input.value = f.place_name;
};
results. appendChild (div);
});
}, 300 );
});
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.42, 50.08],
zoom: 5
});
let marker, timeout;
const input = document.getElementById('search-box');
const results = document.getElementById('results');
input.addEventListener('input', () => {
clearTimeout(timeout);
const q = input.value.trim();
if (q.length < 3) { results.style.display = 'none'; return; }
timeout = setTimeout(async () => {
const res = await fetch('https://api.maptiler.com/geocoding/' + encodeURIComponent(q) + '.json?key=YOUR_MAPTILER_KEY&limit=5');
const data = await res.json();
results.innerHTML = '';
results.style.display = data.features.length ? 'block' : 'none';
data.features.forEach(f => {
const div = document.createElement('div');
div.textContent = f.place_name;
div.onclick = () => {
const [lng, lat] = f.center;
if (marker) marker.remove();
marker = new maplibregl.Marker({ color: '#2196F3' })
.setLngLat([lng, lat])
.setPopup(new maplibregl.Popup().setHTML('<b>' + f.place_name + '</b>'))
.addTo(map);
marker.togglePopup();
map.flyTo({ center: [lng, lat], zoom: 14 });
results.style.display = 'none';
input.value = f.place_name;
};
results.appendChild(div);
});
}, 300);
}); <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.css" />
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
#search-box { position: absolute; top: 10px; left: 10px; z-index: 1; padding: 8px 12px; font-size: 12px; font-family: monospace; background: #141414; color: #e0e0e0; border: 2px solid #2a2a2a; width: 280px; }
#results { position: absolute; top: 42px; left: 10px; z-index: 1; background: #141414; border: 2px solid #2a2a2a; border-top: none; width: 280px; max-height: 200px; overflow-y: auto; display: none; }
#results div { padding: 8px 12px; cursor: pointer; font-size: 11px; font-family: monospace; color: #c0c0c0; border-bottom: 1px solid #1e1e1e; }
#results div:hover { background: #1a1a1a; color: #e0e0e0; }
</style>
</head>
<body>
<div id="map"></div>
<input id="search-box" type="text" placeholder="Search address..." />
<div id="results"></div>
<script type="module">
import * as maplibregl from 'https://unpkg.com/maplibre-gl@6.11.2/dist/maplibre-gl.mjs';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.42, 50.08],
zoom: 5
});
let marker, timeout;
const input = document.getElementById('search-box');
const results = document.getElementById('results');
input.addEventListener('input', () => {
clearTimeout(timeout);
const q = input.value.trim();
if (q.length < 3) { results.style.display = 'none'; return; }
timeout = setTimeout(async () => {
const res = await fetch('https://api.maptiler.com/geocoding/' + encodeURIComponent(q) + '.json?key=YOUR_MAPTILER_KEY&limit=5');
const data = await res.json();
results.innerHTML = '';
results.style.display = data.features.length ? 'block' : 'none';
data.features.forEach(f => {
const div = document.createElement('div');
div.textContent = f.place_name;
div.onclick = () => {
const [lng, lat] = f.center;
if (marker) marker.remove();
marker = new maplibregl.Marker({ color: '#2196F3' })
.setLngLat([lng, lat])
.setPopup(new maplibregl.Popup().setHTML('<b>' + f.place_name + '</b>'))
.addTo(map);
marker.togglePopup();
map.flyTo({ center: [lng, lat], zoom: 14 });
results.style.display = 'none';
input.value = f.place_name;
};
results.appendChild(div);
});
}, 300);
});
</script>
</body>
</html> import maplibregl from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets-v4/style.json?key=YOUR_MAPTILER_KEY',
center: [14.42, 50.08],
zoom: 5
});
let marker, timeout;
const input = document.getElementById('search-box');
const results = document.getElementById('results');
input.addEventListener('input', () => {
clearTimeout(timeout);
const q = input.value.trim();
if (q.length < 3) { results.style.display = 'none'; return; }
timeout = setTimeout(async () => {
const res = await fetch('https://api.maptiler.com/geocoding/' + encodeURIComponent(q) + '.json?key=YOUR_MAPTILER_KEY&limit=5');
const data = await res.json();
results.innerHTML = '';
results.style.display = data.features.length ? 'block' : 'none';
data.features.forEach(f => {
const div = document.createElement('div');
div.textContent = f.place_name;
div.onclick = () => {
const [lng, lat] = f.center;
if (marker) marker.remove();
marker = new maplibregl.Marker({ color: '#2196F3' })
.setLngLat([lng, lat])
.setPopup(new maplibregl.Popup().setHTML('<b>' + f.place_name + '</b>'))
.addTo(map);
marker.togglePopup();
map.flyTo({ center: [lng, lat], zoom: 14 });
results.style.display = 'none';
input.value = f.place_name;
};
results.appendChild(div);
});
}, 300);
});