How to add Geocoding control to Leaflet map

In this tutorial, you will learn how to add a geocoding control to a Leaflet map. Geocoding control will enable map users to search places in the map.

Let’s start with a minimalistic HTML page that displays a fullscreen map. Replace YOUR_MAPTILER_API_KEY_HERE with your actual MapTiler API key.

<!DOCTYPE html>  
<html>  
<head>  
<title>MapTiler Geocoding Control</title>  
<meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>  
  
<style>  
#map {  
position: absolute;  
inset: 0;  
}  
</style>  
</head>  
  
<body>  
<div id="map"></div>  
  
<script>  
const apiKey = "YOUR_MAPTILER_API_KEY_HERE";  
  
const map = L.map(document.getElementById("map")).setView([49.2, 16.3], 6);  
  
const scale = devicePixelRatio > 1.5 ? "@2x" : "";  
  
L.tileLayer(  
`https://api.maptiler.com/maps/streets-v2/{z}/{x}/{y}${scale}.png?key=${apiKey}`,  
{  
tileSize: 512,  
zoomOffset: -1,  
minZoom: 1,  
attribution:  
'<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a>, ' +  
'<a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>',  
crossOrigin: true,  
}  
).addTo(map);  
</script>  
</body>  
</html>

Next, we will include the geocoder control script and styles. Add the following snippet to <head> element:

<script src="https://cdn.maptiler.com/maptiler-geocoding-control/v1.3.3/leaflet.umd.js"></script>
<link href="https://cdn.maptiler.com/maptiler-geocoding-control/v1.3.3/style.css" rel="stylesheet">

Finally, we will instantiate the geocoding control and add it to the map. Add the following snippet after the Leaflet map instantiation code:

L.control.maptilerGeocoding({ apiKey }).addTo(map);

Control can be additionally customized with various options which are documented on its project page.

Complete source code

Here copy the sample source code and paste it into your HTML file (your key is included).
Or copy the code below and replace YOUR_MAPTILER_API_KEY_HERE it with your actual MapTiler API key.

<!DOCTYPE html>  
<html>  
<head>  
<title>MapTiler Geocoding Control</title>  
<meta name="viewport" content="width=device-width, initial-scale=1.0" />  
  
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://cdn.maptiler.com/maptiler-geocoding-control/v1.3.3/leaflet.umd.js"></script>
<link href="https://cdn.maptiler.com/maptiler-geocoding-control/v1.3.3/style.css" rel="stylesheet">  
  
<style>  
#map {  
position: absolute;  
inset: 0;  
}  
</style>  
</head>  
  
<body>  
<div id="map"></div>  
  
<script>  
const apiKey = "YOUR_MAPTILER_API_KEY_HERE";  
  
const map = L.map(document.getElementById("map")).setView([49.2, 16.3], 6);  
  
const scale = devicePixelRatio > 1.5 ? "@2x" : "";  
  
L.tileLayer(  
`https://api.maptiler.com/maps/streets-v2/{z}/{x}/{y}${scale}.png?key=${apiKey}`,  
{  
tileSize: 512,  
zoomOffset: -1,  
minZoom: 1,  
attribution:  
'<a href="https://www.maptiler.com/copyright/" target="_blank">&copy; MapTiler</a>, ' +  
'<a href="https://www.openstreetmap.org/copyright" target="_blank">&copy; OpenStreetMap contributors</a>',  
crossOrigin: true,  
}  
).addTo(map);

L.control.maptilerGeocoding({ apiKey }).addTo(map);

</script>  
</body>  
</html>

Geocoding on-prem with MapTiler Server

You can utilize the geocoding capabilities within your on-prem installation of MapTiler Server. This feature allows you to search for addresses and locations without an internet connection. Please take a look at the MapTiler Server API documentation for a comprehensive understanding of all the available options the geocoding API provides.