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" />
<script src="https://unpkg.com/leaflet@latest/dist/leaflet.js"></script>
<link href="https://unpkg.com/leaflet@latest/dist/leaflet.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/{z}/{x}/{y}${scale}.png?key=${apiKey}`,
{
tileSize: 512,
zoomOffset: -1,
minZoom: 1,
attribution:
'<a href="https://www.maptiler.com/copyright/" target="_blank">© MapTiler</a>, ' +
'<a href="https://www.openstreetmap.org/copyright" target="_blank">© 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://unpkg.com/@maptiler/geocoding-control@latest/leaflet.umd.js"></script>
<link href="https://unpkg.com/@maptiler/geocoding-control@latest/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 lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Display a Geocoding Control</title>
<script src="https://cdn.maptiler.com/maptiler-sdk-js/v1.1.1/maptiler-sdk.umd.min.js"></script>
<link href="https://cdn.maptiler.com/maptiler-sdk-js/v1.1.1/maptiler-sdk.css" rel="stylesheet" />
<script src="https://cdn.maptiler.com/maptiler-geocoding-control/v0.0.98/maptilersdk.umd.js"></script>
<link href="https://cdn.maptiler.com/maptiler-geocoding-control/v0.0.98/style.css" rel="stylesheet" />
<style>
body {
margin: 0;
padding: 0;
font-family: sans-serif;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
maptilersdk.config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = (window.map = new maptilersdk.Map({
container: 'map', // container's id or the HTML element to render the map
style: maptilersdk.MapStyle.STREETS, // stylesheet location
zoom: 3,
center: [-94.77, 38.57],
}));
const gc = new maptilersdkMaptilerGeocoder.GeocodingControl({}); map.addControl(gc, 'top-left');
</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.
Comments
0 comments
Please sign in to leave a comment.