-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (63 loc) · 2.33 KB
/
Copy pathscript.js
File metadata and controls
81 lines (63 loc) · 2.33 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
69
70
71
72
73
74
75
76
77
78
79
80
81
// Geolocation API
// Example One
const textOne = document.getElementById("textOne");
function getLocationOne() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPositionOne);
} else {
textOne.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPositionOne(position) {
textOne.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
// Example Two
const textTwo = document.getElementById("textTwo");
function getLocationTwo() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPositionTwo, showError);
} else {
textTwo.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPositionTwo(position) {
textTwo.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
textTwo.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
textTwo.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
textTwo.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
textTwo.innerHTML = "An unknown error occurred."
break;
}
}
// Example Three
const textThree = document.getElementById("textThree");
function getLocationThree() {
if (navigator.geolocation) {
navigator.geolocation.watchPosition(showPositionThree);
} else {
textThree.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPositionThree(position) {
textThree.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
// Show Position Example Four
let mapholder = document.getElementById("mapholder")
function showPositionFour(position) {
let latlon = position.coords.latitude + "," + position.coords.longitude;
let img_url = "https://maps.googleapis.com/maps/api/staticmap?center=" + latlon + "&zoom=14&size=400x300&sensor=false&key=YOUR_KEY";
mapholder.innerHTML = "<img src='" + img_url + "'>";
}