Leaflet, an open-source JavaScript library, is widely used for creating interactive maps on the web. It's lightweight, mobile-friendly, and packed with features that make it a popular choice among developers. If you're new to Leaflet and looking for example code to get started, you've come to the right place.

In this article, we'll explore Leaflet's core functionalities with practical examples. By the end, you'll have a solid understanding of how to use Leaflet to create engaging maps for your web projects.

Setting Up Your First Leaflet Map
Before we dive into the examples, ensure you have included the Leaflet CSS and JavaScript files in your project. You can either download them or use the CDN links provided on the official Leaflet website.

Once you've included the necessary files, you're ready to create your first map. Here's a simple example:
Basic Map Setup

To create a basic map, you'll need to initialize Leaflet with a map container and set its center and zoom level.
```javascript let map = L.map('map').setView([51.505, -0.09], 13); ```
Adding a Tile Layer

A tile layer provides the base map tiles for your Leaflet map. Leaflet includes a tile layer for OpenStreetMap by default, but you can also use other tile providers or custom tile layers.
```javascript L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap contributors' }).addTo(map); ```
Adding Markers and Popups

Markers are essential for highlighting specific locations on your map. They can also display additional information through popups.
Here's how to add a marker and a popup to your map:


![50+ Flyer Examples, Templates and Design Tips [2022]](https://i.pinimg.com/originals/e2/0b/43/e20b43420b4c3f50f70bd236fe112d27.png)

















Adding a Single Marker
To add a marker, you'll need to specify its latitude and longitude coordinates.
```javascript let marker = L.marker([51.505, -0.09]).addTo(map); ```
Adding a Popup to a Marker
Popups allow you to display additional information when a user clicks on a marker. You can create a popup and bind it to a marker like this:
```javascript let popup = L.popup() .setLatLng([51.505, -0.09]) .setContent('Hello, world!') .openOn(map); ```
Creating Custom Icons and Clusters
Leaflet allows you to create custom marker icons and cluster markers to improve the visual appeal and performance of your map.
Here's how to create a custom icon and use it to add markers to your map:
Creating a Custom Icon
You can create a custom icon by defining its URL and size, and optionally, its shadow URL and size.
```javascript let customIcon = L.icon({ iconUrl: 'path/to/your/icon.png', iconSize: [32, 32], shadowUrl: 'path/to/your/shadow.png', shadowSize: [41, 41], shadowAnchor: [12, 12] }); ```
Using Custom Icons and Marker Clustering
Once you've created a custom icon, you can use it to add markers to your map. To improve performance with many markers, you can use the MarkerCluster plugin to group nearby markers into clusters.
```javascript let markers = [ L.marker([51.505, -0.09], { icon: customIcon }), L.marker([51.515, -0.09], { icon: customIcon }), // Add more markers as needed ]; L.layerGroup(markers).addTo(map); L.MarkerClusterGroup().addLayers(markers).addTo(map); ```
Leaflet's flexibility and extensive feature set make it an excellent choice for creating interactive maps on the web. By understanding and implementing the examples provided in this article, you'll be well on your way to creating engaging and informative maps for your projects. Happy mapping!