-
Notifications
You must be signed in to change notification settings - Fork 5
/
scripts.js
141 lines (117 loc) · 4.18 KB
/
scripts.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const btn = document.querySelector("#button-search");
let searchInput = document.querySelector("#search");
let inputValue = searchInput.value;
const ipAddressElement = document.getElementById("ip-address");
const locationElement = document.getElementById("location");
const timeZoneElement = document.getElementById("time-zone");
const ispElement = document.getElementById("isp");
var map = L.map("map").setView([51.505, -0.09], 13);
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19,
attribution:
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
}).addTo(map);
let marker; // Declare marker globally
let circle; // Declare circle globally
// Function to remove existing marker and circle
const removeMarkerAndCircle = function () {
if (marker) {
map.removeLayer(marker);
}
if (circle) {
map.removeLayer(circle);
}
};
// Function to add marker and circle
const addMarkerAndCircle = function (lat, lng) {
marker = L.marker([lat, lng]).addTo(map);
circle = L.circle([lat, lng], {
color: "red",
fillColor: "#f03",
fillOpacity: 0.5,
radius: 500,
}).addTo(map);
marker.bindPopup("<b>You are Here!").openPopup();
};
//Automatically Fetch User-Data
const leafed_auto = function (result) {
removeMarkerAndCircle(); // Remove existing marker and circle
map.setView([result.latitude, result.longitude], 13);
addMarkerAndCircle(result.latitude, result.longitude); // Add new marker and circle
};
//Automatically Fetch User-Data
const fetchUserIPdata = function () {
fetch("https://ipapi.co/json/")
.then((res) => res.json())
.then((result) => {
console.log(result);
renderData_auto(result);
leafed_auto(result);
});
};
window.addEventListener("load", fetchUserIPdata);
const renderData_auto = function (result) {
searchInput.value = result.ip;
ipAddressElement.innerText = result.ip;
locationElement.innerText = `${result.region},${result.city},${result.country}`;
timeZoneElement.innerText = `${result.utc_offset} , ${result.timezone}`;
ispElement.innerText = result.org;
};
const fetchIPdata = function () {
const inputValue = searchInput.value;
const loadingSpinner = document.getElementById("loading-spinner");
// Hide search button and show loading spinner
btn.style.display = "none";
loadingSpinner.style.display = "block";
// console.log(inputValue);
fetch(
`https://geo.ipify.org/api/v2/country,city?apiKey=at_dM3QxViBz1x1GNAVqqu4RKglkFTYi&ipAddress=${inputValue}`
)
.then((res) => {
if (!res.ok) {
throw new Error("Invalid IP address");
}
return res.json();
})
.then((result) => {
// Hide loading spinner on successful response and show search button
loadingSpinner.style.display = "none";
btn.style.display = "inline-block";
// Hide loading spinner on successful response
loadingSpinner.style.display = "none";
renderData(result);
leafed(result);
}).catch((error) => {
// Hide loading spinner on error and show search button
loadingSpinner.style.display = "none";
btn.style.display = "inline-block";
console.error(error); // Log the error for debugging
showToast(error.message);
});
};
const renderData = function (result) {
ipAddressElement.innerText = result.ip;
locationElement.innerText = `${result.location.region},${result.location.city},${result.location.country}`;
timeZoneElement.innerText = result.location.timezone;
ispElement.innerText = result.isp;
};
btn.addEventListener("click", function (e) {
e.preventDefault();
fetchIPdata();
});
const leafed = function (result) {
map.setView([result.location.lat, result.location.lng], 13);
removeMarkerAndCircle(); // Remove existing marker and circle
addMarkerAndCircle(result.location.lat, result.location.lng); // Add new marker and circle
};
// Function to display toast messages
const showToast = function (message) {
const toastContainer = document.getElementById("toast-container");
const toast = document.createElement("div");
toast.classList.add("toast");
toast.textContent = message;
toastContainer.appendChild(toast);
setTimeout(() => {
toast.remove();
}, 3000);
};