Please can you help me. my code doesn't work

<!DOCTYPE html>
<html>
<head>
    <title>Geolocation Example</title>
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY"></script>
    <style>
        #map {
            height: 400px;
            width: 100%;
        }
    </style>
</head>
<body>
    <h3>Geolocation Example</h3>
    <div id="map"></div>
    <script>
        // Vérifier si la géolocalisation est disponible
        if ("geolocation" in navigator) {
            console.log("La géolocalisation est disponible.");
        } else {
            console.log("La géolocalisation n'est pas disponible.");
        }

        // Fonction pour initialiser la carte avec la position de l'utilisateur
        function initMap(latitude, longitude) {
            var userLocation = { lat: latitude, lng: longitude };
            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 15,
                center: userLocation
            });

            var marker = new google.maps.Marker({
                position: userLocation,
                map: map
            });
        }

        // Fonction pour obtenir la position de l'utilisateur
        function getLocation() {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition, showError);
            } else {
                alert("La géolocalisation n'est pas prise en charge par ce navigateur.");
            }
        }

        // Fonction pour afficher la position de l'utilisateur
        function showPosition(position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            console.log("Latitude: " + latitude + " Longitude: " + longitude);

            // Initialiser la carte avec la position de l'utilisateur
            initMap(latitude, longitude);
        }

        // Fonction pour gérer les erreurs de géolocalisation
        function showError(error) {
            switch (error.code) {
                case error.PERMISSION_DENIED:
                    alert("L'utilisateur a refusé la demande de géolocalisation.");
                    break;
                case error.POSITION_UNAVAILABLE:
                    alert("Les informations de localisation sont indisponibles.");
                    break;
                case error.TIMEOUT:
                    alert("La demande de localisation a expiré.");
                    break;
                case error.UNKNOWN_ERROR:
                    alert("Une erreur inconnue s'est produite.");
                    break;
            }
        }

        // Appeler la fonction getLocation pour obtenir la position de l'utilisateur
        getLocation();
    </script>
</body>
</html>

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

geolocation does not work

can you expand? how to reproduce the issue? what should happen instead?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.