Repository navigation
Expand file tree
/
Copy pathLocation-get.html
More file actions
68 lines (58 loc) · 1.64 KB
/
Copy pathLocation-get.html
File metadata and controls
68 lines (58 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html>
<head>
<style type="text/css" media="all">
button {
padding: 4px 6px;
border: 1px solid blue;
outline: none;
margin: 5em 10em;
}
.Location {
text-align: center;
}
</style>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Getting Location</title>
</head>
<body>
<div class="Location">
<h3>Your Location will Appear here</h3>
</div>
<button>Get Location</button>
</body>
<p>This App Uses navigator.geoLocation api and then get the name of coords region use a third party api</p>
<script>
const button = document.querySelector("button")
const divBox = document.querySelector(".Location h3")
button.onclick =
function getLocation() {
// body...
let startTime = performance.now()
divBox.innerText = "Getting Your Location"
navigator.geolocation.getCurrentPosition(done, notdone)
}
function done(position) {
console.log(position)
const { longitude, latitude} = position.coords;
console.table(longitude, latitude)
getNameOfCoords(latitude,longitude)
}
function notdone(error) {
console.log(error)
}
async function getNameOfCoords(latitude,longitude){
const url = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${latitude}&lon=${longitude}`;
fetch(url).then(data => {
return data.json()
}).then(data => {
let timeEnd = performance.now()
console.table(data.address)
divBox.innerText = data.address.municipality
}).catch(err => {
divBox.innerText = "Plz give Location permission or turn on Location"
})
}
</script>
</html>