MapSnippets logo MapSnippets logo

Address Search with MapTiler SDK

MapTiler SDK

Geocode a place name with the MapTiler Geocoding API via fetch() and fly a MapTiler SDK map to the result with map.flyTo() and a maptilersdk.Marker.

progress_activity Loading map...
maptilersdk.config.apiKey = 'YOUR_MAPTILER_KEY';
const map = new maptilersdk.Map({
  container: 'map',
  style: maptilersdk.MapStyle.STREETS,
  center: [2.3522, 48.8566],
  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 maptilersdk.Marker({ color: '#9C27B0' }).setLngLat([lng, lat]).addTo(map);
        new maptilersdk.Popup().setLngLat([lng, lat]).setHTML('<b>' + f.place_name + '</b>').addTo(map);
        map.flyTo({ center: [lng, lat], zoom: 14 });
        results.style.display = 'none';
        input.value = f.place_name;
      };
      results.appendChild(div);
    });
  }, 300);
});

How to add address search with MapTiler SDK

The MapTiler Geocoding API accepts a text query and returns matching places with coordinates. As the user types (minimum 3 characters), a debounced request fires after 300ms of inactivity. The response fills a dropdown with up to 5 matching results.

Each feature has a center property with [lng, lat] coordinates. Clicking a result places a purple marker, shows a popup, and flies the map to zoom level 14. The dropdown hides and the input updates with the selected place name.

The GeocodingControl widget is a separate package, so this example uses the REST API directly with fetch() for simplicity.

Key APIs

API Description Docs
Geocoding API Forward geocoding REST endpoint docs.maptiler.com/cloud/api/geocoding/
map.flyTo() Animate to a new position docs.maptiler.com/sdk-js/api/
encodeURIComponent URL-encode the search query MDN

FAQ

Can I limit results to a specific country?

Yes. Add &country=fr to the URL to restrict results to France. Multiple countries can be comma-separated.

How many results does the API return?

By default, up to 5 features. Add &limit=10 to get more (maximum 10).

Common mistakes

  • Using the GeocodingControl package without installing it separately. It is NOT in the MapTiler SDK UMD bundle
  • Forgetting encodeURIComponent, so place names with spaces or special characters break the URL
  • Not checking for empty results. The features array may be empty for ambiguous queries

Build maps using MapTiler SDK with AI

Generate code like this address search with maptiler sdk example using the MapTiler SDK skill in your IDE.

Related examples