Skip to content

MapLibre GL JS

This example adds a search control to the top right corner of a MapLibre GL map using the maplibre-gl-geocoder plugin. Results are fetched from the Maptoolkit Geocoding API. Try searching for Sillgasse.

Dependencies: maplibre-gl-geocoder

let map = new maplibregl.Map({
      container: "map",
      style: "https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=YOUR_API_KEY",
      center: [11.40037, 47.26816],
      zoom: 12,
    });

    map.addControl(
      new MaplibreGeocoder({
        forwardGeocode: async (cfg) => {
          const response = await fetch(
            `https://geocoder.maptoolkit.net/search?q=${encodeURIComponent(cfg.query)}&language=${cfg.language[0]}&api_key=YOUR_API_KEY`
          );
          const result = await response.json();
          return {
            features: result.map((e) => ({
              type: "Feature",
              geometry: { type: "Point", coordinates: [e.lon, e.lat] },
              place_type: ["place"],
              place_name: e.display_name,
              text: e.type,
              properties: e,
              center: [e.lon, e.lat]
            }))
          };
        },
      }, {
        showResultsWhileTyping: true,
        showResultMarkers: false,
        maplibregl: maplibregl
      })
    );
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.js"></script>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/maplibre-gl.css" />
  <script src="https://cdn.jsdelivr.net/npm/@maplibre/[email protected]/dist/maplibre-gl-geocoder.min.js"></script>
  <link href="https://cdn.jsdelivr.net/npm/@maplibre/[email protected]/dist/maplibre-gl-geocoder.css" rel="stylesheet" />
  <style>
    html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
    #map { width: 100%; height: 100%; }
  </style>
</head>
<body>
  <div id="map"></div>
  <script>
    let map = new maplibregl.Map({
      container: "map",
      style: "https://styles.maptoolkit.net/maptoolkit/maptoolkit.summer.json?api_key=YOUR_API_KEY",
      center: [11.40037, 47.26816],
      zoom: 12,
    });

    map.addControl(
      new MaplibreGeocoder({
        forwardGeocode: async (cfg) => {
          const response = await fetch(
            `https://geocoder.maptoolkit.net/search?q=${encodeURIComponent(cfg.query)}&language=${cfg.language[0]}&api_key=YOUR_API_KEY`
          );
          const result = await response.json();
          return {
            features: result.map((e) => ({
              type: "Feature",
              geometry: { type: "Point", coordinates: [e.lon, e.lat] },
              place_type: ["place"],
              place_name: e.display_name,
              text: e.type,
              properties: e,
              center: [e.lon, e.lat]
            }))
          };
        },
      }, {
        showResultsWhileTyping: true,
        showResultMarkers: false,
        maplibregl: maplibregl
      })
    );
  </script>
</body>
</html>

Use the prompt below with any LLM to get the same result. Make sure the Maptoolkit MCP server is connected first — check out AI Integration & MCP to get started.

Use the Maptoolkit Connector. Create a map of Innsbruck with a geocoder in the top right corner, using MapLibre GL.