Leaflet Autocomplete Geosearch Tutorial

The code examples below will use your API key once you log in.

Adding a Geosearch control to a Leaflet map

Leafletjs is a powerful open source library to add maps to a website. Below is an example HTML page and map that includes an OpenCage Geosearch control to allow users to search for locations (cities, states, villages, etc) and then re-center the map on a selected location.

For details on how to control the map (set position, zoom, listen to events, add map markers, or set a different style) see the Leaflet quick tutorial and reference documentation.

For details on how to configure the Geosearch control (number of results, language, or country filters) see the geosearch documentation. Changing the layout (fonts, colors) is explained on the algolia autocomplete theming reference guide.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <!-- CSS files need to be in the head section -->
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.8.0/dist/leaflet.css" integrity="sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ==" crossorigin="" />
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@opencage/leaflet-opencage-geosearch/leaflet-opencage-geosearch.css" />
  </head>
  <body>

    <!-- add a map element anywhere on the map -->
    <div id="map1" style="width:750px; height:500px;"></div>

    <!-- script can be either in the head section or anywhere below the map -->
    <script src="https://unpkg.com/leaflet@1.8.0/dist/leaflet.js" integrity="sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ==" crossorigin=""></script>            
    <script src="https://cdn.jsdelivr.net/npm/@opencage/geosearch-bundle"></script>
    <script src="https://cdn.jsdelivr.net/npm/@opencage/leaflet-opencage-geosearch"></script>
    <script>
      // wait until everything is loaded
      document.addEventListener('DOMContentLoaded', () => {

        // create and initialize leaflet map object
        var map = L.map('map1').setView([40, 0], 5);

        // load map tiles
        L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
          attribution: 'Data <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, '
          + 'Map tiles &copy; <a href="https://carto.com/attribution">CARTO</a>'
        }).addTo(map);

        // geosearch options

        var options = {
          key: 'YOUR-GEOSEARCH-API-KEY',
          // you will need to become a customer to get a geosearch key

          position: 'topright',
          // see possible values: https://leafletjs.com/reference.html#control-position
        };

        // add geosearch to the map
        var geosearchControl = L.Control.openCageGeosearch(options).addTo(map);
      });
    </script>
  </body>
</html>

Example leaflet map with an OpenCage Geosearch control