Exécuter
❯
Exécuter le code
Ctrl+Alt+R
Enregistrer le code
Ctrl+Alt+A
Changer d'orientation
Ctrl+Alt+O
Changer de thème
Ctrl+Alt+D
<!DOCTYPE html> <html> <body> <h1>HTML Géolocalisation</h1> <p>Cliquez sur le bouton pour obtenir vos coordonnées.</p> <button onclick="getLocation()">Essayez-le</button> <p id="demo"></p> <script> const x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.watchPosition(success, error); } else { x.innerHTML = "La géolocalisation n'est pas prise en charge par ce navigateur."; } } function success(position) { x.innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } function error(error) { switch(error.code) { cas error.PERMISSION_DENIED : x.innerHTML = "L'utilisateur a refusé la demande de géolocalisation." break; cas error.POSITION_UNAVAILABLE : x.innerHTML = "Les informations de localisation ne sont pas disponibles." break; cas error.TIMEOUT: x.innerHTML = "La demande pour obtenir la localisation de l'utilisateur a expiré." pause; cas erreur.UNKNOWN_ERROR : x.innerHTML = "Une erreur inconnue s'est produite." pause; } } </script> </body> </html>