code Snippetlanguage CDNpackage_2 NPM
content_copy Copyopen_in_new Open
var map = new ol. Map ({
target: 'map' ,
layers: [
new ol.layer. Tile ({ source: new ol.source. XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) })
],
view: new ol. View ({ center: ol.proj. fromLonLat ([ 2.3522 , 48.8566 ]), zoom: 5 })
});
var marker = new ol.layer. Vector ({ source: new ol.source. Vector (), style: new ol.style. Style ({ image: new ol.style. Circle ({ radius: 8 , fill: new ol.style. Fill ({ color: '#e63946' }), stroke: new ol.style. Stroke ({ color: '#fff' , width: 2 }) }) }) });
map. addLayer (marker);
var input = document. getElementById ( 'search-input' );
var resultsDiv = document. getElementById ( 'results' );
input. addEventListener ( 'input' , function () {
var q = input.value. trim ();
if (q. length < 3 ) { resultsDiv.style.display = 'none' ; return ; }
fetch ( 'https://api.maptiler.com/geocoding/' + encodeURIComponent (q) + '.json?key=YOUR_MAPTILER_KEY' )
. then ( function ( r ) { return r. json (); })
. then ( function ( data ) {
resultsDiv.innerHTML = '' ;
resultsDiv.style.display = data.features. length ? 'block' : 'none' ;
data.features. forEach ( function ( f ) {
var div = document. createElement ( 'div' );
div.textContent = f.place_name;
div.style.cssText = 'padding:8px 12px;cursor:pointer;border-bottom:1px solid #eee;' ;
div. onclick = function () {
marker. getSource (). clear ();
marker. getSource (). addFeature ( new ol. Feature ({ geometry: new ol.geom. Point (ol.proj. fromLonLat (f.center)) }));
map. getView (). animate ({ center: ol.proj. fromLonLat (f.center), zoom: 14 });
resultsDiv.style.display = 'none' ;
input.value = f.place_name;
};
resultsDiv. appendChild (div);
});
});
}); 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/ol@10.10.0/ol.css" />
< script src = "https://unpkg.com/ol@10.10.0/dist/ol.js" ></ script >
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< div style = "position:absolute;top:10px;left:10px;z-index:1;" >
< input id = "search-input" type = "text" placeholder = "Search a place..." style = "padding:6px 12px;width:260px;background:#141414;color:#e0e0e0;border:2px solid #2a2a2a;font-family:monospace;font-size:12px;" >
< div id = "results" style = "background:#141414;color:#e0e0e0;max-height:200px;overflow-y:auto;border:2px solid #2a2a2a;border-top:none;display:none;font-family:monospace;font-size:12px;" ></ div >
</ div >
< script >
var map = new ol. Map ({
target: 'map' ,
layers: [
new ol.layer. Tile ({ source: new ol.source. XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) })
],
view: new ol. View ({ center: ol.proj. fromLonLat ([ 2.3522 , 48.8566 ]), zoom: 5 })
});
var marker = new ol.layer. Vector ({ source: new ol.source. Vector (), style: new ol.style. Style ({ image: new ol.style. Circle ({ radius: 8 , fill: new ol.style. Fill ({ color: '#e63946' }), stroke: new ol.style. Stroke ({ color: '#fff' , width: 2 }) }) }) });
map. addLayer (marker);
var input = document. getElementById ( 'search-input' );
var resultsDiv = document. getElementById ( 'results' );
input. addEventListener ( 'input' , function () {
var q = input.value. trim ();
if (q. length < 3 ) { resultsDiv.style.display = 'none' ; return ; }
fetch ( 'https://api.maptiler.com/geocoding/' + encodeURIComponent (q) + '.json?key=YOUR_MAPTILER_KEY' )
. then ( function ( r ) { return r. json (); })
. then ( function ( data ) {
resultsDiv.innerHTML = '' ;
resultsDiv.style.display = data.features. length ? 'block' : 'none' ;
data.features. forEach ( function ( f ) {
var div = document. createElement ( 'div' );
div.textContent = f.place_name;
div.style.cssText = 'padding:8px 12px;cursor:pointer;border-bottom:1px solid #eee;' ;
div. onclick = function () {
marker. getSource (). clear ();
marker. getSource (). addFeature ( new ol. Feature ({ geometry: new ol.geom. Point (ol.proj. fromLonLat (f.center)) }));
map. getView (). animate ({ center: ol.proj. fromLonLat (f.center), zoom: 14 });
resultsDiv.style.display = 'none' ;
input.value = f.place_name;
};
resultsDiv. appendChild (div);
});
});
});
</ script >
</ body >
</ html > content_copy Copyopen_in_new Open
import Map from 'ol/Map' ;
import View from 'ol/View' ;
import TileLayer from 'ol/layer/Tile' ;
import { fromLonLat } from 'ol/proj' ;
import 'ol/ol.css' ;
var map = new ol. Map ({
target: 'map' ,
layers: [
new ol.layer. Tile ({ source: new ol.source. XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) })
],
view: new ol. View ({ center: ol.proj. fromLonLat ([ 2.3522 , 48.8566 ]), zoom: 5 })
});
var marker = new ol.layer. Vector ({ source: new ol.source. Vector (), style: new ol.style. Style ({ image: new ol.style. Circle ({ radius: 8 , fill: new ol.style. Fill ({ color: '#e63946' }), stroke: new ol.style. Stroke ({ color: '#fff' , width: 2 }) }) }) });
map. addLayer (marker);
var input = document. getElementById ( 'search-input' );
var resultsDiv = document. getElementById ( 'results' );
input. addEventListener ( 'input' , function () {
var q = input.value. trim ();
if (q. length < 3 ) { resultsDiv.style.display = 'none' ; return ; }
fetch ( 'https://api.maptiler.com/geocoding/' + encodeURIComponent (q) + '.json?key=YOUR_MAPTILER_KEY' )
. then ( function ( r ) { return r. json (); })
. then ( function ( data ) {
resultsDiv.innerHTML = '' ;
resultsDiv.style.display = data.features. length ? 'block' : 'none' ;
data.features. forEach ( function ( f ) {
var div = document. createElement ( 'div' );
div.textContent = f.place_name;
div.style.cssText = 'padding:8px 12px;cursor:pointer;border-bottom:1px solid #eee;' ;
div. onclick = function () {
marker. getSource (). clear ();
marker. getSource (). addFeature ( new ol. Feature ({ geometry: new ol.geom. Point (ol.proj. fromLonLat (f.center)) }));
map. getView (). animate ({ center: ol.proj. fromLonLat (f.center), zoom: 14 });
resultsDiv.style.display = 'none' ;
input.value = f.place_name;
};
resultsDiv. appendChild (div);
});
});
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) })
],
view: new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), zoom: 5 })
});
var marker = new ol.layer.Vector({ source: new ol.source.Vector(), style: new ol.style.Style({ image: new ol.style.Circle({ radius: 8, fill: new ol.style.Fill({ color: '#e63946' }), stroke: new ol.style.Stroke({ color: '#fff', width: 2 }) }) }) });
map.addLayer(marker);
var input = document.getElementById('search-input');
var resultsDiv = document.getElementById('results');
input.addEventListener('input', function() {
var q = input.value.trim();
if (q.length < 3) { resultsDiv.style.display = 'none'; return; }
fetch('https://api.maptiler.com/geocoding/' + encodeURIComponent(q) + '.json?key=YOUR_MAPTILER_KEY')
.then(function(r) { return r.json(); })
.then(function(data) {
resultsDiv.innerHTML = '';
resultsDiv.style.display = data.features.length ? 'block' : 'none';
data.features.forEach(function(f) {
var div = document.createElement('div');
div.textContent = f.place_name;
div.style.cssText = 'padding:8px 12px;cursor:pointer;border-bottom:1px solid #eee;';
div.onclick = function() {
marker.getSource().clear();
marker.getSource().addFeature(new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat(f.center)) }));
map.getView().animate({ center: ol.proj.fromLonLat(f.center), zoom: 14 });
resultsDiv.style.display = 'none';
input.value = f.place_name;
};
resultsDiv.appendChild(div);
});
});
}); <!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/ol@10.10.0/ol.css" />
<script src="https://unpkg.com/ol@10.10.0/dist/ol.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<div style="position:absolute;top:10px;left:10px;z-index:1;">
<input id="search-input" type="text" placeholder="Search a place..." style="padding:6px 12px;width:260px;background:#141414;color:#e0e0e0;border:2px solid #2a2a2a;font-family:monospace;font-size:12px;">
<div id="results" style="background:#141414;color:#e0e0e0;max-height:200px;overflow-y:auto;border:2px solid #2a2a2a;border-top:none;display:none;font-family:monospace;font-size:12px;"></div>
</div>
<script>
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) })
],
view: new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), zoom: 5 })
});
var marker = new ol.layer.Vector({ source: new ol.source.Vector(), style: new ol.style.Style({ image: new ol.style.Circle({ radius: 8, fill: new ol.style.Fill({ color: '#e63946' }), stroke: new ol.style.Stroke({ color: '#fff', width: 2 }) }) }) });
map.addLayer(marker);
var input = document.getElementById('search-input');
var resultsDiv = document.getElementById('results');
input.addEventListener('input', function() {
var q = input.value.trim();
if (q.length < 3) { resultsDiv.style.display = 'none'; return; }
fetch('https://api.maptiler.com/geocoding/' + encodeURIComponent(q) + '.json?key=YOUR_MAPTILER_KEY')
.then(function(r) { return r.json(); })
.then(function(data) {
resultsDiv.innerHTML = '';
resultsDiv.style.display = data.features.length ? 'block' : 'none';
data.features.forEach(function(f) {
var div = document.createElement('div');
div.textContent = f.place_name;
div.style.cssText = 'padding:8px 12px;cursor:pointer;border-bottom:1px solid #eee;';
div.onclick = function() {
marker.getSource().clear();
marker.getSource().addFeature(new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat(f.center)) }));
map.getView().animate({ center: ol.proj.fromLonLat(f.center), zoom: 14 });
resultsDiv.style.display = 'none';
input.value = f.place_name;
};
resultsDiv.appendChild(div);
});
});
});
</script>
</body>
</html> import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import { fromLonLat } from 'ol/proj';
import 'ol/ol.css';
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) })
],
view: new ol.View({ center: ol.proj.fromLonLat([2.3522, 48.8566]), zoom: 5 })
});
var marker = new ol.layer.Vector({ source: new ol.source.Vector(), style: new ol.style.Style({ image: new ol.style.Circle({ radius: 8, fill: new ol.style.Fill({ color: '#e63946' }), stroke: new ol.style.Stroke({ color: '#fff', width: 2 }) }) }) });
map.addLayer(marker);
var input = document.getElementById('search-input');
var resultsDiv = document.getElementById('results');
input.addEventListener('input', function() {
var q = input.value.trim();
if (q.length < 3) { resultsDiv.style.display = 'none'; return; }
fetch('https://api.maptiler.com/geocoding/' + encodeURIComponent(q) + '.json?key=YOUR_MAPTILER_KEY')
.then(function(r) { return r.json(); })
.then(function(data) {
resultsDiv.innerHTML = '';
resultsDiv.style.display = data.features.length ? 'block' : 'none';
data.features.forEach(function(f) {
var div = document.createElement('div');
div.textContent = f.place_name;
div.style.cssText = 'padding:8px 12px;cursor:pointer;border-bottom:1px solid #eee;';
div.onclick = function() {
marker.getSource().clear();
marker.getSource().addFeature(new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat(f.center)) }));
map.getView().animate({ center: ol.proj.fromLonLat(f.center), zoom: 14 });
resultsDiv.style.display = 'none';
input.value = f.place_name;
};
resultsDiv.appendChild(div);
});
});
});