-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
63 lines (51 loc) · 2.26 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Geolocation test</title>
</head>
<body>
<button id="find-me">Show my location</button><br />
<p id="status"></p>
<script>
function geoFindMe() {
const status = document.querySelector('#status');
const mapFrame = document.querySelector('#map-frame');
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
const bbox = `${longitude}%2C${latitude}%2C${longitude}$2C${latitude}`;
const marker = `${latitude}%2C${longitude}`;
if (mapFrame) {
mapFrame.src = `https://www.openstreetmap.org/export/embed.html?bbox=${bbox}&layer=mapnik&marker=${marker}`;
} else {
const newIframe = document.createElement('iframe');
newIframe.id = 'map-frame';
newIframe.frameBorder = 0;
newIframe.width = 425;
newIframe.height = 350;
newIframe.scrolling = 'no';
newIframe.marginHeight = 0;
newIframe.marginWidth = 0;
newIframe.style = "border: 1px solid black; margin-top: 10px;";
newIframe.src = `https://www.openstreetmap.org/export/embed.html?bbox=${bbox}&layer=mapnik&marker=${marker}`;
document.body.insertBefore(newIframe, status);
}
status.textContent = '';
}
function error(error) {
status.textContent = 'Unable to retrieve your location';
}
if (!navigator.geolocation) {
status.textContent = 'Geolocation is not supported by your browser';
} else {
status.textContent = 'Locating…';
navigator.geolocation.getCurrentPosition(success, error);
}
}
document.querySelector('#find-me').addEventListener('click', geoFindMe);
</script>
</body>
</html>