Skip to content

Add multiple markers

Add multiple markers to your map.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <script src="https://cdn.smartmaps.cloud/packages/smartmaps/smartmaps-gl/v2/umd/smartmaps-gl.min.js"></script>
        <style>
            body {
                margin: 0;
                padding: 0;
            }

            html,
            body,
            #map {
                height: 100%;
            }
        </style>
    </head>

    <body>
        <div id="map"></div>

        <script>
            const map = new smartmapsgl.Map({
                apiKey: "YOUR API-KEY",
                container: 'map',
                center: [-120, 50],
                zoom: 2,
                style: 'essential'
            });

            map.on('load', () => {
                // Fetch the GeoJSON data
                fetch('../data/multiple-markers.geojson')
                    .then(response => response.json())
                    .then(data => {
                        // Add markers for each feature in the GeoJSON
                        data.features.forEach(feature => {
                            const coordinates = feature.geometry.coordinates;

                            const marker = new smartmapsgl.Marker()
                                .setLngLat(coordinates)
                                .addTo(map);

                            // Add popup to the marker
                            marker.setPopup(
                                new smartmapsgl.Popup()
                                    .setHTML(`Coordinates: ${coordinates}`)
                            );
                        });
                    })
                    .catch(error => console.error('Error loading GeoJSON:', error));
            });
        </script>
    </body>
</html>