Answers for "html5 geolocation to get user location"

0

get user geo position in html5

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Users Position</title>
</head>

<body>
    <div id="demo"></div>
    <script>
        const div = document.querySelector('div');
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition, showError);
        }

        function showPosition(postion) {
            div.innerHTML = `Latitude: ${postion.coords.latitude} Longitude: ${postion.coords.longitude}`
        }

        function showError(error) {
            switch (error.code) {
                case error.PERMISSION_DENIED:
                    div.innerHTML = "User denied the request for Geolocation."
                    break;
                case error.POSITION_UNAVAILABLE:
                    div.innerHTML = "Location information is unavailable."
                    break;
                case error.TIMEOUT:
                    div.innerHTML = "The request to get user location timed out."
                    break;
                case error.UNKNOWN_ERROR:
                    div.innerHTML = "An unknown error occurred."
                    break;
            }
        }
    </script>
</body>

</html>
Posted by: Guest on August-17-2021
0

get user geo position in html5

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Users Position</title>
</head>

<body>
    <div id="demo"></div>
    <script>
        const div = document.querySelector('div');
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition, showError);
        }

        function showPosition(postion) {
            div.innerHTML = `Latitude: ${postion.coords.latitude} Longitude: ${postion.coords.longitude}`
        }

        function showError(error) {
            switch (error.code) {
                case error.PERMISSION_DENIED:
                    div.innerHTML = "User denied the request for Geolocation."
                    break;
                case error.POSITION_UNAVAILABLE:
                    div.innerHTML = "Location information is unavailable."
                    break;
                case error.TIMEOUT:
                    div.innerHTML = "The request to get user location timed out."
                    break;
                case error.UNKNOWN_ERROR:
                    div.innerHTML = "An unknown error occurred."
                    break;
            }
        }
    </script>
</body>

</html>
Posted by: Guest on August-17-2021

Browse Popular Code Answers by Language