-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfo.js
103 lines (94 loc) · 3.6 KB
/
info.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Function to make an AJAX request
function ajaxRequest(url, method, data, callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback(xhr.responseText);
} else {
console.error("AJAX request failed with status " + xhr.status);
}
}
};
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
}
// Function to update the page with location information
function updateLocationInfo(locationData) {
var locationInfoElement = document.getElementById("locationInfo");
locationInfoElement.innerHTML = locationData;
}
// Function to get user's location
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
document.getElementById("locationInfo").innerHTML = "Geolocation is not supported by this browser.";
}
}
// Function to handle successful geolocation
function showPosition(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// Send the user's location to the PHP script
ajaxRequest("./info.php", "POST", "latitude=" + latitude + "&longitude=" + longitude, function (response) {
updateLocationInfo(response);
});
}
// Function to handle geolocation errors
function showError(error) {
var locationInfoElement = document.getElementById("locationInfo");
var errorMessage = "";
switch (error.code) {
case error.PERMISSION_DENIED:
errorMessage = "<div class='warning'>You denied the request for Geolocation!</div>";
break;
case error.POSITION_UNAVAILABLE:
errorMessage = "<div class='warning'>Location information is unavailable!</div>";
break;
case error.TIMEOUT:
errorMessage = "<div class='warning'>The request to get user location timed out!</div>";
break;
case error.UNKNOWN_ERROR:
errorMessage = "<div class='warning'>An unknown error occurred!</div>";
break;
}
// load data from json
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var locations = JSON.parse(xhr.responseText);
var maxLocations = 3; // Maximum number of locations to display
// Generate random indices
var rand = [];
while (rand.length < maxLocations && rand.length < locations.length) {
var randomIndex = Math.floor(Math.random() * locations.length);
if (rand.indexOf(randomIndex) === -1) {
rand.push(randomIndex);
}
}
if (rand.length > 0) {
// errorMessage += " Here are some random default locations:";
for (var i = 0; i < rand.length; i++) {
var index = rand[i];
errorMessage += "<p class='default_location'><strong>Office: </strong> " + locations[index].street_address + ", ";
errorMessage += locations[index].city + ", ";
errorMessage += locations[index].state + " - ";
errorMessage += locations[index].zip + ", ";
errorMessage += locations[index].country;
errorMessage += "<br><strong>Customer Support:</strong> " + locations[index].hotline + "</p>";
}
}
locationInfoElement.innerHTML = errorMessage;
} else {
locationInfoElement.innerHTML = errorMessage;
}
}
};
xhr.open("GET", "default_location.json", true);
xhr.send();
}
// Trigger location retrieval when the page loads
window.addEventListener("load", getLocation);