Blog -

Jan 4, 2025

Integrate leafletjs with a search bar on my website

A step-by-step guide to integrating Leaflet.js with a search bar, and how mamap.io can simplify the process.
mamap.iomamap.io

Adding a search bar to an interactive map can significantly enhance user experience, especially for location-based services like directories, event maps, or delivery zones. While Leaflet.js, a powerful open-source mapping library, is a popular choice for custom maps, integrating a search bar can be more complex than it seems.

In this tutorial, we’ll walk you through the process step by step and highlight the challenges you might face along the way. If managing these complexities feels overwhelming, don't worry — we'll show you how mamap.io can handle this for you with ease.


Step 1: Set Up Leaflet.js

Installing Leaflet.js

Start by adding Leaflet.js to your project. You can either include the library using a CDN or install it via npm.

Option: by Using CDN

<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>


<div id="map" style="height: 500px;"></div>

<script>
  const map = L.map('map').setView([51.505, -0.09], 13);
  L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '© OpenStreetMap contributors'
  }).addTo(map);
</script>

At this point, you have a simple map displaying OpenStreetMap tiles.

Choosing a Search Plugin Leaflet.js does not include a search bar by default. To add one, you’ll need to use a plugin like Leaflet Control Search.

Installing Leaflet Control Search Add the plugin to your project:

<link rel="stylesheet" href="https://unpkg.com/leaflet-control-search/dist/leaflet-search.min.css" />
<script src="https://unpkg.com/leaflet-control-search/dist/leaflet-search.min.js"></script>

You need to provide searchable data, such as markers with associated properties.

const data = [
  { loc: [51.505, -0.09], title: "Location A" },
  { loc: [51.51, -0.1], title: "Location B" }
];

const markers = data.map(item => {
  const marker = L.marker(item.loc).addTo(map);
  marker.bindPopup(item.title);
  return marker;
});

const searchControl = new L.Control.Search({
  layer: L.layerGroup(markers),
  propertyName: 'title',
  moveToLocation: (latlng) => {
    map.setView(latlng, 16);
  }
});

map.addControl(searchControl);

This code adds a basic search bar that allows users to search for predefined markers by title.

Challenges you might face

  1. Data Management You’ll need a structured dataset for searchable locations and autofill suggestions, whether it's hardcoded, fetched from a database, or sourced from APIs. This can quickly add some hours of works depending your needs.
  2. Styling and User Experience Providing a seamless user experience involves careful attention to the UI, including how suggestions appear and behave during user interactions.

Fastest solution

Integrating Leaflet.js with a search bar and managing autofill can be complex, especially when handling large datasets or ensuring optimal performance. If you want to save time and effort, mamap.io has you covered.

With mamap.io, you can:

Create customized maps with built-in search bar and autofill functionality. Easily manage location data from your dashboard without worrying about coding. Focus on your business while we handle the boring stuff.