Skip to content

Geocoding Point

This example demonstrates how to display geocoding information on a map using the Geocoding service. Click on any location on the map to retrieve geocoding information.

<!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/geocoding/umd/geocoding.min.js"></script>
        <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 apiKey = "INSERT API-KEY";

            const map = new smartmapsgl.Map({
                apiKey: apiKey,
                container: "map",
                center: { lat: 49.02164948779226, lng: 8.439330018049352 },
                zoom: 10,
                style: smartmapsgl.MapStyle.LIGHT
            });

            const geocoder = new smartmaps.geocodingService.Geocoder(apiKey);

            map.on('click', async (event) => {

                const geocodeData = await geocoder.geocodeReverse(event.lngLat);
                const geocodeProperties = geocodeData.features[0].properties

                const popupContent = `
                                <strong>Geocoded Data:</strong><br>
                                City: ${geocodeProperties.city}<br>
                                City Part: ${geocodeProperties.cityPart}<br>
                                District: ${geocodeProperties.district}<br>
                                Country: ${geocodeProperties.country}<br>
                                Street: ${geocodeProperties.street}<br>
                                ZIP: ${geocodeProperties.zip}<br>

                            `;

                new smartmapsgl.Popup()
                    .setLngLat(event.lngLat)
                    .setHTML(popupContent)
                    .addTo(map);
            });
        </script>
    </body>
</html>