get user geo position in html5

Solutions on MaxInterview for get user geo position in html5 by the best coders in the world

showing results for - "get user geo position in html5"
Kevin
15 Jun 2016
1<!DOCTYPE html>
2<html lang="en">
3
4<head>
5    <meta charset="UTF-8">
6    <meta http-equiv="X-UA-Compatible" content="IE=edge">
7    <meta name="viewport" content="width=device-width, initial-scale=1.0">
8    <title>Users Position</title>
9</head>
10
11<body>
12    <div id="demo"></div>
13    <script>
14        const div = document.querySelector('div');
15        if (navigator.geolocation) {
16            navigator.geolocation.getCurrentPosition(showPosition, showError);
17        }
18
19        function showPosition(postion) {
20            div.innerHTML = `Latitude: ${postion.coords.latitude} Longitude: ${postion.coords.longitude}`
21        }
22
23        function showError(error) {
24            switch (error.code) {
25                case error.PERMISSION_DENIED:
26                    div.innerHTML = "User denied the request for Geolocation."
27                    break;
28                case error.POSITION_UNAVAILABLE:
29                    div.innerHTML = "Location information is unavailable."
30                    break;
31                case error.TIMEOUT:
32                    div.innerHTML = "The request to get user location timed out."
33                    break;
34                case error.UNKNOWN_ERROR:
35                    div.innerHTML = "An unknown error occurred."
36                    break;
37            }
38        }
39    </script>
40</body>
41
42</html>
Rafael
14 Jun 2019
1<!DOCTYPE html>
2<html lang="en">
3
4<head>
5    <meta charset="UTF-8">
6    <meta http-equiv="X-UA-Compatible" content="IE=edge">
7    <meta name="viewport" content="width=device-width, initial-scale=1.0">
8    <title>Users Position</title>
9</head>
10
11<body>
12    <div id="demo"></div>
13    <script>
14        const div = document.querySelector('div');
15        if (navigator.geolocation) {
16            navigator.geolocation.getCurrentPosition(showPosition);
17        } else {
18            div.innerHTML = 'The are not allowed your location';
19        }
20
21        function showPosition(postion) {
22            div.innerHTML = `Latitude: ${postion.coords.latitude} Longitude: ${postion.coords.longitude}`
23        }
24    </script>
25</body>
26
27</html>