Skip to content

Add a geolocation control

Add a geolocation control to identify and display the user's current location on the map.

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

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

    <body>
        <div id="map"></div>
        <script>
            const map = new smartmapsgl.Map({
                apiKey: "INSERT API-KEY",
                container: "map",
                center: [11.39085, 47.27574],
                zoom: 2,
                style: smartmapsgl.MapStyle.AUTO
            });

            // Add geolocate control to the map.
            map.addControl(
                new smartmapsgl.GeolocateControl({
                    positionOptions: {
                        enableHighAccuracy: true
                    },
                    trackUserLocation: true
                })
            );
        </script>
    </body>
</html>