Leaflet marker with a custom icon

This tutorial shows how to define your custom icons for markers using Leaflet JS.

Introducing custom images and shadows to your markers can significantly enhance the visual appeal of your map. By adding a personal touch to your Leaflet icons, you can create a unique and engaging experience for your users.

NPM module setup


npm install --save @maptiler/leaflet-maptilersdk leaflet
import { MaptilerLayer, MapStyle } from '@maptiler/leaflet-maptilersdk';
import L from 'leaflet';
import 'leaflet/dist/leaflet.css';

const key = 'YOUR_MAPTILER_API_KEY_HERE';
const map = L.map('map').setView([45.9767521, 7.6569418], 12);
const mtLayer = new MaptilerLayer({
  apiKey: key,
  style: MapStyle.OUTDOOR, // optional
}).addTo(map);

const LeafIcon = L.Icon.extend({
    options: {
      shadowUrl: "https://docs.maptiler.com/leaflet/assets/leaf_shadow.png",
      iconSize:     [38, 95],
      shadowSize:   [50, 64],
      iconAnchor:   [22, 94],
      shadowAnchor: [4, 62],
      popupAnchor:  [-3, -76]
    }
  });

const leafIcon = new LeafIcon({iconUrl: "https://docs.maptiler.com/leaflet/assets/leaf_marker.png"});

L.marker([45.9767521, 7.6569418], {icon: leafIcon})
  .addTo(map)
  .openPopup();

Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.

Learn more

Check out the How to make a map with pins using Leaflet to learn How to load a geoJSON of points in Leaflet JS and add your custom pins or icons.

Display a Marker on the map using Leaflet

Display Marker

Tutorials

Add default map markers using Leaflet JS web mapping library.

How to make a map with pins using Leaflet

Make a map with pins

Examples

Create maps with custom pins and markers in Leaflet JS.

Was this helpful?