check the current location using js

Solutions on MaxInterview for check the current location using js by the best coders in the world

showing results for - "check the current location using js"
Emmanuel
08 Jul 2018
1   var x = document.getElementById("demo");
2    function getLocation() {
3        if (navigator.geolocation) {
4            navigator.geolocation.getCurrentPosition(showPosition, function (error) {
5                var x = document.getElementById("demo");
6                switch(error.code) {
7                    case error.PERMISSION_DENIED:
8                        x.innerHTML = "User denied the request for Geolocation."
9                        break;
10                    case error.POSITION_UNAVAILABLE:
11                        x.innerHTML = "Location information is unavailable."
12                        break;
13                    case error.TIMEOUT:
14                        x.innerHTML = "The request to get user location timed out."
15                        break;
16                    case error.UNKNOWN_ERROR:
17                        x.innerHTML = "An unknown error occurred."
18                        break;
19                }
20            });
21        } else {
22            var x = document.getElementById("demo");
23            x.innerHTML = "Geolocation is not supported by this browser.";
24        }
25    }
26
27    function showPosition(position) {
28        var x = document.getElementById("demo");
29        x.innerHTML = "Latitude: " + position.coords.latitude +
30            "<br>Longitude: " + position.coords.longitude;
31    }