Geocoder with MapLibreGL map

Geocoding example using the maplibre-gl-geocoder plugin.

<html>
<head>
  <meta charset="UTF-8" />
  <link href="https://unpkg.com/[email protected]/dist/maplibre-gl.css" rel="stylesheet" />
  <link href="https://unpkg.com/@maplibre/[email protected]/dist/maplibre-gl-geocoder.css" rel="stylesheet" />
  <style>
    body { width: 100%; height: 100%; padding: 0; margin: 0; }
    #map { width: 100%; height: 100%; }
  </style>
</head>
<body>
  <div id="map"></div>
  <script src="https://unpkg.com/[email protected]/dist/maplibre-gl.js"></script>
  <script src="https://unpkg.com/@maplibre/[email protected]/dist/maplibre-gl-geocoder.min.js"></script>
  <script>
    // Initialize MapLibreGL map
    let map = new maplibregl.Map({
      container: "map",
      style: "https://static.maptoolkit.net/rapidapi/styles/terrain.json?rapidapi-key=your-api-key",
      center: [11.40037, 47.26816],
      zoom: 12,
    });
    // Add geocoder control
    map.addControl(
      new MaplibreGeocoder({
        forwardGeocode: async (cfg) => {
          const response = await fetch(`https://maptoolkit.p.rapidapi.com/geocode/search?q=${cfg.query}&language=${cfg.language[0]}&rapidapi-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>