Skip to content
Add an Icon to the Map

Add an Icon to the Map

Load a custom icon image and use it as a symbol layer.

const API_KEY = 'toursprung';

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 12,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    map.on('load', () => {
        map.loadImage('https://maplibre.org/maplibre-gl-js/docs/assets/custom_marker.png', (err, image) => {
            if (err) throw err;
            map.addImage('custom-marker', image);

            map.addSource('points', {
                type: 'geojson',
                data: {
                    type: 'FeatureCollection',
                    features: [
                        { type: 'Feature', geometry: { type: 'Point', coordinates: [11.39085, 47.27574] }, properties: { title: 'Innsbruck' } },
                        { type: 'Feature', geometry: { type: 'Point', coordinates: [13.0550, 47.8095] }, properties: { title: 'Salzburg' } },
                        { type: 'Feature', geometry: { type: 'Point', coordinates: [16.3738, 48.2082] }, properties: { title: 'Vienna' } }
                    ]
                }
            });

            map.addLayer({
                id: 'symbols',
                type: 'symbol',
                source: 'points',
                layout: {
                    'icon-image': 'custom-marker',
                    'icon-size': 1.5,
                    'text-field': ['get', 'title'],
                    'text-offset': [0, 1.8],
                    'text-anchor': 'top'
                }
            });
        });
    });
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Add an Icon to the Map – Maptoolkit Maps JS</title>
    <meta property="og:description" content="Load a custom icon image and use it as a symbol layer." />
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/@maptoolkit/maps@11.0.0-beta.2/dist/maptoolkit.css" />
    <style>
        html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
        #map { width: 100%; height: 100%; }
    </style>
</head>
<body>
<div id="map"></div>
<script>
    const API_KEY = 'toursprung';

    const map = new maptoolkit.Map({
        container: 'map',
        style: `https://static.maptoolkit.net/styles/toursprung/terrain.json?api_key=${API_KEY}`,
        center: [11.39085, 47.27574],
        zoom: 12,
        attributionControl: { compact: false }
    });

    map.addControl(new maptoolkit.NavigationControl(), 'top-right');

    map.on('load', () => {
        map.loadImage('https://maplibre.org/maplibre-gl-js/docs/assets/custom_marker.png', (err, image) => {
            if (err) throw err;
            map.addImage('custom-marker', image);

            map.addSource('points', {
                type: 'geojson',
                data: {
                    type: 'FeatureCollection',
                    features: [
                        { type: 'Feature', geometry: { type: 'Point', coordinates: [11.39085, 47.27574] }, properties: { title: 'Innsbruck' } },
                        { type: 'Feature', geometry: { type: 'Point', coordinates: [13.0550, 47.8095] }, properties: { title: 'Salzburg' } },
                        { type: 'Feature', geometry: { type: 'Point', coordinates: [16.3738, 48.2082] }, properties: { title: 'Vienna' } }
                    ]
                }
            });

            map.addLayer({
                id: 'symbols',
                type: 'symbol',
                source: 'points',
                layout: {
                    'icon-image': 'custom-marker',
                    'icon-size': 1.5,
                    'text-field': ['get', 'title'],
                    'text-offset': [0, 1.8],
                    'text-anchor': 'top'
                }
            });
        });
    });
</script>
</body>
</html>